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>
25 #define NR_TYPE_HASH 521
28 static int DEBUG_maxchar = 1024;
32 struct en_values* next;
41 struct datatype * type;
49 struct datatype * next;
62 unsigned short bitoff;
64 struct datatype * basetype;
69 struct datatype * pointsto;
73 struct datatype * rettype;
79 struct datatype * basictype;
84 struct member * members;
88 struct en_values * members;
98 #define BASIC_LONGLONG 6
99 #define BASIC_ULONGLONGI 7
100 #define BASIC_SHORT 8
101 #define BASIC_SHORTUI 9
102 #define BASIC_SCHAR 10
103 #define BASIC_UCHAR 11
105 #define BASIC_LONG_DOUBLE 13
106 #define BASIC_DOUBLE 14
107 #define BASIC_CMPLX_INT 15
108 #define BASIC_CMPLX_FLT 16
109 #define BASIC_CMPLX_DBL 17
110 #define BASIC_CMPLX_LONG_DBL 18
111 #define BASIC_VOID 19
113 struct datatype * DEBUG_TypeInt = NULL;
114 struct datatype * DEBUG_TypeIntConst = NULL;
115 struct datatype * DEBUG_TypeUSInt = NULL;
116 struct datatype * DEBUG_TypeString = NULL;
119 * All of the types that have been defined so far.
121 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
122 static struct datatype * pointer_types = NULL;
124 static unsigned int type_hash( const char * name )
126 unsigned int hash = 0;
134 hash = (hash << 4) + *p++;
136 if( (tmp = (hash & 0xf0000000)) )
142 return hash % NR_TYPE_HASH;
146 static struct datatype *
147 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
148 char * output_format)
152 struct datatype * dt;
153 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
159 hash = type_hash(name);
168 dt->next = type_hash_table[hash];
169 type_hash_table[hash] = dt;
170 dt->un.basic.basic_type = type;
171 dt->un.basic.basic_size = size;
172 dt->un.basic.b_signed = b_signed;
173 dt->un.basic.output_format = output_format;
181 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
183 struct datatype * dt = NULL;
185 if( typename != NULL )
187 for( dt = type_hash_table[hash]; dt; dt = dt->next )
189 if( xtype != dt->type || dt->name == NULL
190 || dt->name[0] != typename[0])
195 if( strcmp(dt->name, typename) == 0 )
206 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
208 struct datatype * dt = NULL;
212 * The last bucket is special, and is used to hold typeless names.
214 if( typename == NULL )
220 hash = type_hash(typename);
223 dt = DEBUG_LookupDataType(xtype, hash, typename);
227 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
231 memset(dt, 0, sizeof(*dt));
234 if( typename != NULL )
236 dt->name = DBG_strdup(typename);
242 if( xtype == DT_POINTER )
244 dt->next = pointer_types;
249 dt->next = type_hash_table[hash];
250 type_hash_table[hash] = dt;
259 DEBUG_FindOrMakePointerType(struct datatype * reftype)
261 struct datatype * dt = NULL;
263 if( reftype != NULL )
265 for( dt = pointer_types; dt; dt = dt->next )
267 if( dt->type != DT_POINTER )
272 if( dt->un.pointer.pointsto == reftype )
281 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
285 dt->type = DT_POINTER;
286 dt->un.pointer.pointsto = reftype;
287 dt->next = pointer_types;
296 DEBUG_InitTypes(void)
298 static int beenhere = 0;
299 struct datatype * chartype;
301 if( beenhere++ != 0 )
307 * Special version of int used with constants of various kinds.
309 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
312 * Initialize a few builtin types.
315 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
316 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
317 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
318 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
319 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
320 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
321 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
322 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
323 DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
324 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
325 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
326 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
327 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
328 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
329 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
330 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
331 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
332 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
333 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
335 DEBUG_TypeString = DEBUG_NewDataType(DT_POINTER, NULL);
336 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
339 * Now initialize the builtins for codeview.
341 DEBUG_InitCVDataTypes();
346 DEBUG_GetExprValue(const DBG_VALUE *_value, char ** format)
349 struct datatype * type2 = NULL;
350 struct en_values * e;
351 char * def_format = "0x%x";
352 DBG_VALUE value = *_value;
354 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
357 /* FIXME? I don't quite get this...
358 * if this is wrong, value.addr shall be linearized
361 assert(value.type != NULL);
363 switch(value.type->type)
368 /* FIXME: following code implies i386 byte ordering */
369 if (_value->cookie == DV_TARGET) {
370 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn,
371 value.type->un.basic.basic_size))
374 memcpy(&rtn, (void*)value.addr.off, value.type->un.basic.basic_size);
377 if( (value.type->un.basic.b_signed)
378 && ((value.type->un.basic.basic_size & 3) != 0)
379 && ((rtn >> (value.type->un.basic.basic_size * 8 - 1)) != 0) )
381 rtn = rtn | ((-1) << (value.type->un.basic.basic_size * 8));
383 if( value.type->un.basic.output_format != NULL )
385 def_format = value.type->un.basic.output_format;
389 * Check for single character prints that are out of range.
391 if( value.type->un.basic.basic_size == 1
392 && strcmp(def_format, "'%c'") == 0
393 && ((rtn < 0x20) || (rtn > 0x80)) )
399 if (_value->cookie == DV_TARGET) {
400 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn, sizeof(void*)))
403 rtn = *(unsigned int*)(value.addr.off);
406 type2 = value.type->un.pointer.pointsto;
410 def_format = "Internal symbol error: unable to access memory location 0x%08x";
415 if( type2->type == DT_BASIC && type2->un.basic.basic_size == 1 )
417 if ( _value->cookie == DV_TARGET ) {
419 def_format = "\"%S\"";
420 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn, &ch, 1))
423 def_format = "\"%s\"";
428 def_format = "0x%8.8x";
433 assert(_value->cookie == DV_TARGET);
434 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn, sizeof(rtn)))
436 def_format = "0x%8.8x";
439 assert(_value->cookie == DV_TARGET);
440 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn, sizeof(rtn)))
442 for(e = value.type->un.enumeration.members; e; e = e->next )
444 if( e->value == rtn )
467 *format = def_format;
473 DEBUG_TypeDerefPointer(const DBG_VALUE *value, struct datatype ** newtype)
475 DBG_ADDR addr = value->addr;
478 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
483 * Make sure that this really makes sense.
485 if( value->type->type != DT_POINTER )
488 if (value->cookie == DV_TARGET) {
489 if (!DEBUG_READ_MEM((void*)value->addr.off, &val, sizeof(val)))
492 val = *(unsigned int*)value->addr.off;
495 *newtype = value->type->un.pointer.pointsto;
497 return DEBUG_ToLinear(&addr); /* FIXME: is this right (or "better") ? */
501 DEBUG_FindStructElement(DBG_VALUE* value, const char * ele_name, int * tmpbuf)
506 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
509 * Make sure that this really makes sense.
511 if( value->type->type != DT_STRUCT )
517 for(m = value->type->un.structure.members; m; m = m->next)
519 if( strcmp(m->name, ele_name) == 0 )
521 value->type = m->type;
522 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
525 * Bitfield operation. We have to extract the field and store
526 * it in a temporary buffer so that we get it all right.
528 *tmpbuf = ((*(int* ) (value->addr.off + (m->offset >> 3))) >> (m->offset & 7));
529 value->addr.off = (int) tmpbuf;
531 mask = 0xffffffff << (m->size);
534 * OK, now we have the correct part of the number.
535 * Check to see whether the basic type is signed or not, and if so,
536 * we need to sign extend the number.
538 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
539 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
546 value->addr.off += (m->offset >> 3);
557 DEBUG_SetStructSize(struct datatype * dt, int size)
559 assert(dt->type == DT_STRUCT);
561 if( dt->un.structure.members != NULL )
566 dt->un.structure.size = size;
567 dt->un.structure.members = NULL;
573 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
576 assert( dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)));
578 if( dt->type == DT_STRUCT )
580 dt->un.structure.members = dt2->un.structure.members;
584 dt->un.enumeration.members = dt2->un.enumeration.members;
591 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
592 int offset, int size)
595 struct member * last;
596 struct en_values * e;
598 if( dt->type == DT_STRUCT )
600 for(last = dt->un.structure.members; last; last = last->next)
602 if( (last->name[0] == name[0])
603 && (strcmp(last->name, name) == 0) )
607 if( last->next == NULL )
612 m = (struct member *) DBG_alloc(sizeof(struct member));
618 m->name = DBG_strdup(name);
624 m->next = dt->un.structure.members;
625 dt->un.structure.members = m;
633 * If the base type is bitfield, then adjust the offsets here so that we
634 * are able to look things up without lots of falter-all.
636 if( type->type == DT_BITFIELD )
638 m->offset += m->type->un.bitfield.bitoff;
639 m->size = m->type->un.bitfield.nbits;
640 m->type = m->type->un.bitfield.basetype;
643 else if( dt->type == DT_ENUM )
645 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
651 e->name = DBG_strdup(name);
653 e->next = dt->un.enumeration.members;
654 dt->un.enumeration.members = e;
664 DEBUG_GetPointerType(struct datatype * dt)
666 if( dt->type == DT_POINTER )
668 return dt->un.pointer.pointsto;
675 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
680 dt->un.pointer.pointsto = dt2;
683 dt->un.funct.rettype = dt2;
693 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
695 assert(dt->type == DT_ARRAY);
696 dt->un.array.start = min;
697 dt->un.array.end = max;
698 dt->un.array.basictype = dt2;
704 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
705 struct datatype * dt2)
707 assert(dt->type == DT_BITFIELD);
708 dt->un.bitfield.bitoff = offset;
709 dt->un.bitfield.nbits = nbits;
710 dt->un.bitfield.basetype = dt2;
715 int DEBUG_GetObjectSize(struct datatype * dt)
725 return dt->un.basic.basic_size;
727 return sizeof(int *);
729 return dt->un.structure.size;
733 return (dt->un.array.end - dt->un.array.start)
734 * DEBUG_GetObjectSize(dt->un.array.basictype);
737 * Bitfields have to be handled seperately later on
738 * when we insert the element into the structure.
750 DEBUG_ArrayIndex(const DBG_VALUE * value, DBG_VALUE * result, int index)
754 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
757 * Make sure that this really makes sense.
759 if( value->type->type == DT_POINTER )
762 * Get the base type, so we know how much to index by.
764 size = DEBUG_GetObjectSize(value->type->un.pointer.pointsto);
765 result->type = value->type->un.pointer.pointsto;
766 result->addr.off = (*(unsigned int*) (value->addr.off)) + size * index;
768 else if (value->type->type == DT_ARRAY)
770 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
771 result->type = value->type->un.array.basictype;
772 result->addr.off = value->addr.off + size * (index - value->type->un.array.start);
778 /***********************************************************************
781 * Implementation of the 'print' command.
784 DEBUG_Print( const DBG_VALUE *value, int count, char format, int level )
793 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
797 DEBUG_Printf( DBG_CHN_MESG, "Count other than 1 is meaningless in 'print' command\n" );
801 if( value->type == NULL )
803 /* No type, just print the addr value */
804 if (value->addr.seg && (value->addr.seg != 0xffffffff))
805 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%04lx: ", value->addr.seg );
806 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", value->addr.off );
815 if( DEBUG_nchar > DEBUG_maxchar )
817 DEBUG_Printf(DBG_CHN_MESG, "...");
821 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
823 DEBUG_Printf( DBG_CHN_MESG, "Format specifier '%c' is meaningless in 'print' command\n", format );
827 switch(value->type->type)
833 DEBUG_PrintBasic(value, 1, format);
836 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
837 for(m = value->type->un.structure.members; m; m = m->next)
840 DEBUG_FindStructElement(&val1, m->name, &xval);
841 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "%s=", m->name);
842 DEBUG_Print(&val1, 1, format, level + 1);
843 if( m->next != NULL )
845 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
847 if( DEBUG_nchar > DEBUG_maxchar )
849 DEBUG_Printf(DBG_CHN_MESG, "...}");
853 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
857 * Loop over all of the entries, printing stuff as we go.
859 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
863 * Special handling for character arrays.
865 pnt = (char *) value->addr.off;
866 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
867 for( i=value->type->un.array.start; i < value->type->un.array.end; i++ )
869 fputc(*pnt++, stderr);
871 if( DEBUG_nchar > DEBUG_maxchar )
873 DEBUG_Printf(DBG_CHN_MESG, "...\"");
877 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
881 val1.type = value->type->un.array.basictype;
882 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
883 for( i=value->type->un.array.start; i <= value->type->un.array.end; i++ )
885 DEBUG_Print(&val1, 1, format, level + 1);
886 val1.addr.off += size;
887 if( i == value->type->un.array.end )
889 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
893 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
895 if( DEBUG_nchar > DEBUG_maxchar )
897 DEBUG_Printf(DBG_CHN_MESG, "...}");
911 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\n");
917 DEBUG_DumpTypes(void)
919 struct datatype * dt = NULL;
926 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
928 for( dt = type_hash_table[hash]; dt; dt = dt->next )
931 if( dt->name != NULL )
938 DEBUG_Printf(DBG_CHN_MESG, "0x%p - BASIC(%s)\n",
942 DEBUG_Printf(DBG_CHN_MESG, "0x%p - POINTER(%s)(%p)\n",
943 dt, name, dt->un.pointer.pointsto);
946 member_name = "none";
948 if( dt->un.structure.members != NULL
949 && dt->un.structure.members->name != NULL )
951 member_name = dt->un.structure.members->name;
952 for( m = dt->un.structure.members; m; m = m->next)
957 DEBUG_Printf(DBG_CHN_MESG, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
958 dt->un.structure.size, nm, member_name);
961 DEBUG_Printf(DBG_CHN_MESG, "0x%p - ARRAY(%s)(%p)\n",
962 dt, name, dt->un.array.basictype);
965 DEBUG_Printf(DBG_CHN_MESG, "0x%p - ENUM(%s)\n", dt, name);
968 DEBUG_Printf(DBG_CHN_MESG, "0x%p - BITFIELD(%s)\n", dt, name);
971 DEBUG_Printf(DBG_CHN_MESG, "0x%p - FUNC(%s)(%p)\n",
972 dt, name, dt->un.funct.rettype);
976 DEBUG_Printf(DBG_CHN_MESG, "What???\n");
985 enum debug_type DEBUG_GetType(struct datatype * dt)
991 DEBUG_TypeCast(enum debug_type type, const char * name)
994 struct datatype * rtn;
997 * The last bucket is special, and is used to hold typeless names.
1001 hash = NR_TYPE_HASH;
1005 hash = type_hash(name);
1008 rtn = DEBUG_LookupDataType(type, hash, name);
1015 DEBUG_PrintTypeCast(const struct datatype * dt)
1017 const char* name = "none";
1019 if( dt->name != NULL )
1027 DEBUG_Printf(DBG_CHN_MESG, "%s", name);
1030 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
1031 DEBUG_Printf(DBG_CHN_MESG, "*");
1034 DEBUG_Printf(DBG_CHN_MESG, "struct %s", name);
1037 DEBUG_Printf(DBG_CHN_MESG, "%s[]", name);
1040 DEBUG_Printf(DBG_CHN_MESG, "enum %s", name);
1043 DEBUG_Printf(DBG_CHN_MESG, "unsigned %s", name);
1046 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1047 DEBUG_Printf(DBG_CHN_MESG, "(*%s)()", name);
1051 DEBUG_Printf(DBG_CHN_MESG, "What???\n");
1058 int DEBUG_PrintType( const DBG_VALUE *value )
1060 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
1064 DEBUG_Printf(DBG_CHN_MESG, "Unknown type\n");
1067 if (!DEBUG_PrintTypeCast(value->type))
1069 DEBUG_Printf(DBG_CHN_MESG, "\n");