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.
18 #include <sys/types.h>
32 #define NR_TYPE_HASH 521
35 static int DEBUG_maxchar = 1024;
39 struct en_values* next;
48 struct datatype * type;
56 struct datatype * next;
69 unsigned short bitoff;
71 struct datatype * basetype;
76 struct datatype * pointsto;
80 struct datatype * rettype;
86 struct datatype * basictype;
91 struct member * members;
95 struct en_values * members;
105 #define BASIC_LONGLONG 6
106 #define BASIC_ULONGLONGI 7
107 #define BASIC_SHORT 8
108 #define BASIC_SHORTUI 9
109 #define BASIC_SCHAR 10
110 #define BASIC_UCHAR 11
112 #define BASIC_LONG_DOUBLE 13
113 #define BASIC_DOUBLE 14
114 #define BASIC_CMPLX_INT 15
115 #define BASIC_CMPLX_FLT 16
116 #define BASIC_CMPLX_DBL 17
117 #define BASIC_CMPLX_LONG_DBL 18
118 #define BASIC_VOID 19
120 struct datatype * DEBUG_TypeInt = NULL;
121 struct datatype * DEBUG_TypeIntConst = NULL;
122 struct datatype * DEBUG_TypeUSInt = NULL;
123 struct datatype * DEBUG_TypeString = NULL;
126 * All of the types that have been defined so far.
128 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
129 static struct datatype * pointer_types = NULL;
131 static unsigned int type_hash( const char * name )
133 unsigned int hash = 0;
141 hash = (hash << 4) + *p++;
143 if( (tmp = (hash & 0xf0000000)) )
149 return hash % NR_TYPE_HASH;
153 static struct datatype *
154 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
155 char * output_format)
159 struct datatype * dt;
160 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
166 hash = type_hash(name);
175 dt->next = type_hash_table[hash];
176 type_hash_table[hash] = dt;
177 dt->un.basic.basic_type = type;
178 dt->un.basic.basic_size = size;
179 dt->un.basic.b_signed = b_signed;
180 dt->un.basic.output_format = output_format;
188 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
190 struct datatype * dt = NULL;
192 if( typename != NULL )
194 for( dt = type_hash_table[hash]; dt; dt = dt->next )
196 if( xtype != dt->type || dt->name == NULL
197 || dt->name[0] != typename[0])
202 if( strcmp(dt->name, typename) == 0 )
213 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
215 struct datatype * dt = NULL;
219 * The last bucket is special, and is used to hold typeless names.
221 if( typename == NULL )
227 hash = type_hash(typename);
230 dt = DEBUG_LookupDataType(xtype, hash, typename);
234 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
238 memset(dt, 0, sizeof(*dt));
241 if( typename != NULL )
243 dt->name = xstrdup(typename);
249 if( xtype == POINTER )
251 dt->next = pointer_types;
256 dt->next = type_hash_table[hash];
257 type_hash_table[hash] = dt;
266 DEBUG_FindOrMakePointerType(struct datatype * reftype)
268 struct datatype * dt = NULL;
270 if( reftype != NULL )
272 for( dt = pointer_types; dt; dt = dt->next )
274 if( dt->type != POINTER )
279 if( dt->un.pointer.pointsto == reftype )
288 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
293 dt->un.pointer.pointsto = reftype;
294 dt->next = pointer_types;
305 static int beenhere = 0;
306 struct datatype * chartype;
308 if( beenhere++ != 0 )
314 * Special version of int used with constants of various kinds.
316 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
319 * Initialize a few builtin types.
323 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
324 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
325 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
326 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
327 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
328 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
329 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
330 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
331 DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
332 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
333 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
334 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
335 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
336 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
337 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
338 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
339 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
340 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
341 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
343 DEBUG_TypeString = DEBUG_NewDataType(POINTER, NULL);
344 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
347 * Now initialize the builtins for codeview.
349 DEBUG_InitCVDataTypes();
354 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
357 struct datatype * type2 = NULL;
358 struct en_values * e;
359 char * def_format = "0x%x";
362 assert(addr->type != NULL);
364 switch(addr->type->type)
367 if (!DBG_CHECK_READ_PTR( addr, addr->type->un.basic.basic_size))
372 memcpy(&rtn, (char *) addr->off, addr->type->un.basic.basic_size);
373 if( (addr->type->un.basic.b_signed)
374 && ((addr->type->un.basic.basic_size & 3) != 0)
375 && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
377 rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
379 if( addr->type->un.basic.output_format != NULL )
381 def_format = addr->type->un.basic.output_format;
385 * Check for single character prints that are out of range.
387 if( addr->type->un.basic.basic_size == 1
388 && strcmp(def_format, "'%c'") == 0
389 && ((rtn < 0x20) || (rtn > 0x80)) )
395 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
396 rtn = (unsigned int) *((unsigned char **)addr->off);
397 type2 = addr->type->un.pointer.pointsto;
398 if( type2->type == BASIC && type2->un.basic.basic_size == 1 )
400 def_format = "\"%s\"";
405 def_format = "0x%8.8x";
410 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
411 rtn = (unsigned int) *((unsigned char **)addr->off);
412 def_format = "0x%8.8x";
415 rtn = (unsigned int) *((unsigned char **)addr->off);
416 for(e = addr->type->un.enumeration.members; e; e = e->next )
418 if( e->value == rtn )
441 *format = def_format;
447 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
450 * Make sure that this really makes sense.
452 if( addr->type->type != POINTER )
458 *newtype = addr->type->un.pointer.pointsto;
459 return *(unsigned int*) (addr->off);
463 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
469 * Make sure that this really makes sense.
471 if( addr->type->type != STRUCT )
477 for(m = addr->type->un.structure.members; m; m = m->next)
479 if( strcmp(m->name, ele_name) == 0 )
481 addr->type = m->type;
482 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
485 * Bitfield operation. We have to extract the field and store
486 * it in a temporary buffer so that we get it all right.
488 *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
489 addr->off = (int) tmpbuf;
491 mask = 0xffffffff << (m->size);
494 * OK, now we have the correct part of the number.
495 * Check to see whether the basic type is signed or not, and if so,
496 * we need to sign extend the number.
498 if( m->type->type == BASIC && m->type->un.basic.b_signed != 0
499 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
506 addr->off += (m->offset >> 3);
517 DEBUG_SetStructSize(struct datatype * dt, int size)
519 assert(dt->type == STRUCT);
521 if( dt->un.structure.members != NULL )
526 dt->un.structure.size = size;
527 dt->un.structure.members = NULL;
533 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
536 assert( dt->type == dt2->type && ((dt->type == STRUCT) || (dt->type == ENUM)));
538 if( dt->type == STRUCT )
540 dt->un.structure.members = dt2->un.structure.members;
544 dt->un.enumeration.members = dt2->un.enumeration.members;
551 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
552 int offset, int size)
555 struct member * last;
556 struct en_values * e;
558 if( dt->type == STRUCT )
560 for(last = dt->un.structure.members; last; last = last->next)
562 if( (last->name[0] == name[0])
563 && (strcmp(last->name, name) == 0) )
567 if( last->next == NULL )
572 m = (struct member *) xmalloc(sizeof(struct member));
578 m->name = xstrdup(name);
584 m->next = dt->un.structure.members;
585 dt->un.structure.members = m;
593 * If the base type is bitfield, then adjust the offsets here so that we
594 * are able to look things up without lots of falter-all.
596 if( type->type == BITFIELD )
598 m->offset += m->type->un.bitfield.bitoff;
599 m->size = m->type->un.bitfield.nbits;
600 m->type = m->type->un.bitfield.basetype;
603 else if( dt->type == ENUM )
605 e = (struct en_values *) xmalloc(sizeof(struct en_values));
611 e->name = xstrdup(name);
613 e->next = dt->un.enumeration.members;
614 dt->un.enumeration.members = e;
624 DEBUG_GetPointerType(struct datatype * dt)
626 if( dt->type == POINTER )
628 return dt->un.pointer.pointsto;
635 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
640 dt->un.pointer.pointsto = dt2;
643 dt->un.funct.rettype = dt2;
653 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
655 assert(dt->type == ARRAY);
656 dt->un.array.start = min;
657 dt->un.array.end = max;
658 dt->un.array.basictype = dt2;
664 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
665 struct datatype * dt2)
667 assert(dt->type == BITFIELD);
668 dt->un.bitfield.bitoff = offset;
669 dt->un.bitfield.nbits = nbits;
670 dt->un.bitfield.basetype = dt2;
675 int DEBUG_GetObjectSize(struct datatype * dt)
685 return dt->un.basic.basic_size;
687 return sizeof(int *);
689 return dt->un.structure.size;
693 return (dt->un.array.end - dt->un.array.start)
694 * DEBUG_GetObjectSize(dt->un.array.basictype);
697 * Bitfields have to be handled seperately later on
698 * when we insert the element into the structure.
710 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
715 * Make sure that this really makes sense.
717 if( addr->type->type == POINTER )
720 * Get the base type, so we know how much to index by.
722 size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
723 result->type = addr->type->un.pointer.pointsto;
724 result->off = (*(unsigned int*) (addr->off)) + size * index;
726 else if (addr->type->type == ARRAY)
728 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
729 result->type = addr->type->un.array.basictype;
730 result->off = addr->off + size * (index - addr->type->un.array.start);
736 /***********************************************************************
739 * Implementation of the 'print' command.
742 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
753 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
757 if( addr->type == NULL )
759 fprintf(stderr, "Unable to evaluate expression\n");
768 if( DEBUG_nchar > DEBUG_maxchar )
770 fprintf(stderr, "...");
774 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
776 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
780 switch(addr->type->type)
786 DEBUG_PrintBasic(addr, 1, format);
789 DEBUG_nchar += fprintf(stderr, "{");
790 for(m = addr->type->un.structure.members; m; m = m->next)
793 DEBUG_FindStructElement(&addr1, m->name,
795 DEBUG_nchar += fprintf(stderr, "%s=", m->name);
796 DEBUG_Print(&addr1, 1, format, level + 1);
797 if( m->next != NULL )
799 DEBUG_nchar += fprintf(stderr, ", ");
801 if( DEBUG_nchar > DEBUG_maxchar )
803 fprintf(stderr, "...}");
807 DEBUG_nchar += fprintf(stderr, "}");
811 * Loop over all of the entries, printing stuff as we go.
813 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
817 * Special handling for character arrays.
819 pnt = (char *) addr->off;
820 DEBUG_nchar += fprintf(stderr, "\"");
821 for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
823 fputc(*pnt++, stderr);
825 if( DEBUG_nchar > DEBUG_maxchar )
827 fprintf(stderr, "...\"");
831 DEBUG_nchar += fprintf(stderr, "\"");
835 addr1.type = addr->type->un.array.basictype;
836 DEBUG_nchar += fprintf(stderr, "{");
837 for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
839 DEBUG_Print(&addr1, 1, format, level + 1);
841 if( i == addr->type->un.array.end )
843 DEBUG_nchar += fprintf(stderr, "}");
847 DEBUG_nchar += fprintf(stderr, ", ");
849 if( DEBUG_nchar > DEBUG_maxchar )
851 fprintf(stderr, "...}");
865 DEBUG_nchar += fprintf(stderr, "\n");
873 struct datatype * dt = NULL;
880 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
882 for( dt = type_hash_table[hash]; dt; dt = dt->next )
885 if( dt->name != NULL )
892 fprintf(stderr, "0x%p - BASIC(%s)\n",
896 fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
897 dt, name, dt->un.pointer.pointsto);
900 member_name = "none";
902 if( dt->un.structure.members != NULL
903 && dt->un.structure.members->name != NULL )
905 member_name = dt->un.structure.members->name;
906 for( m = dt->un.structure.members; m; m = m->next)
911 fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
912 dt->un.structure.size, nm, member_name);
915 fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
916 dt, name, dt->un.array.basictype);
919 fprintf(stderr, "0x%p - ENUM(%s)\n",
923 fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
926 fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
927 dt, name, dt->un.funct.rettype);
931 fprintf(stderr, "What???\n");
940 enum debug_type DEBUG_GetType(struct datatype * dt)
946 DEBUG_TypeCast(enum debug_type type, const char * name)
949 struct datatype * rtn;
952 * The last bucket is special, and is used to hold typeless names.
960 hash = type_hash(name);
963 rtn = DEBUG_LookupDataType(type, hash, name);
970 DEBUG_PrintTypeCast(struct datatype * dt)
975 if( dt->name != NULL )
983 fprintf(stderr, "%s", name);
986 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
987 fprintf(stderr, "*");
990 fprintf(stderr, "struct %s", name);
993 fprintf(stderr, "%s[]", name);
996 fprintf(stderr, "enum %s", name);
999 fprintf(stderr, "unsigned %s", name);
1002 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1003 fprintf(stderr, "(*%s)()", name);
1007 fprintf(stderr, "What???\n");