Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print <incomplete type> variable in gdb

Tags:

gdb

Sometimes gdb prints "incomplete type" for some type of variables. What does this mean and how can we see that value?

like image 942
grigy Avatar asked Oct 24 '08 07:10

grigy


People also ask

Which command in gdb is used to find the type of variable?

The ptype [ARG] command will print the type. Show activity on this post. This question may be related: vtable in polymorphic class of C++ using gdb: (gdb) help set print object Set printing of object's derived type based on vtable info.

What does 0x0 mean in gdb?

0x0 refers the the address at 0 a.k.a NULL . This means there is no object at that location and trying to access anything at that address will likely result in a segmentation fault.

What does gdb print do?

Print settings. GDB provides the following ways to control how arrays, structures, and symbols are printed. You can use `set print address off' to eliminate all machine dependent displays from the GDB interface.


2 Answers

It means that the type of that variable has been incompletely specified. For example:

struct hatstand; struct hatstand *foo; 

GDB knows that foo is a pointer to a hatstand structure, but the members of that structure haven't been defined. Hence, "incomplete type".

To print the value, you can cast it to a compatible type.

For example, if you know that foo is really a pointer to a lampshade structure:

print (struct lampshade *)foo 

Or, you could print it as a generic pointer, or treat it as if it were an integer:

print (void *)foo print (int)foo 

See also these pages from the GDB manual:

  • http://sourceware.org/gdb/current/onlinedocs/gdb/Data.html#Data
  • http://sourceware.org/gdb/current/onlinedocs/gdb/Symbols.html#Symbols
like image 83
Daniel Cassidy Avatar answered Sep 18 '22 13:09

Daniel Cassidy


What I've found is that if you disassemble a function that uses the incomplete struct type gdb 'discovers' the struct members and can subsequently display them. For example, say you have a string struct:

struct my_string {     char * _string,     int _size } ; 

some functions to create and get the string via pointer:

my_string * create_string(const char *) {...} const char * get_string(my_string *){...} 

and a test that creates a string:

int main(int argc, char *argv[]) {     my_string *str = create_string("Hello World!") ;     printf("String value: %s\n", get_string(str)) ;     ... } 

Run it in gdb and try to 'print *str' and you'll get an 'incomplete type' response. However, try 'disassemble get_string' and then 'print *str' and it'll display the struct and values properly. I have no idea why this works, but it does.

like image 37
Peter Avatar answered Sep 20 '22 13:09

Peter