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.
16 #include <sys/types.h>
26 #define NR_TYPE_HASH 521
29 static int DEBUG_maxchar = 1024;
33 struct en_values* next;
42 struct datatype * type;
50 struct datatype * next;
63 unsigned short bitoff;
65 struct datatype * basetype;
70 struct datatype * pointsto;
74 struct datatype * rettype;
80 struct datatype * basictype;
85 struct member * members;
89 struct en_values * members;
99 #define BASIC_LONGLONG 6
100 #define BASIC_ULONGLONGI 7
101 #define BASIC_SHORT 8
102 #define BASIC_SHORTUI 9
103 #define BASIC_SCHAR 10
104 #define BASIC_UCHAR 11
106 #define BASIC_LONG_DOUBLE 13
107 #define BASIC_DOUBLE 14
108 #define BASIC_CMPLX_INT 15
109 #define BASIC_CMPLX_FLT 16
110 #define BASIC_CMPLX_DBL 17
111 #define BASIC_CMPLX_LONG_DBL 18
112 #define BASIC_VOID 19
114 struct datatype * DEBUG_TypeInt = NULL;
115 struct datatype * DEBUG_TypeIntConst = NULL;
116 struct datatype * DEBUG_TypeUSInt = NULL;
117 struct datatype * DEBUG_TypeString = NULL;
120 * All of the types that have been defined so far.
122 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
123 static struct datatype * pointer_types = NULL;
125 static unsigned int type_hash( const char * name )
127 unsigned int hash = 0;
135 hash = (hash << 4) + *p++;
137 if( (tmp = (hash & 0xf0000000)) )
143 return hash % NR_TYPE_HASH;
147 static struct datatype *
148 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
149 char * output_format)
153 struct datatype * dt;
154 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
160 hash = type_hash(name);
169 dt->next = type_hash_table[hash];
170 type_hash_table[hash] = dt;
171 dt->un.basic.basic_type = type;
172 dt->un.basic.basic_size = size;
173 dt->un.basic.b_signed = b_signed;
174 dt->un.basic.output_format = output_format;
182 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
184 struct datatype * dt = NULL;
186 if( typename != NULL )
188 for( dt = type_hash_table[hash]; dt; dt = dt->next )
190 if( xtype != dt->type || dt->name == NULL
191 || dt->name[0] != typename[0])
196 if( strcmp(dt->name, typename) == 0 )
207 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
209 struct datatype * dt = NULL;
213 * The last bucket is special, and is used to hold typeless names.
215 if( typename == NULL )
221 hash = type_hash(typename);
224 dt = DEBUG_LookupDataType(xtype, hash, typename);
228 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
232 memset(dt, 0, sizeof(*dt));
235 if( typename != NULL )
237 dt->name = DBG_strdup(typename);
243 if( xtype == DT_POINTER )
245 dt->next = pointer_types;
250 dt->next = type_hash_table[hash];
251 type_hash_table[hash] = dt;
260 DEBUG_FindOrMakePointerType(struct datatype * reftype)
262 struct datatype * dt = NULL;
264 if( reftype != NULL )
266 for( dt = pointer_types; dt; dt = dt->next )
268 if( dt->type != DT_POINTER )
273 if( dt->un.pointer.pointsto == reftype )
282 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
286 dt->type = DT_POINTER;
287 dt->un.pointer.pointsto = reftype;
288 dt->next = pointer_types;
299 static int beenhere = 0;
300 struct datatype * chartype;
302 if( beenhere++ != 0 )
308 * Special version of int used with constants of various kinds.
310 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
313 * Initialize a few builtin types.
317 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
318 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
319 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
320 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
321 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
322 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
323 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
324 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
325 DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
326 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
327 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
328 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
329 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
330 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
331 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
332 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
333 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
334 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
335 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
337 DEBUG_TypeString = DEBUG_NewDataType(DT_POINTER, NULL);
338 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
341 * Now initialize the builtins for codeview.
343 DEBUG_InitCVDataTypes();
348 DEBUG_GetExprValue(const DBG_ADDR * addr, char ** format)
350 DBG_ADDR address = *addr;
352 struct datatype * type2 = NULL;
353 struct en_values * e;
354 char * def_format = "0x%x";
357 address.seg = 0; /* FIXME? I don't quite get this... */
358 assert(addr->type != NULL);
360 switch(addr->type->type)
364 if (!DEBUG_READ_MEM_VERBOSE((void*)addr->off, &rtn, addr->type->un.basic.basic_size))
367 if( (addr->type->un.basic.b_signed)
368 && ((addr->type->un.basic.basic_size & 3) != 0)
369 && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
371 rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
373 if( addr->type->un.basic.output_format != NULL )
375 def_format = addr->type->un.basic.output_format;
379 * Check for single character prints that are out of range.
381 if( addr->type->un.basic.basic_size == 1
382 && strcmp(def_format, "'%c'") == 0
383 && ((rtn < 0x20) || (rtn > 0x80)) )
389 if (!DEBUG_READ_MEM_VERBOSE((void*)addr->off, &rtn, sizeof(void*)))
392 type2 = addr->type->un.pointer.pointsto;
396 def_format = "Internal symbol error: unable to access memory location 0x%08x";
401 if( type2->type == DT_BASIC && type2->un.basic.basic_size == 1 )
403 def_format = "\"%s\"";
404 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn, &rtn, 1))
410 def_format = "0x%8.8x";
415 if (!DEBUG_READ_MEM_VERBOSE((void*)addr->off, &rtn, sizeof(rtn)))
417 def_format = "0x%8.8x";
420 if (!DEBUG_READ_MEM_VERBOSE((void*)addr->off, &rtn, sizeof(rtn)))
422 for(e = addr->type->un.enumeration.members; e; e = e->next )
424 if( e->value == rtn )
447 *format = def_format;
453 DEBUG_TypeDerefPointer(const DBG_ADDR * addr, struct datatype ** newtype)
455 DBG_ADDR address = *addr;
459 * Make sure that this really makes sense.
461 if( addr->type->type != DT_POINTER || !DEBUG_READ_MEM((void*)addr->off, &val, sizeof(val)))
467 *newtype = addr->type->un.pointer.pointsto;
469 return DEBUG_ToLinear(&address); /* FIXME: is this right (or "better") ? */
473 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
479 * Make sure that this really makes sense.
481 if( addr->type->type != DT_STRUCT )
487 for(m = addr->type->un.structure.members; m; m = m->next)
489 if( strcmp(m->name, ele_name) == 0 )
491 addr->type = m->type;
492 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
495 * Bitfield operation. We have to extract the field and store
496 * it in a temporary buffer so that we get it all right.
498 *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
499 addr->off = (int) tmpbuf;
501 mask = 0xffffffff << (m->size);
504 * OK, now we have the correct part of the number.
505 * Check to see whether the basic type is signed or not, and if so,
506 * we need to sign extend the number.
508 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
509 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
516 addr->off += (m->offset >> 3);
527 DEBUG_SetStructSize(struct datatype * dt, int size)
529 assert(dt->type == DT_STRUCT);
531 if( dt->un.structure.members != NULL )
536 dt->un.structure.size = size;
537 dt->un.structure.members = NULL;
543 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
546 assert( dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)));
548 if( dt->type == DT_STRUCT )
550 dt->un.structure.members = dt2->un.structure.members;
554 dt->un.enumeration.members = dt2->un.enumeration.members;
561 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
562 int offset, int size)
565 struct member * last;
566 struct en_values * e;
568 if( dt->type == DT_STRUCT )
570 for(last = dt->un.structure.members; last; last = last->next)
572 if( (last->name[0] == name[0])
573 && (strcmp(last->name, name) == 0) )
577 if( last->next == NULL )
582 m = (struct member *) DBG_alloc(sizeof(struct member));
588 m->name = DBG_strdup(name);
594 m->next = dt->un.structure.members;
595 dt->un.structure.members = m;
603 * If the base type is bitfield, then adjust the offsets here so that we
604 * are able to look things up without lots of falter-all.
606 if( type->type == DT_BITFIELD )
608 m->offset += m->type->un.bitfield.bitoff;
609 m->size = m->type->un.bitfield.nbits;
610 m->type = m->type->un.bitfield.basetype;
613 else if( dt->type == DT_ENUM )
615 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
621 e->name = DBG_strdup(name);
623 e->next = dt->un.enumeration.members;
624 dt->un.enumeration.members = e;
634 DEBUG_GetPointerType(struct datatype * dt)
636 if( dt->type == DT_POINTER )
638 return dt->un.pointer.pointsto;
645 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
650 dt->un.pointer.pointsto = dt2;
653 dt->un.funct.rettype = dt2;
663 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
665 assert(dt->type == DT_ARRAY);
666 dt->un.array.start = min;
667 dt->un.array.end = max;
668 dt->un.array.basictype = dt2;
674 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
675 struct datatype * dt2)
677 assert(dt->type == DT_BITFIELD);
678 dt->un.bitfield.bitoff = offset;
679 dt->un.bitfield.nbits = nbits;
680 dt->un.bitfield.basetype = dt2;
685 int DEBUG_GetObjectSize(struct datatype * dt)
695 return dt->un.basic.basic_size;
697 return sizeof(int *);
699 return dt->un.structure.size;
703 return (dt->un.array.end - dt->un.array.start)
704 * DEBUG_GetObjectSize(dt->un.array.basictype);
707 * Bitfields have to be handled seperately later on
708 * when we insert the element into the structure.
720 DEBUG_ArrayIndex(const DBG_ADDR * addr, DBG_ADDR * result, int index)
725 * Make sure that this really makes sense.
727 if( addr->type->type == DT_POINTER )
730 * Get the base type, so we know how much to index by.
732 size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
733 result->type = addr->type->un.pointer.pointsto;
734 result->off = (*(unsigned int*) (addr->off)) + size * index;
736 else if (addr->type->type == DT_ARRAY)
738 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
739 result->type = addr->type->un.array.basictype;
740 result->off = addr->off + size * (index - addr->type->un.array.start);
746 /***********************************************************************
749 * Implementation of the 'print' command.
752 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
763 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
767 if( addr->type == NULL )
769 /* No type, just print the addr value */
770 if (addr->seg && (addr->seg != 0xffffffff))
771 DEBUG_nchar += fprintf( stderr, "0x%04lx: ", addr->seg );
772 DEBUG_nchar += fprintf( stderr, "0x%08lx", addr->off );
781 if( DEBUG_nchar > DEBUG_maxchar )
783 fprintf(stderr, "...");
787 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
789 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
793 switch(addr->type->type)
799 DEBUG_PrintBasic(addr, 1, format);
802 DEBUG_nchar += fprintf(stderr, "{");
803 for(m = addr->type->un.structure.members; m; m = m->next)
806 DEBUG_FindStructElement(&addr1, m->name,
808 DEBUG_nchar += fprintf(stderr, "%s=", m->name);
809 DEBUG_Print(&addr1, 1, format, level + 1);
810 if( m->next != NULL )
812 DEBUG_nchar += fprintf(stderr, ", ");
814 if( DEBUG_nchar > DEBUG_maxchar )
816 fprintf(stderr, "...}");
820 DEBUG_nchar += fprintf(stderr, "}");
824 * Loop over all of the entries, printing stuff as we go.
826 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
830 * Special handling for character arrays.
832 pnt = (char *) addr->off;
833 DEBUG_nchar += fprintf(stderr, "\"");
834 for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
836 fputc(*pnt++, stderr);
838 if( DEBUG_nchar > DEBUG_maxchar )
840 fprintf(stderr, "...\"");
844 DEBUG_nchar += fprintf(stderr, "\"");
848 addr1.type = addr->type->un.array.basictype;
849 DEBUG_nchar += fprintf(stderr, "{");
850 for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
852 DEBUG_Print(&addr1, 1, format, level + 1);
854 if( i == addr->type->un.array.end )
856 DEBUG_nchar += fprintf(stderr, "}");
860 DEBUG_nchar += fprintf(stderr, ", ");
862 if( DEBUG_nchar > DEBUG_maxchar )
864 fprintf(stderr, "...}");
878 DEBUG_nchar += fprintf(stderr, "\n");
886 struct datatype * dt = NULL;
893 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
895 for( dt = type_hash_table[hash]; dt; dt = dt->next )
898 if( dt->name != NULL )
905 fprintf(stderr, "0x%p - BASIC(%s)\n",
909 fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
910 dt, name, dt->un.pointer.pointsto);
913 member_name = "none";
915 if( dt->un.structure.members != NULL
916 && dt->un.structure.members->name != NULL )
918 member_name = dt->un.structure.members->name;
919 for( m = dt->un.structure.members; m; m = m->next)
924 fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
925 dt->un.structure.size, nm, member_name);
928 fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
929 dt, name, dt->un.array.basictype);
932 fprintf(stderr, "0x%p - ENUM(%s)\n",
936 fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
939 fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
940 dt, name, dt->un.funct.rettype);
944 fprintf(stderr, "What???\n");
953 enum debug_type DEBUG_GetType(struct datatype * dt)
959 DEBUG_TypeCast(enum debug_type type, const char * name)
962 struct datatype * rtn;
965 * The last bucket is special, and is used to hold typeless names.
973 hash = type_hash(name);
976 rtn = DEBUG_LookupDataType(type, hash, name);
983 DEBUG_PrintTypeCast(struct datatype * dt)
988 if( dt->name != NULL )
996 fprintf(stderr, "%s", name);
999 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
1000 fprintf(stderr, "*");
1003 fprintf(stderr, "struct %s", name);
1006 fprintf(stderr, "%s[]", name);
1009 fprintf(stderr, "enum %s", name);
1012 fprintf(stderr, "unsigned %s", name);
1015 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1016 fprintf(stderr, "(*%s)()", name);
1020 fprintf(stderr, "What???\n");