[轉載]:Linux -gdb如何顯示宏定義的值_波波諸葛偉-CSDN博客_gdb打印宏的值
本文大概闡述如何讓gdb能夠顯示宏定義的值以及對應的原理
gcc默認編譯的時候,gdb調試過程中是不能看到宏定義的,"p 宏名" 會顯示錯誤,如下:
(gdb) p CLIENT6_BEGIN_FLG
No symbol "CLIENT6_BEGIN_FLG" in current context.
(gdb)
其中 #define CLIENT6_BEGIN_FLG "<Client6>"
這樣gdb調試過程中就很不直觀,尤其是很多大型程序中,宏定義比較復雜的時候。
如果需要在gdb中能查看宏定義,gcc編譯的時候需要加上 -gdwarf-2和-g3的參數。
下面看一下-gdward-2 和-g3參數的意思。
-g3 參數的意思:
man gcc,可以得出如下的解釋:
-glevel
-ggdblevel
-gstabslevel
-gcofflevel
-gxcofflevel
-gvmslevel
Request debugging information and also use level to specify how much information. The default level is 2.
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don’t plan to debug.
This includes descriptions of functions and external variables, but no information about local variables and no line numbers.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro
expansion when you use -g3.
加了-g3的參數后,gcc編譯的時候,會將擴展的debug 信息編譯進二進制文件里面,包括宏定義信息。
所以,如果要使用gdb調試二進制文件里面的宏定義信息,這個選項必須開啟。
-gdwarf-2 參數的意思:
man gcc, 可以得出如下相關的解釋:
-gdwarf-2 does not accept a concatenated debug level, because GCC used to support an option -gdwarf that meant to generate
debug information in version 1 of the DWARF format (which is very different from version 2), and it would have been too con-
fusing. That debug format is long obsolete, but the option cannot be changed now. Instead use an additional -glevel option
to change the debug level for DWARF2.
DWARF是一種應用的比較廣泛的elf(可執行和鏈接格式), 目前有dwarf1, dwarf2,dwarf3 3種版本,
其中版本1已經是比較老舊的,基本廢棄不用了。
這邊的-gdwarf-2意思是使用版本2的格式,
對dwarf格式感興趣的,可以看一下ibm的開發者文檔:
http://www.ibm.com/developerworks/cn/opensource/os-debugging/
里面會有一些解釋。
加上相關參數后,編譯:
gcc -gdwarf-2 -g3 test.c
gdb a.out后就能夠使用 "p 宏名" 輸出宏的內容了
(gdb) p CLIENT6_BEGIN_FLG
$1 = "<Client6>"
(gdb)
需要查看宏定義是如何被展開的,可以使用如下的命令:
macro expand macro_name