Displaying enum symbol names in GDB

O

omkar pangarkar

For a simple code as follows :

typedef enum {
apple,
banana,
grape
} fruit;

typedef unsigned short fruitkind;

when I have this in code :

fruitkind = apple;

and see the value through GDB while debugging, I get fruitkind = 0
(obviously). What if I want to print the symbol "apple" when I ask for
value of fruitkind. Is there some GDB trick/setting, to do so. This
will be very helpful with enums having large number of members. When
you have large number of predefined states, its represented with this
style and stored in shorts. These enum assigned variables (like
fruitkind) are used widely in code, making it difficult to understand
it when walking through a debugger. Is there any other way to achieve
this without using this style, so that its easy for debugging?

I am posting this on comp.lang.c, as some of you might have solved
this problem while working with C.


Thanks,
Omkarenator.
 
T

Tomás Ó hÉilidhe

I get fruitkind = 0
(obviously). What if I want to print the symbol "apple" when I ask for
value of fruitkind.


Did you make sure to compile with "-g" (sorry I have to ask).

To be honest I don't know whether GDB can show enum names.

However if it turns out that GDB isn't capable of showing enum names,
you could get creative and use the addresses of objects:

char const o_apple, *const apple = &o_apple,
o_orange, *const orange = &o_orange,
o_mango, *const mango = &o_mango;

typedef char const *fruit;

fruit my_fruit = orange;

I'm pretty sure GDB will then tell you the name of the object to which
your pointer points. (Yes I know it's a hideous solution but it's
quick and dirty if it does what you want).
 
U

Ulrich Eckhardt

omkar said:
For a simple code as follows :

typedef enum {
apple,
banana,
grape
} fruit;

typedef unsigned short fruitkind;

when I have this in code :

fruitkind = apple;

No you don't. ;)

'fruitkind' is a type, and you can not assign to a type in C. What you have
is probably an unsigned short that is assigned a value, at least I'll
assume that.
and see the value through GDB while debugging, I get fruitkind = 0
(obviously). What if I want to print the symbol "apple" when I ask for
value of fruitkind.

An unsigned short will always and for any debugger remain a number. What you
should do instead is assign to a variable of type 'fruit', of which the
debugger knows that it is an enumeration and at least has a chance to
decode it somehow. Also, it makes the code much clearer, because the reader
immediately knows that it is actually a value from a limited set of
possible values.

Uli
 
O

omkar pangarkar

No you don't. ;)

'fruitkind' is a type, and you can not assign to a type in C. What you have
is probably an unsigned short that is assigned a value, at least I'll
assume that.


An unsigned short will always and for any debugger remain a number. What you
should do instead is assign to a variable of type 'fruit', of which the
debugger knows that it is an enumeration and at least has a chance to
decode it somehow. Also, it makes the code much clearer, because the reader
immediately knows that it is actually a value from a limited set of
possible values.

Uli

yeah its a type. I meant assigning to a variable of that type.
like

fruitkind my_fruit = apple;
 
D

David Thompson

#if OT(gdb)
typedef enum { <snip> } fruit;

typedef unsigned short fruitkind;

fruitkind = apple;
corrected to my_fruit (of type fruitkind)
and see the value through GDB while debugging, I get fruitkind = 0
(obviously). What if I want to print the symbol "apple" when I ask for
value of fruitkind. Is there some GDB trick/setting, to do so.

I don't know if it even counts as a trick, but
p (fruitkind) my_fruit

Probably you could pre-define a bunch of commands like this (one for
each variable you need) in .gdbrc or wherever it lives nowadays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top