zoukankan      html  css  js  c++  java
  • Linux -gdb如何显示宏定义的值

    [转载]: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

  • 相关阅读:
    display ntp-service sessions
    display ntp-service status
    MySQL与telnet安装
    YL_组播_IGMPv2-v3
    YL_组播_PIM-DM协议原理
    YL_组播_IGMP协议原理
    IIS发布站点问题
    css 定位及遮罩层小技巧
    MYSQL查询某字段中以逗号分隔的字符串的方法
    零度
  • 原文地址:https://www.cnblogs.com/huixinquan/p/14873559.html
Copyright © 2011-2022 走看看