Displaying enum symbol names in GDB

Discussion in 'C Programming' started by omkar pangarkar, Dec 26, 2008.

  1. 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.
     
    omkar pangarkar, Dec 26, 2008
    #1
    1. Advertising

  2. On Dec 26, 8:59 pm, omkar pangarkar <> 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).
     
    Tomás Ó hÉilidhe, Dec 26, 2008
    #2
    1. Advertising

  3. 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
     
    Ulrich Eckhardt, Dec 26, 2008
    #3
  4. On Dec 26, 7:26 pm, Ulrich Eckhardt <> 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;
     
    omkar pangarkar, Dec 26, 2008
    #4
  5. #if OT(gdb)
    On Fri, 26 Dec 2008 05:59:51 -0800 (PST), omkar pangarkar
    <> 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.
     
    David Thompson, Jan 5, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surendra
    Replies:
    0
    Views:
    521
    Surendra
    Mar 23, 2006
  2. baumann@pan
    Replies:
    1
    Views:
    757
    Richard Bos
    Apr 15, 2005
  3. seba
    Replies:
    1
    Views:
    1,464
    mlimber
    Mar 22, 2007
  4. Timothy Madden
    Replies:
    1
    Views:
    1,662
    Timothy Madden
    Sep 17, 2009
  5. Replies:
    1
    Views:
    177
    dieter
    Mar 2, 2013
Loading...

Share This Page