Linux -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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值