Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Displaying enum symbol names in GDB

Reply
Thread Tools

Displaying enum symbol names in GDB

 
 
omkar pangarkar
Guest
Posts: n/a
 
      12-26-2008
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.
 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      12-26-2008
On Dec 26, 8:59*pm, omkar pangarkar <(E-Mail Removed)> wrote:

> 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).
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      12-26-2008
omkar pangarkar wrote:
> 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

 
Reply With Quote
 
omkar pangarkar
Guest
Posts: n/a
 
      12-26-2008
On Dec 26, 7:26*pm, Ulrich Eckhardt <(E-Mail Removed)> wrote:
> omkar pangarkar wrote:
> > 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


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

fruitkind my_fruit = apple;


 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      01-05-2009
#if OT(gdb)
On Fri, 26 Dec 2008 05:59:51 -0800 (PST), omkar pangarkar
<(E-Mail Removed)> wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"No symbol" error using pystack gdb macro debby@josesmex.com Python 1 03-02-2013 07:08 AM
Debugging with gdb: pystack macro for gdb to show python stack trace Timothy Madden Python 1 09-17-2009 11:47 AM
GDB - no debugging symbols found (GDB Vs GCC?) seba C++ 1 03-22-2007 02:02 PM
Capturing gdb output in pyhton script using gdb -batch -x arguments Surendra Python 0 03-23-2006 02:47 PM
what's differnece between #ifdef symbol and #if defined(symbol) baumann@pan C Programming 1 04-15-2005 08:25 AM



Advertisments