This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Accessing struct fields without casting


Hi list,

I'm working with some code base which expresses most internally
used values in terms of some struct "value_t" only containing a single
field, "kind". This field is used as an discriminator, and based on its
value the object is cast after which its available fields can be
accessed (see attached test case).

Since this is pretty hard to work with from within GDB, especially
because the "kind" field doesn't contain human readable text but rather
raw memory (a pointer to a globally defined object), I wrote a pretty
printer which detects the actual type of "value_t" objects, casts them,
and displays the actual typename when called with to_string() and
returns the casted value its fields when called with children().

However, this doesn't ease debugging much, because despite implementing
a children() method which lists all available fields GDB doesn't allow
to access those without an explicit cast (see attached sample).
> (gdb) print value->foo
> There is no member named foo.
> (gdb) print ((example_t*)value)->foo
> $1 = 42
This is even worse in the actual code, where value_t "subtypes" often
contain pointers to other value_t "subtypes", but since those pointers
are always of type "value_t*" I need to pretty print them in order to
know the type and then cast them in before I can finally access its fields.

Is it possible to teach GDB about the "actual" type of these objects, or
work around this problem in some other way so that I can access subtype
fields without having to cast each object manually?

Thanks,
-- 
Tim Besard
Computer Systems Lab
Department of Electronics & Information Systems
Ghent University

#include <stdio.h>
#include <stdlib.h>

#define KIND int kind;
#define example_kind 1

typedef struct {
  KIND
} value_t;

typedef struct {
  KIND
  int foo;
} example_t;

int main() {
  value_t *value = (value_t *)malloc(sizeof(example_t));
  ((example_t *)value)->kind = example_kind;
  ((example_t *)value)->foo = 42;

  switch (value->kind) {
  case example_kind:
    printf("example_t with foo=%d\n",
           ((example_t *)value)->foo);
    break;
  default:
    printf("unknown value kind\n");
  }

  return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]