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>
27 #define NR_TYPE_HASH 521
30 static int DEBUG_maxchar = 1024;
34 struct en_values* next;
43 struct datatype * type;
51 struct datatype * next;
64 unsigned short bitoff;
66 struct datatype * basetype;
71 struct datatype * pointsto;
75 struct datatype * rettype;
81 struct datatype * basictype;
86 struct member * members;
90 struct en_values * members;
100 #define BASIC_LONGLONG 6
101 #define BASIC_ULONGLONGI 7
102 #define BASIC_SHORT 8
103 #define BASIC_SHORTUI 9
104 #define BASIC_SCHAR 10
105 #define BASIC_UCHAR 11
107 #define BASIC_LONG_DOUBLE 13
108 #define BASIC_DOUBLE 14
109 #define BASIC_CMPLX_INT 15
110 #define BASIC_CMPLX_FLT 16
111 #define BASIC_CMPLX_DBL 17
112 #define BASIC_CMPLX_LONG_DBL 18
113 #define BASIC_VOID 19
115 struct datatype * DEBUG_TypeInt = NULL;
116 struct datatype * DEBUG_TypeIntConst = NULL;
117 struct datatype * DEBUG_TypeUSInt = NULL;
118 struct datatype * DEBUG_TypeString = NULL;
121 * All of the types that have been defined so far.
123 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
124 static struct datatype * pointer_types = NULL;
126 static unsigned int type_hash( const char * name )
128 unsigned int hash = 0;
136 hash = (hash << 4) + *p++;
138 if( (tmp = (hash & 0xf0000000)) )
144 return hash % NR_TYPE_HASH;
148 static struct datatype *
149 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
150 char * output_format)
154 struct datatype * dt;
155 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
161 hash = type_hash(name);
170 dt->next = type_hash_table[hash];
171 type_hash_table[hash] = dt;
172 dt->un.basic.basic_type = type;
173 dt->un.basic.basic_size = size;
174 dt->un.basic.b_signed = b_signed;
175 dt->un.basic.output_format = output_format;
183 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
185 struct datatype * dt = NULL;
187 if( typename != NULL )
189 for( dt = type_hash_table[hash]; dt; dt = dt->next )
191 if( xtype != dt->type || dt->name == NULL
192 || dt->name[0] != typename[0])
197 if( strcmp(dt->name, typename) == 0 )
208 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
210 struct datatype * dt = NULL;
214 * The last bucket is special, and is used to hold typeless names.
216 if( typename == NULL )
222 hash = type_hash(typename);
225 dt = DEBUG_LookupDataType(xtype, hash, typename);
229 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
233 memset(dt, 0, sizeof(*dt));
236 if( typename != NULL )
238 dt->name = DBG_strdup(typename);
244 if( xtype == DT_POINTER )
246 dt->next = pointer_types;
251 dt->next = type_hash_table[hash];
252 type_hash_table[hash] = dt;
261 DEBUG_FindOrMakePointerType(struct datatype * reftype)
263 struct datatype * dt = NULL;
265 if( reftype != NULL )
267 for( dt = pointer_types; dt; dt = dt->next )
269 if( dt->type != DT_POINTER )
274 if( dt->un.pointer.pointsto == reftype )
283 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
287 dt->type = DT_POINTER;
288 dt->un.pointer.pointsto = reftype;
289 dt->next = pointer_types;
300 static int beenhere = 0;
301 struct datatype * chartype;
303 if( beenhere++ != 0 )
309 * Special version of int used with constants of various kinds.
311 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
314 * Initialize a few builtin types.
318 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
319 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
320 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
321 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
322 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
323 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
324 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
325 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
326 DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
327 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
328 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
329 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
330 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
331 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
332 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
333 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
334 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
335 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
336 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
338 DEBUG_TypeString = DEBUG_NewDataType(DT_POINTER, NULL);
339 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
342 * Now initialize the builtins for codeview.
344 DEBUG_InitCVDataTypes();
349 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
351 DBG_ADDR address = *addr;
353 struct datatype * type2 = NULL;
354 struct en_values * e;
355 char * def_format = "0x%x";
358 address.seg = 0; /* FIXME? I don't quite get this... */
359 assert(addr->type != NULL);
361 switch(addr->type->type)
364 if (!DBG_CHECK_READ_PTR( &address, 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( &address, 1 )) return 0;
393 rtn = (unsigned int) *((unsigned char **)addr->off);
394 type2 = addr->type->un.pointer.pointsto;
395 if( type2->type == DT_BASIC && type2->un.basic.basic_size == 1 )
397 def_format = "\"%s\"";
402 def_format = "0x%8.8x";
407 if (!DBG_CHECK_READ_PTR( &address, 1 )) return 0;
408 rtn = (unsigned int) *((unsigned char **)addr->off);
409 def_format = "0x%8.8x";
412 if (!DBG_CHECK_READ_PTR( &address, 1 )) return 0;
413 rtn = (unsigned int) *((unsigned char **)addr->off);
414 for(e = addr->type->un.enumeration.members; e; e = e->next )
416 if( e->value == rtn )
439 *format = def_format;
445 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
447 DBG_ADDR address = *addr;
450 * Make sure that this really makes sense.
452 if( addr->type->type != DT_POINTER )
458 *newtype = addr->type->un.pointer.pointsto;
459 address.off = *(unsigned int*) (addr->off);
460 return (unsigned int)DBG_ADDR_TO_LIN(&address); /* FIXME: is this right (or "better") ? */
464 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
470 * Make sure that this really makes sense.
472 if( addr->type->type != DT_STRUCT )
478 for(m = addr->type->un.structure.members; m; m = m->next)
480 if( strcmp(m->name, ele_name) == 0 )
482 addr->type = m->type;
483 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
486 * Bitfield operation. We have to extract the field and store
487 * it in a temporary buffer so that we get it all right.
489 *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
490 addr->off = (int) tmpbuf;
492 mask = 0xffffffff << (m->size);
495 * OK, now we have the correct part of the number.
496 * Check to see whether the basic type is signed or not, and if so,
497 * we need to sign extend the number.
499 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
500 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
507 addr->off += (m->offset >> 3);
518 DEBUG_SetStructSize(struct datatype * dt, int size)
520 assert(dt->type == DT_STRUCT);
522 if( dt->un.structure.members != NULL )
527 dt->un.structure.size = size;
528 dt->un.structure.members = NULL;
534 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
537 assert( dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)));
539 if( dt->type == DT_STRUCT )
541 dt->un.structure.members = dt2->un.structure.members;
545 dt->un.enumeration.members = dt2->un.enumeration.members;
552 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
553 int offset, int size)
556 struct member * last;
557 struct en_values * e;
559 if( dt->type == DT_STRUCT )
561 for(last = dt->un.structure.members; last; last = last->next)
563 if( (last->name[0] == name[0])
564 && (strcmp(last->name, name) == 0) )
568 if( last->next == NULL )
573 m = (struct member *) DBG_alloc(sizeof(struct member));
579 m->name = DBG_strdup(name);
585 m->next = dt->un.structure.members;
586 dt->un.structure.members = m;
594 * If the base type is bitfield, then adjust the offsets here so that we
595 * are able to look things up without lots of falter-all.
597 if( type->type == DT_BITFIELD )
599 m->offset += m->type->un.bitfield.bitoff;
600 m->size = m->type->un.bitfield.nbits;
601 m->type = m->type->un.bitfield.basetype;
604 else if( dt->type == DT_ENUM )
606 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
612 e->name = DBG_strdup(name);
614 e->next = dt->un.enumeration.members;
615 dt->un.enumeration.members = e;
625 DEBUG_GetPointerType(struct datatype * dt)
627 if( dt->type == DT_POINTER )
629 return dt->un.pointer.pointsto;
636 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
641 dt->un.pointer.pointsto = dt2;
644 dt->un.funct.rettype = dt2;
654 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
656 assert(dt->type == DT_ARRAY);
657 dt->un.array.start = min;
658 dt->un.array.end = max;
659 dt->un.array.basictype = dt2;
665 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
666 struct datatype * dt2)
668 assert(dt->type == DT_BITFIELD);
669 dt->un.bitfield.bitoff = offset;
670 dt->un.bitfield.nbits = nbits;
671 dt->un.bitfield.basetype = dt2;
676 int DEBUG_GetObjectSize(struct datatype * dt)
686 return dt->un.basic.basic_size;
688 return sizeof(int *);
690 return dt->un.structure.size;
694 return (dt->un.array.end - dt->un.array.start)
695 * DEBUG_GetObjectSize(dt->un.array.basictype);
698 * Bitfields have to be handled seperately later on
699 * when we insert the element into the structure.
711 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
716 * Make sure that this really makes sense.
718 if( addr->type->type == DT_POINTER )
721 * Get the base type, so we know how much to index by.
723 size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
724 result->type = addr->type->un.pointer.pointsto;
725 result->off = (*(unsigned int*) (addr->off)) + size * index;
727 else if (addr->type->type == DT_ARRAY)
729 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
730 result->type = addr->type->un.array.basictype;
731 result->off = addr->off + size * (index - addr->type->un.array.start);
737 /***********************************************************************
740 * Implementation of the 'print' command.
743 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
754 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
758 if( addr->type == NULL )
760 /* No type, just print the addr value */
761 if (addr->seg && (addr->seg != 0xffffffff))
762 DEBUG_nchar += fprintf( stderr, "0x%04lx: ", addr->seg );
763 DEBUG_nchar += fprintf( stderr, "0x%08lx", addr->off );
772 if( DEBUG_nchar > DEBUG_maxchar )
774 fprintf(stderr, "...");
778 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
780 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
784 switch(addr->type->type)
790 DEBUG_PrintBasic(addr, 1, format);
793 DEBUG_nchar += fprintf(stderr, "{");
794 for(m = addr->type->un.structure.members; m; m = m->next)
797 DEBUG_FindStructElement(&addr1, m->name,
799 DEBUG_nchar += fprintf(stderr, "%s=", m->name);
800 DEBUG_Print(&addr1, 1, format, level + 1);
801 if( m->next != NULL )
803 DEBUG_nchar += fprintf(stderr, ", ");
805 if( DEBUG_nchar > DEBUG_maxchar )
807 fprintf(stderr, "...}");
811 DEBUG_nchar += fprintf(stderr, "}");
815 * Loop over all of the entries, printing stuff as we go.
817 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
821 * Special handling for character arrays.
823 pnt = (char *) addr->off;
824 DEBUG_nchar += fprintf(stderr, "\"");
825 for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
827 fputc(*pnt++, stderr);
829 if( DEBUG_nchar > DEBUG_maxchar )
831 fprintf(stderr, "...\"");
835 DEBUG_nchar += fprintf(stderr, "\"");
839 addr1.type = addr->type->un.array.basictype;
840 DEBUG_nchar += fprintf(stderr, "{");
841 for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
843 DEBUG_Print(&addr1, 1, format, level + 1);
845 if( i == addr->type->un.array.end )
847 DEBUG_nchar += fprintf(stderr, "}");
851 DEBUG_nchar += fprintf(stderr, ", ");
853 if( DEBUG_nchar > DEBUG_maxchar )
855 fprintf(stderr, "...}");
869 DEBUG_nchar += fprintf(stderr, "\n");
877 struct datatype * dt = NULL;
884 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
886 for( dt = type_hash_table[hash]; dt; dt = dt->next )
889 if( dt->name != NULL )
896 fprintf(stderr, "0x%p - BASIC(%s)\n",
900 fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
901 dt, name, dt->un.pointer.pointsto);
904 member_name = "none";
906 if( dt->un.structure.members != NULL
907 && dt->un.structure.members->name != NULL )
909 member_name = dt->un.structure.members->name;
910 for( m = dt->un.structure.members; m; m = m->next)
915 fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
916 dt->un.structure.size, nm, member_name);
919 fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
920 dt, name, dt->un.array.basictype);
923 fprintf(stderr, "0x%p - ENUM(%s)\n",
927 fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
930 fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
931 dt, name, dt->un.funct.rettype);
935 fprintf(stderr, "What???\n");
944 enum debug_type DEBUG_GetType(struct datatype * dt)
950 DEBUG_TypeCast(enum debug_type type, const char * name)
953 struct datatype * rtn;
956 * The last bucket is special, and is used to hold typeless names.
964 hash = type_hash(name);
967 rtn = DEBUG_LookupDataType(type, hash, name);
974 DEBUG_PrintTypeCast(struct datatype * dt)
979 if( dt->name != NULL )
987 fprintf(stderr, "%s", name);
990 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
991 fprintf(stderr, "*");
994 fprintf(stderr, "struct %s", name);
997 fprintf(stderr, "%s[]", name);
1000 fprintf(stderr, "enum %s", name);
1003 fprintf(stderr, "unsigned %s", name);
1006 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1007 fprintf(stderr, "(*%s)()", name);
1011 fprintf(stderr, "What???\n");