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.
14 #include <sys/types.h>
23 #define NR_TYPE_HASH 521
26 static int DEBUG_maxchar = 1024;
30 struct en_values* next;
39 struct datatype * type;
47 struct datatype * next;
60 unsigned short bitoff;
62 struct datatype * basetype;
67 struct datatype * pointsto;
71 struct datatype * rettype;
77 struct datatype * basictype;
82 struct member * members;
86 struct en_values * members;
96 #define BASIC_LONGLONG 6
97 #define BASIC_ULONGLONGI 7
99 #define BASIC_SHORTUI 9
100 #define BASIC_SCHAR 10
101 #define BASIC_UCHAR 11
103 #define BASIC_LONG_DOUBLE 13
104 #define BASIC_DOUBLE 14
105 #define BASIC_CMPLX_INT 15
106 #define BASIC_CMPLX_FLT 16
107 #define BASIC_CMPLX_DBL 17
108 #define BASIC_CMPLX_LONG_DBL 18
109 #define BASIC_VOID 19
111 struct datatype * DEBUG_TypeInt = NULL;
112 struct datatype * DEBUG_TypeIntConst = NULL;
113 struct datatype * DEBUG_TypeUSInt = NULL;
114 struct datatype * DEBUG_TypeString = NULL;
115 struct datatype * DEBUG_TypeShortUInt = NULL;
117 * All of the types that have been defined so far.
119 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
120 static struct datatype * pointer_types = NULL;
122 static unsigned int type_hash( const char * name )
124 unsigned int hash = 0;
132 hash = (hash << 4) + *p++;
134 if( (tmp = (hash & 0xf0000000)) )
140 return hash % NR_TYPE_HASH;
144 static struct datatype *
145 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
146 char * output_format)
150 struct datatype * dt;
151 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
157 hash = type_hash(name);
166 dt->next = type_hash_table[hash];
167 type_hash_table[hash] = dt;
168 dt->un.basic.basic_type = type;
169 dt->un.basic.basic_size = size;
170 dt->un.basic.b_signed = b_signed;
171 dt->un.basic.output_format = output_format;
179 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
181 struct datatype * dt = NULL;
183 if( typename != NULL )
185 for( dt = type_hash_table[hash]; dt; dt = dt->next )
187 if( xtype != dt->type || dt->name == NULL
188 || dt->name[0] != typename[0])
193 if( strcmp(dt->name, typename) == 0 )
204 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
206 struct datatype * dt = NULL;
210 * The last bucket is special, and is used to hold typeless names.
212 if( typename == NULL )
218 hash = type_hash(typename);
221 dt = DEBUG_LookupDataType(xtype, hash, typename);
225 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
229 memset(dt, 0, sizeof(*dt));
232 if( typename != NULL )
234 dt->name = DBG_strdup(typename);
240 if( xtype == DT_POINTER )
242 dt->next = pointer_types;
247 dt->next = type_hash_table[hash];
248 type_hash_table[hash] = dt;
257 DEBUG_FindOrMakePointerType(struct datatype * reftype)
259 struct datatype * dt = NULL;
261 if( reftype != NULL )
263 for( dt = pointer_types; dt; dt = dt->next )
265 if( dt->type != DT_POINTER )
270 if( dt->un.pointer.pointsto == reftype )
279 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
283 dt->type = DT_POINTER;
284 dt->un.pointer.pointsto = reftype;
285 dt->next = pointer_types;
294 DEBUG_InitTypes(void)
296 static int beenhere = 0;
297 struct datatype * chartype;
299 if( beenhere++ != 0 )
305 * Special version of int used with constants of various kinds.
307 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
310 * Initialize a few builtin types.
313 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
314 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
315 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
316 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
317 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
318 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
319 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
320 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
321 DEBUG_TypeShortUInt = DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
322 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
323 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
324 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
325 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
326 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
327 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
328 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
329 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
330 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
331 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
333 DEBUG_TypeString = DEBUG_NewDataType(DT_POINTER, NULL);
334 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
337 * Now initialize the builtins for codeview.
339 DEBUG_InitCVDataTypes();
344 DEBUG_GetExprValue(const DBG_VALUE *_value, char ** format)
347 struct datatype * type2 = NULL;
348 struct en_values * e;
349 char * def_format = "0x%x";
350 DBG_VALUE value = *_value;
352 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
355 /* FIXME? I don't quite get this...
356 * if this is wrong, value.addr shall be linearized
359 assert(value.type != NULL);
361 switch(value.type->type)
366 /* FIXME: following code implies i386 byte ordering */
367 if (_value->cookie == DV_TARGET) {
368 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn,
369 value.type->un.basic.basic_size))
372 memcpy(&rtn, (void*)value.addr.off, value.type->un.basic.basic_size);
375 if( (value.type->un.basic.b_signed)
376 && ((value.type->un.basic.basic_size & 3) != 0)
377 && ((rtn >> (value.type->un.basic.basic_size * 8 - 1)) != 0) )
379 rtn = rtn | ((-1) << (value.type->un.basic.basic_size * 8));
381 if( value.type->un.basic.output_format != NULL )
383 def_format = value.type->un.basic.output_format;
387 * Check for single character prints that are out of range.
389 if( value.type->un.basic.basic_size == 1
390 && strcmp(def_format, "'%c'") == 0
391 && ((rtn < 0x20) || (rtn > 0x80)) )
397 if (_value->cookie == DV_TARGET) {
398 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn, sizeof(void*)))
401 rtn = *(unsigned int*)(value.addr.off);
404 type2 = value.type->un.pointer.pointsto;
408 def_format = "Internal symbol error: unable to access memory location 0x%08x";
413 if( type2->type == DT_BASIC && type2->un.basic.basic_size == 1 )
415 if ( _value->cookie == DV_TARGET ) {
417 def_format = "\"%S\"";
418 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn, &ch, 1))
421 def_format = "\"%s\"";
426 def_format = "0x%8.8x";
431 assert(_value->cookie == DV_TARGET);
432 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn, sizeof(rtn)))
434 def_format = "0x%8.8x";
437 assert(_value->cookie == DV_TARGET);
438 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn, sizeof(rtn)))
440 for(e = value.type->un.enumeration.members; e; e = e->next )
442 if( e->value == rtn )
465 *format = def_format;
471 DEBUG_TypeDerefPointer(const DBG_VALUE *value, struct datatype ** newtype)
473 DBG_ADDR addr = value->addr;
476 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
481 * Make sure that this really makes sense.
483 if( value->type->type != DT_POINTER )
486 if (value->cookie == DV_TARGET) {
487 if (!DEBUG_READ_MEM((void*)value->addr.off, &val, sizeof(val)))
490 val = *(unsigned int*)value->addr.off;
493 *newtype = value->type->un.pointer.pointsto;
495 return DEBUG_ToLinear(&addr); /* FIXME: is this right (or "better") ? */
499 DEBUG_FindStructElement(DBG_VALUE* value, const char * ele_name, int * tmpbuf)
504 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
507 * Make sure that this really makes sense.
509 if( value->type->type != DT_STRUCT )
515 for(m = value->type->un.structure.members; m; m = m->next)
517 if( strcmp(m->name, ele_name) == 0 )
519 value->type = m->type;
520 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
523 * Bitfield operation. We have to extract the field and store
524 * it in a temporary buffer so that we get it all right.
526 *tmpbuf = ((*(int* ) (value->addr.off + (m->offset >> 3))) >> (m->offset & 7));
527 value->addr.off = (int) tmpbuf;
529 mask = 0xffffffff << (m->size);
532 * OK, now we have the correct part of the number.
533 * Check to see whether the basic type is signed or not, and if so,
534 * we need to sign extend the number.
536 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
537 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
544 value->addr.off += (m->offset >> 3);
555 DEBUG_SetStructSize(struct datatype * dt, int size)
557 assert(dt->type == DT_STRUCT);
559 if( dt->un.structure.members != NULL )
564 dt->un.structure.size = size;
565 dt->un.structure.members = NULL;
571 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
574 assert( dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)));
576 if( dt->type == DT_STRUCT )
578 dt->un.structure.members = dt2->un.structure.members;
582 dt->un.enumeration.members = dt2->un.enumeration.members;
589 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
590 int offset, int size)
593 struct member * last;
594 struct en_values * e;
596 if( dt->type == DT_STRUCT )
598 for(last = dt->un.structure.members; last; last = last->next)
600 if( (last->name[0] == name[0])
601 && (strcmp(last->name, name) == 0) )
605 if( last->next == NULL )
610 m = (struct member *) DBG_alloc(sizeof(struct member));
616 m->name = DBG_strdup(name);
622 m->next = dt->un.structure.members;
623 dt->un.structure.members = m;
631 * If the base type is bitfield, then adjust the offsets here so that we
632 * are able to look things up without lots of falter-all.
634 if( type && type->type == DT_BITFIELD )
636 m->offset += m->type->un.bitfield.bitoff;
637 m->size = m->type->un.bitfield.nbits;
638 m->type = m->type->un.bitfield.basetype;
641 else if( dt->type == DT_ENUM )
643 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
649 e->name = DBG_strdup(name);
651 e->next = dt->un.enumeration.members;
652 dt->un.enumeration.members = e;
662 DEBUG_GetPointerType(struct datatype * dt)
664 if( dt->type == DT_POINTER )
666 return dt->un.pointer.pointsto;
673 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
678 dt->un.pointer.pointsto = dt2;
681 dt->un.funct.rettype = dt2;
691 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
693 assert(dt->type == DT_ARRAY);
694 dt->un.array.start = min;
695 dt->un.array.end = max;
696 dt->un.array.basictype = dt2;
702 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
703 struct datatype * dt2)
705 assert(dt->type == DT_BITFIELD);
706 dt->un.bitfield.bitoff = offset;
707 dt->un.bitfield.nbits = nbits;
708 dt->un.bitfield.basetype = dt2;
713 int DEBUG_GetObjectSize(struct datatype * dt)
723 return dt->un.basic.basic_size;
725 return sizeof(int *);
727 return dt->un.structure.size;
731 return (dt->un.array.end - dt->un.array.start)
732 * DEBUG_GetObjectSize(dt->un.array.basictype);
735 * Bitfields have to be handled seperately later on
736 * when we insert the element into the structure.
748 DEBUG_ArrayIndex(const DBG_VALUE * value, DBG_VALUE * result, int index)
752 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
755 * Make sure that this really makes sense.
757 if( value->type->type == DT_POINTER )
760 * Get the base type, so we know how much to index by.
762 size = DEBUG_GetObjectSize(value->type->un.pointer.pointsto);
763 result->type = value->type->un.pointer.pointsto;
764 result->addr.off = (*(unsigned int*) (value->addr.off)) + size * index;
766 else if (value->type->type == DT_ARRAY)
768 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
769 result->type = value->type->un.array.basictype;
770 result->addr.off = value->addr.off + size * (index - value->type->un.array.start);
776 /***********************************************************************
779 * Implementation of the 'print' command.
782 DEBUG_Print( const DBG_VALUE *value, int count, char format, int level )
791 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
795 DEBUG_Printf( DBG_CHN_MESG, "Count other than 1 is meaningless in 'print' command\n" );
799 if( value->type == NULL )
801 /* No type, just print the addr value */
802 if (value->addr.seg && (value->addr.seg != 0xffffffff))
803 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%04lx: ", value->addr.seg );
804 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", value->addr.off );
813 if( DEBUG_nchar > DEBUG_maxchar )
815 DEBUG_Printf(DBG_CHN_MESG, "...");
819 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
821 DEBUG_Printf( DBG_CHN_MESG, "Format specifier '%c' is meaningless in 'print' command\n", format );
825 switch(value->type->type)
831 DEBUG_PrintBasic(value, 1, format);
834 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
835 for(m = value->type->un.structure.members; m; m = m->next)
838 DEBUG_FindStructElement(&val1, m->name, &xval);
839 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "%s=", m->name);
840 DEBUG_Print(&val1, 1, format, level + 1);
841 if( m->next != NULL )
843 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
845 if( DEBUG_nchar > DEBUG_maxchar )
847 DEBUG_Printf(DBG_CHN_MESG, "...}");
851 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
855 * Loop over all of the entries, printing stuff as we go.
857 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
861 * Special handling for character arrays.
863 pnt = (char *) value->addr.off;
864 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
865 for( i=value->type->un.array.start; i < value->type->un.array.end; i++ )
867 DEBUG_Output(DBG_CHN_MESG, pnt++, 1);
869 if( DEBUG_nchar > DEBUG_maxchar )
871 DEBUG_Printf(DBG_CHN_MESG, "...\"");
875 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
879 val1.type = value->type->un.array.basictype;
880 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
881 for( i=value->type->un.array.start; i <= value->type->un.array.end; i++ )
883 DEBUG_Print(&val1, 1, format, level + 1);
884 val1.addr.off += size;
885 if( i == value->type->un.array.end )
887 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
891 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
893 if( DEBUG_nchar > DEBUG_maxchar )
895 DEBUG_Printf(DBG_CHN_MESG, "...}");
909 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\n");
915 DEBUG_DumpTypes(void)
917 struct datatype * dt = NULL;
924 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
926 for( dt = type_hash_table[hash]; dt; dt = dt->next )
929 if( dt->name != NULL )
936 DEBUG_Printf(DBG_CHN_MESG, "0x%p - BASIC(%s)\n",
940 DEBUG_Printf(DBG_CHN_MESG, "0x%p - POINTER(%s)(%p)\n",
941 dt, name, dt->un.pointer.pointsto);
944 member_name = "none";
946 if( dt->un.structure.members != NULL
947 && dt->un.structure.members->name != NULL )
949 member_name = dt->un.structure.members->name;
950 for( m = dt->un.structure.members; m; m = m->next)
955 DEBUG_Printf(DBG_CHN_MESG, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
956 dt->un.structure.size, nm, member_name);
959 DEBUG_Printf(DBG_CHN_MESG, "0x%p - ARRAY(%s)(%p)\n",
960 dt, name, dt->un.array.basictype);
963 DEBUG_Printf(DBG_CHN_MESG, "0x%p - ENUM(%s)\n", dt, name);
966 DEBUG_Printf(DBG_CHN_MESG, "0x%p - BITFIELD(%s)\n", dt, name);
969 DEBUG_Printf(DBG_CHN_MESG, "0x%p - FUNC(%s)(%p)\n",
970 dt, name, dt->un.funct.rettype);
974 DEBUG_Printf(DBG_CHN_MESG, "What???\n");
983 enum debug_type DEBUG_GetType(struct datatype * dt)
989 DEBUG_TypeCast(enum debug_type type, const char * name)
992 struct datatype * rtn;
995 * The last bucket is special, and is used to hold typeless names.
1003 hash = type_hash(name);
1006 rtn = DEBUG_LookupDataType(type, hash, name);
1013 DEBUG_PrintTypeCast(const struct datatype * dt)
1015 const char* name = "none";
1017 if( dt->name != NULL )
1025 DEBUG_Printf(DBG_CHN_MESG, "%s", name);
1028 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
1029 DEBUG_Printf(DBG_CHN_MESG, "*");
1032 DEBUG_Printf(DBG_CHN_MESG, "struct %s", name);
1035 DEBUG_Printf(DBG_CHN_MESG, "%s[]", name);
1038 DEBUG_Printf(DBG_CHN_MESG, "enum %s", name);
1041 DEBUG_Printf(DBG_CHN_MESG, "unsigned %s", name);
1044 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1045 DEBUG_Printf(DBG_CHN_MESG, "(*%s)()", name);
1049 DEBUG_Printf(DBG_CHN_MESG, "What???\n");
1056 int DEBUG_PrintType( const DBG_VALUE *value )
1058 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
1062 DEBUG_Printf(DBG_CHN_MESG, "Unknown type\n");
1065 if (!DEBUG_PrintTypeCast(value->type))
1067 DEBUG_Printf(DBG_CHN_MESG, "\n");