使用GDB查找枚举的可能数值



使用如下声明和实例化的枚举:

enum test_enumeration
{
test1 = 4,
test2 = 10,
test3,
};
test_enumeration test_enum;

我可以打

(gdb) ptype test_enum

退出

type = enum test_enumeration {test1 = 4, test2 = 10, test3}

这给了我test1和test2的数值,但没有test3。如果我打电话给

(gdb) print (int)test3

GDB打印出值11。然而,我希望能够得到这样的东西:

type = enum test_enumeration {test1 = 4, test2 = 10, test3 = 11}

通过使用test_enum打印出整个类型定义
不幸的是

(gdb) ptype (int)test_enum

将类型返回为int,而不是值。

有没有办法打印出这样的枚举常量,或者有没有需要设置为始终显示其数字版本的选项?

GDB V10.1

这将打印出枚举类型的所有元素。该可执行文件需要使用debuginfo进行编译。

$ cat print-enum.py
import gdb
class PrintEnumCmd(gdb.Command):
"""print all elements of the given enum type"""
def __init__(self):
super(PrintEnumCmd, self).__init__("print-enum", gdb.COMMAND_DATA, gdb.COMPLETE_EXPRESSION)
def invoke(self, argstr, from_tty):
typename = argstr
if not typename or typename.isspace():
raise gdb.GdbError("Usage: print-enum type")
try:
t = gdb.lookup_type(typename)
except gdb.error:
typename = "enum " + typename
try:
t = gdb.lookup_type(typename)
except gdb.error:
raise gdb.GdbError("type " + typename + " not found")
if t.code != gdb.TYPE_CODE_ENUM:
raise gdb.GdbError("type " + typename + " is not an enum")
for f in t.fields():
print(f.name, "=", f.enumval)
PrintEnumCmd()
$ gdb enu
Reading symbols from enu...done.
(gdb) source print-enum.py
(gdb) print-enum
Usage: print-enum type
(gdb) print-enum test<tab>
test1             test2             test3             test_enumeration
(gdb) print-enum test_enumeration
test1 = 4
test2 = 10
test3 = 11

这在GDB中目前是不可能的。决定是否应打印= VAL零件的代码如下:

https://sourceware.org/git/?p=binutils-gdb.git;a=斑点;f=gdb/c-typeprint.c;h=0502d31有效9605e7e2e430c8ad72908792c1b475;hb=封头#l1607

只有当该值不比上一个枚举项的值多1时,才会打印该值。