2 * File types.c - datatype handling stuff for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 * This really doesn't do much at the moment, but it forms the framework
7 * upon which full support for datatype handling will eventually be hung.
15 #include <sys/types.h>
29 #define NR_TYPE_HASH 521
32 static int DEBUG_maxchar = 1024;
36 struct en_values* next;
45 struct datatype * type;
53 struct datatype * next;
66 unsigned short bitoff;
68 struct datatype * basetype;
73 struct datatype * pointsto;
77 struct datatype * rettype;
83 struct datatype * basictype;
88 struct member * members;
92 struct en_values * members;
102 #define BASIC_LONGLONG 6
103 #define BASIC_ULONGLONGI 7
104 #define BASIC_SHORT 8
105 #define BASIC_SHORTUI 9
106 #define BASIC_SCHAR 10
107 #define BASIC_UCHAR 11
109 #define BASIC_LONG_DOUBLE 13
110 #define BASIC_DOUBLE 14
111 #define BASIC_CMPLX_INT 15
112 #define BASIC_CMPLX_FLT 16
113 #define BASIC_CMPLX_DBL 17
114 #define BASIC_CMPLX_LONG_DBL 18
115 #define BASIC_VOID 19
117 struct datatype * DEBUG_TypeInt = NULL;
118 struct datatype * DEBUG_TypeIntConst = NULL;
119 struct datatype * DEBUG_TypeUSInt = NULL;
120 struct datatype * DEBUG_TypeString = NULL;
123 * All of the types that have been defined so far.
125 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
126 static struct datatype * pointer_types = NULL;
128 static unsigned int type_hash( const char * name )
130 unsigned int hash = 0;
138 hash = (hash << 4) + *p++;
140 if( (tmp = (hash & 0xf0000000)) )
146 return hash % NR_TYPE_HASH;
150 static struct datatype *
151 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
152 char * output_format)
156 struct datatype * dt;
157 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
163 hash = type_hash(name);
172 dt->next = type_hash_table[hash];
173 type_hash_table[hash] = dt;
174 dt->un.basic.basic_type = type;
175 dt->un.basic.basic_size = size;
176 dt->un.basic.b_signed = b_signed;
177 dt->un.basic.output_format = output_format;
185 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
187 struct datatype * dt = NULL;
189 if( typename != NULL )
191 for( dt = type_hash_table[hash]; dt; dt = dt->next )
193 if( xtype != dt->type || dt->name == NULL
194 || dt->name[0] != typename[0])
199 if( strcmp(dt->name, typename) == 0 )
210 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
212 struct datatype * dt = NULL;
216 * The last bucket is special, and is used to hold typeless names.
218 if( typename == NULL )
224 hash = type_hash(typename);
227 dt = DEBUG_LookupDataType(xtype, hash, typename);
231 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
235 memset(dt, 0, sizeof(*dt));
238 if( typename != NULL )
240 dt->name = xstrdup(typename);
246 if( xtype == POINTER )
248 dt->next = pointer_types;
253 dt->next = type_hash_table[hash];
254 type_hash_table[hash] = dt;
263 DEBUG_FindOrMakePointerType(struct datatype * reftype)
265 struct datatype * dt = NULL;
267 if( reftype != NULL )
269 for( dt = pointer_types; dt; dt = dt->next )
271 if( dt->type != POINTER )
276 if( dt->un.pointer.pointsto == reftype )
285 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
290 dt->un.pointer.pointsto = reftype;
291 dt->next = pointer_types;
302 static int beenhere = 0;
303 struct datatype * chartype;
305 if( beenhere++ != 0 )
311 * Special version of int used with constants of various kinds.
313 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
316 * Initialize a few builtin types.
320 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
321 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
322 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
323 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
324 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
325 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
326 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
327 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
328 DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
329 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
330 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
331 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
332 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
333 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
334 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
335 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
336 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
337 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
338 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
340 DEBUG_TypeString = DEBUG_NewDataType(POINTER, NULL);
341 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
344 * Now initialize the builtins for codeview.
346 DEBUG_InitCVDataTypes();
351 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
354 struct datatype * type2 = NULL;
355 struct en_values * e;
356 char * def_format = "0x%x";
359 assert(addr->type != NULL);
361 switch(addr->type->type)
364 if (!DBG_CHECK_READ_PTR( addr, addr->type->un.basic.basic_size))
369 memcpy(&rtn, (char *) addr->off, addr->type->un.basic.basic_size);
370 if( (addr->type->un.basic.b_signed)
371 && ((addr->type->un.basic.basic_size & 3) != 0)
372 && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
374 rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
376 if( addr->type->un.basic.output_format != NULL )
378 def_format = addr->type->un.basic.output_format;
382 * Check for single character prints that are out of range.
384 if( addr->type->un.basic.basic_size == 1
385 && strcmp(def_format, "'%c'") == 0
386 && ((rtn < 0x20) || (rtn > 0x80)) )
392 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
393 rtn = (unsigned int) *((unsigned char **)addr->off);
394 type2 = addr->type->un.pointer.pointsto;
395 if( type2->type == BASIC && type2->un.basic.basic_size == 1 )
397 def_format = "\"%s\"";
402 def_format = "0x%8.8x";
407 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
408 rtn = (unsigned int) *((unsigned char **)addr->off);
409 def_format = "0x%8.8x";
412 rtn = (unsigned int) *((unsigned char **)addr->off);
413 for(e = addr->type->un.enumeration.members; e; e = e->next )
415 if( e->value == rtn )
438 *format = def_format;
444 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
447 * Make sure that this really makes sense.
449 if( addr->type->type != POINTER )
455 *newtype = addr->type->un.pointer.pointsto;
456 return *(unsigned int*) (addr->off);
460 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
466 * Make sure that this really makes sense.
468 if( addr->type->type != STRUCT )
474 for(m = addr->type->un.structure.members; m; m = m->next)
476 if( strcmp(m->name, ele_name) == 0 )
478 addr->type = m->type;
479 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
482 * Bitfield operation. We have to extract the field and store
483 * it in a temporary buffer so that we get it all right.
485 *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
486 addr->off = (int) tmpbuf;
488 mask = 0xffffffff << (m->size);
491 * OK, now we have the correct part of the number.
492 * Check to see whether the basic type is signed or not, and if so,
493 * we need to sign extend the number.
495 if( m->type->type == BASIC && m->type->un.basic.b_signed != 0
496 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
503 addr->off += (m->offset >> 3);
514 DEBUG_SetStructSize(struct datatype * dt, int size)
516 assert(dt->type == STRUCT);
518 if( dt->un.structure.members != NULL )
523 dt->un.structure.size = size;
524 dt->un.structure.members = NULL;
530 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
533 assert( dt->type == dt2->type && ((dt->type == STRUCT) || (dt->type == ENUM)));
535 if( dt->type == STRUCT )
537 dt->un.structure.members = dt2->un.structure.members;
541 dt->un.enumeration.members = dt2->un.enumeration.members;
548 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
549 int offset, int size)
552 struct member * last;
553 struct en_values * e;
555 if( dt->type == STRUCT )
557 for(last = dt->un.structure.members; last; last = last->next)
559 if( (last->name[0] == name[0])
560 && (strcmp(last->name, name) == 0) )
564 if( last->next == NULL )
569 m = (struct member *) xmalloc(sizeof(struct member));
575 m->name = xstrdup(name);
581 m->next = dt->un.structure.members;
582 dt->un.structure.members = m;
590 * If the base type is bitfield, then adjust the offsets here so that we
591 * are able to look things up without lots of falter-all.
593 if( type->type == BITFIELD )
595 m->offset += m->type->un.bitfield.bitoff;
596 m->size = m->type->un.bitfield.nbits;
597 m->type = m->type->un.bitfield.basetype;
600 else if( dt->type == ENUM )
602 e = (struct en_values *) xmalloc(sizeof(struct en_values));
608 e->name = xstrdup(name);
610 e->next = dt->un.enumeration.members;
611 dt->un.enumeration.members = e;
621 DEBUG_GetPointerType(struct datatype * dt)
623 if( dt->type == POINTER )
625 return dt->un.pointer.pointsto;
632 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
637 dt->un.pointer.pointsto = dt2;
640 dt->un.funct.rettype = dt2;
650 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
652 assert(dt->type == ARRAY);
653 dt->un.array.start = min;
654 dt->un.array.end = max;
655 dt->un.array.basictype = dt2;
661 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
662 struct datatype * dt2)
664 assert(dt->type == BITFIELD);
665 dt->un.bitfield.bitoff = offset;
666 dt->un.bitfield.nbits = nbits;
667 dt->un.bitfield.basetype = dt2;
672 int DEBUG_GetObjectSize(struct datatype * dt)
682 return dt->un.basic.basic_size;
684 return sizeof(int *);
686 return dt->un.structure.size;
690 return (dt->un.array.end - dt->un.array.start)
691 * DEBUG_GetObjectSize(dt->un.array.basictype);
694 * Bitfields have to be handled seperately later on
695 * when we insert the element into the structure.
707 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
712 * Make sure that this really makes sense.
714 if( addr->type->type == POINTER )
717 * Get the base type, so we know how much to index by.
719 size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
720 result->type = addr->type->un.pointer.pointsto;
721 result->off = (*(unsigned int*) (addr->off)) + size * index;
723 else if (addr->type->type == ARRAY)
725 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
726 result->type = addr->type->un.array.basictype;
727 result->off = addr->off + size * (index - addr->type->un.array.start);
733 /***********************************************************************
736 * Implementation of the 'print' command.
739 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
750 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
754 if( addr->type == NULL )
756 fprintf(stderr, "Unable to evaluate expression\n");
765 if( DEBUG_nchar > DEBUG_maxchar )
767 fprintf(stderr, "...");
771 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
773 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
777 switch(addr->type->type)
783 DEBUG_PrintBasic(addr, 1, format);
786 DEBUG_nchar += fprintf(stderr, "{");
787 for(m = addr->type->un.structure.members; m; m = m->next)
790 DEBUG_FindStructElement(&addr1, m->name,
792 DEBUG_nchar += fprintf(stderr, "%s=", m->name);
793 DEBUG_Print(&addr1, 1, format, level + 1);
794 if( m->next != NULL )
796 DEBUG_nchar += fprintf(stderr, ", ");
798 if( DEBUG_nchar > DEBUG_maxchar )
800 fprintf(stderr, "...}");
804 DEBUG_nchar += fprintf(stderr, "}");
808 * Loop over all of the entries, printing stuff as we go.
810 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
814 * Special handling for character arrays.
816 pnt = (char *) addr->off;
817 DEBUG_nchar += fprintf(stderr, "\"");
818 for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
820 fputc(*pnt++, stderr);
822 if( DEBUG_nchar > DEBUG_maxchar )
824 fprintf(stderr, "...\"");
828 DEBUG_nchar += fprintf(stderr, "\"");
832 addr1.type = addr->type->un.array.basictype;
833 DEBUG_nchar += fprintf(stderr, "{");
834 for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
836 DEBUG_Print(&addr1, 1, format, level + 1);
838 if( i == addr->type->un.array.end )
840 DEBUG_nchar += fprintf(stderr, "}");
844 DEBUG_nchar += fprintf(stderr, ", ");
846 if( DEBUG_nchar > DEBUG_maxchar )
848 fprintf(stderr, "...}");
862 DEBUG_nchar += fprintf(stderr, "\n");
870 struct datatype * dt = NULL;
877 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
879 for( dt = type_hash_table[hash]; dt; dt = dt->next )
882 if( dt->name != NULL )
889 fprintf(stderr, "0x%p - BASIC(%s)\n",
893 fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
894 dt, name, dt->un.pointer.pointsto);
897 member_name = "none";
899 if( dt->un.structure.members != NULL
900 && dt->un.structure.members->name != NULL )
902 member_name = dt->un.structure.members->name;
903 for( m = dt->un.structure.members; m; m = m->next)
908 fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
909 dt->un.structure.size, nm, member_name);
912 fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
913 dt, name, dt->un.array.basictype);
916 fprintf(stderr, "0x%p - ENUM(%s)\n",
920 fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
923 fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
924 dt, name, dt->un.funct.rettype);
928 fprintf(stderr, "What???\n");
937 enum debug_type DEBUG_GetType(struct datatype * dt)
943 DEBUG_TypeCast(enum debug_type type, const char * name)
946 struct datatype * rtn;
949 * The last bucket is special, and is used to hold typeless names.
957 hash = type_hash(name);
960 rtn = DEBUG_LookupDataType(type, hash, name);
967 DEBUG_PrintTypeCast(struct datatype * dt)
972 if( dt->name != NULL )
980 fprintf(stderr, "%s", name);
983 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
984 fprintf(stderr, "*");
987 fprintf(stderr, "struct %s", name);
990 fprintf(stderr, "%s[]", name);
993 fprintf(stderr, "enum %s", name);
996 fprintf(stderr, "unsigned %s", name);
999 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1000 fprintf(stderr, "(*%s)()", name);
1004 fprintf(stderr, "What???\n");