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 built.
14 #include <sys/types.h>
22 #define NR_TYPE_HASH 521
25 static int DEBUG_maxchar = 1024;
29 struct en_values* next;
38 struct datatype * type;
46 struct datatype * next;
59 unsigned short bitoff;
61 struct datatype * basetype;
66 struct datatype * pointsto;
70 struct datatype * rettype;
76 struct datatype * basictype;
81 struct member * members;
85 struct en_values * members;
95 #define BASIC_LONGLONG 6
96 #define BASIC_ULONGLONGI 7
98 #define BASIC_SHORTUI 9
99 #define BASIC_SCHAR 10
100 #define BASIC_UCHAR 11
102 #define BASIC_LONG_DOUBLE 13
103 #define BASIC_DOUBLE 14
104 #define BASIC_CMPLX_INT 15
105 #define BASIC_CMPLX_FLT 16
106 #define BASIC_CMPLX_DBL 17
107 #define BASIC_CMPLX_LONG_DBL 18
108 #define BASIC_VOID 19
110 struct datatype * DEBUG_TypeInt = NULL;
111 struct datatype * DEBUG_TypeIntConst = NULL;
112 struct datatype * DEBUG_TypeUSInt = NULL;
113 struct datatype * DEBUG_TypeString = NULL;
114 struct datatype * DEBUG_TypeShortUInt = NULL;
116 * All of the types that have been defined so far.
118 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
119 static struct datatype * pointer_types = NULL;
121 static unsigned int type_hash( const char * name )
123 unsigned int hash = 0;
131 hash = (hash << 4) + *p++;
133 if( (tmp = (hash & 0xf0000000)) )
139 return hash % NR_TYPE_HASH;
143 static struct datatype *
144 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
145 char * output_format)
149 struct datatype * dt;
150 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
156 hash = type_hash(name);
165 dt->next = type_hash_table[hash];
166 type_hash_table[hash] = dt;
167 dt->un.basic.basic_type = type;
168 dt->un.basic.basic_size = size;
169 dt->un.basic.b_signed = b_signed;
170 dt->un.basic.output_format = output_format;
178 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
180 struct datatype * dt = NULL;
182 if( typename != NULL )
184 for( dt = type_hash_table[hash]; dt; dt = dt->next )
186 if( xtype != dt->type || dt->name == NULL
187 || dt->name[0] != typename[0])
192 if( strcmp(dt->name, typename) == 0 )
203 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
205 struct datatype * dt = NULL;
209 * The last bucket is special, and is used to hold typeless names.
211 if( typename == NULL )
217 hash = type_hash(typename);
220 dt = DEBUG_LookupDataType(xtype, hash, typename);
224 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
228 memset(dt, 0, sizeof(*dt));
231 if( typename != NULL )
233 dt->name = DBG_strdup(typename);
239 if( xtype == DT_POINTER )
241 dt->next = pointer_types;
246 dt->next = type_hash_table[hash];
247 type_hash_table[hash] = dt;
256 DEBUG_FindOrMakePointerType(struct datatype * reftype)
258 struct datatype * dt = NULL;
260 if( reftype != NULL )
262 for( dt = pointer_types; dt; dt = dt->next )
264 if( dt->type != DT_POINTER )
269 if( dt->un.pointer.pointsto == reftype )
278 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
282 dt->type = DT_POINTER;
283 dt->un.pointer.pointsto = reftype;
284 dt->next = pointer_types;
293 DEBUG_InitTypes(void)
295 static int beenhere = 0;
296 struct datatype * chartype;
298 if( beenhere++ != 0 )
304 * Special version of int used with constants of various kinds.
306 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
309 * Initialize a few builtin types.
312 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
313 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
314 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
315 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
316 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
317 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
318 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
319 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
320 DEBUG_TypeShortUInt = DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
321 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
322 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
323 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
324 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
325 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
326 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
327 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
328 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
329 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
330 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
332 DEBUG_TypeString = DEBUG_NewDataType(DT_POINTER, NULL);
333 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
336 * Now initialize the builtins for codeview.
338 DEBUG_InitCVDataTypes();
343 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) {
364 if (value.type->un.basic.basic_size > sizeof(rtn)) {
365 DEBUG_Printf(DBG_CHN_ERR, "Size too large (%d)\n",
366 value.type->un.basic.basic_size);
369 /* FIXME: following code implies i386 byte ordering */
370 if (_value->cookie == DV_TARGET) {
371 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn,
372 value.type->un.basic.basic_size))
375 memcpy(&rtn, (void*)value.addr.off, value.type->un.basic.basic_size);
378 if ( (value.type->un.basic.b_signed)
379 && ((value.type->un.basic.basic_size & 3) != 0)
380 && ((rtn >> (value.type->un.basic.basic_size * 8 - 1)) != 0)) {
381 rtn = rtn | ((-1) << (value.type->un.basic.basic_size * 8));
383 /* float type has to be promoted as a double */
384 if (value.type->un.basic.basic_type == BASIC_FLT) {
387 memcpy(&f, &rtn, sizeof(f));
389 memcpy(&rtn, &d, sizeof(rtn));
391 if (value.type->un.basic.output_format != NULL) {
392 def_format = value.type->un.basic.output_format;
396 * Check for single character prints that are out of range.
398 if ( value.type->un.basic.basic_size == 1
399 && strcmp(def_format, "'%c'") == 0
400 && ((rtn < 0x20) || (rtn > 0x80))) {
405 if (_value->cookie == DV_TARGET) {
406 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(void*)))
409 rtn2 = *(unsigned int*)(value.addr.off);
412 type2 = value.type->un.pointer.pointsto;
415 def_format = "Internal symbol error: unable to access memory location 0x%08x";
420 if (type2->type == DT_BASIC && type2->un.basic.basic_size == 1) {
421 if (_value->cookie == DV_TARGET) {
423 def_format = "\"%S\"";
424 /* FIXME: assuming little endian */
425 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn2, &ch, 1))
428 def_format = "\"%s\"";
431 def_format = "0x%8.8x";
437 assert(_value->cookie == DV_TARGET);
438 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(rtn2)))
441 def_format = "0x%8.8x";
444 assert(_value->cookie == DV_TARGET);
445 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(rtn2)))
449 for (e = value.type->un.enumeration.members; e; e = e->next) {
450 if (e->value == rtn) {
463 if (format != NULL) {
464 *format = def_format;
470 DEBUG_TypeDerefPointer(const DBG_VALUE *value, struct datatype ** newtype)
472 DBG_ADDR addr = value->addr;
475 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
480 * Make sure that this really makes sense.
482 if( value->type->type != DT_POINTER )
485 if (value->cookie == DV_TARGET) {
486 if (!DEBUG_READ_MEM((void*)value->addr.off, &val, sizeof(val)))
489 val = *(unsigned int*)value->addr.off;
492 *newtype = value->type->un.pointer.pointsto;
494 return DEBUG_ToLinear(&addr); /* FIXME: is this right (or "better") ? */
498 DEBUG_FindStructElement(DBG_VALUE* value, const char * ele_name, int * tmpbuf)
503 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
506 * Make sure that this really makes sense.
508 if( value->type->type != DT_STRUCT )
514 for(m = value->type->un.structure.members; m; m = m->next)
516 if( strcmp(m->name, ele_name) == 0 )
518 value->type = m->type;
519 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
522 * Bitfield operation. We have to extract the field and store
523 * it in a temporary buffer so that we get it all right.
525 *tmpbuf = ((*(int* ) (value->addr.off + (m->offset >> 3))) >> (m->offset & 7));
526 value->addr.off = (int) tmpbuf;
528 mask = 0xffffffff << (m->size);
531 * OK, now we have the correct part of the number.
532 * Check to see whether the basic type is signed or not, and if so,
533 * we need to sign extend the number.
535 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
536 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
543 value->addr.off += (m->offset >> 3);
554 DEBUG_SetStructSize(struct datatype * dt, int size)
556 assert(dt->type == DT_STRUCT);
558 if( dt->un.structure.members != NULL )
563 dt->un.structure.size = size;
564 dt->un.structure.members = NULL;
570 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
573 assert( dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)));
575 if( dt->type == DT_STRUCT )
577 dt->un.structure.members = dt2->un.structure.members;
581 dt->un.enumeration.members = dt2->un.enumeration.members;
588 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
589 int offset, int size)
592 struct member * last;
593 struct en_values * e;
595 if( dt->type == DT_STRUCT )
597 for(last = dt->un.structure.members; last; last = last->next)
599 if( (last->name[0] == name[0])
600 && (strcmp(last->name, name) == 0) )
604 if( last->next == NULL )
609 m = (struct member *) DBG_alloc(sizeof(struct member));
615 m->name = DBG_strdup(name);
621 m->next = dt->un.structure.members;
622 dt->un.structure.members = m;
630 * If the base type is bitfield, then adjust the offsets here so that we
631 * are able to look things up without lots of falter-all.
633 if( type && type->type == DT_BITFIELD )
635 m->offset += m->type->un.bitfield.bitoff;
636 m->size = m->type->un.bitfield.nbits;
637 m->type = m->type->un.bitfield.basetype;
640 else if( dt->type == DT_ENUM )
642 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
648 e->name = DBG_strdup(name);
650 e->next = dt->un.enumeration.members;
651 dt->un.enumeration.members = e;
661 DEBUG_GetPointerType(struct datatype * dt)
663 if( dt->type == DT_POINTER )
665 return dt->un.pointer.pointsto;
672 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
677 dt->un.pointer.pointsto = dt2;
680 dt->un.funct.rettype = dt2;
690 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
692 assert(dt->type == DT_ARRAY);
693 dt->un.array.start = min;
694 dt->un.array.end = max;
695 dt->un.array.basictype = dt2;
701 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
702 struct datatype * dt2)
704 assert(dt->type == DT_BITFIELD);
705 dt->un.bitfield.bitoff = offset;
706 dt->un.bitfield.nbits = nbits;
707 dt->un.bitfield.basetype = dt2;
712 int DEBUG_GetObjectSize(struct datatype * dt)
722 return dt->un.basic.basic_size;
724 return sizeof(int *);
726 return dt->un.structure.size;
730 return (dt->un.array.end - dt->un.array.start)
731 * DEBUG_GetObjectSize(dt->un.array.basictype);
734 * Bitfields have to be handled separately later on
735 * when we insert the element into the structure.
741 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
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 = (DWORD)DEBUG_ReadMemory(value) + size*index;
766 /* Contents of array must be on same target */
767 result->cookie = value->cookie;
769 else if (value->type->type == DT_ARRAY)
771 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
772 result->type = value->type->un.array.basictype;
773 result->addr.off = value->addr.off + size * (index - value->type->un.array.start);
775 /* Contents of array must be on same target */
776 result->cookie = value->cookie;
786 /***********************************************************************
789 * Implementation of the 'print' command.
792 DEBUG_Print( const DBG_VALUE *value, int count, char format, int level )
801 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
805 DEBUG_Printf( DBG_CHN_MESG, "Count other than 1 is meaningless in 'print' command\n" );
809 if( value->type == NULL )
811 /* No type, just print the addr value */
812 if (value->addr.seg && (value->addr.seg != 0xffffffff))
813 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%04lx: ", value->addr.seg );
814 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", value->addr.off );
823 if( DEBUG_nchar > DEBUG_maxchar )
825 DEBUG_Printf(DBG_CHN_MESG, "...");
829 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
831 DEBUG_Printf( DBG_CHN_MESG, "Format specifier '%c' is meaningless in 'print' command\n", format );
835 switch(value->type->type)
840 DEBUG_PrintBasic(value, 1, format);
843 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
844 for(m = value->type->un.structure.members; m; m = m->next)
847 DEBUG_FindStructElement(&val1, m->name, &xval);
848 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "%s=", m->name);
849 DEBUG_Print(&val1, 1, format, level + 1);
850 if( m->next != NULL )
852 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
854 if( DEBUG_nchar > DEBUG_maxchar )
856 DEBUG_Printf(DBG_CHN_MESG, "...}");
860 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
864 * Loop over all of the entries, printing stuff as we go.
866 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
870 * Special handling for character arrays.
872 pnt = (char *) value->addr.off;
873 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
874 for( i=value->type->un.array.start; i < value->type->un.array.end; i++ )
876 DEBUG_Output(DBG_CHN_MESG, pnt++, 1);
878 if( DEBUG_nchar > DEBUG_maxchar )
880 DEBUG_Printf(DBG_CHN_MESG, "...\"");
884 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
888 val1.type = value->type->un.array.basictype;
889 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
890 for( i=value->type->un.array.start; i <= value->type->un.array.end; i++ )
892 DEBUG_Print(&val1, 1, format, level + 1);
893 val1.addr.off += size;
894 if( i == value->type->un.array.end )
896 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
900 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
902 if( DEBUG_nchar > DEBUG_maxchar )
904 DEBUG_Printf(DBG_CHN_MESG, "...}");
910 DEBUG_Printf(DBG_CHN_MESG, "Function at ???\n");
913 DEBUG_Printf(DBG_CHN_MESG, "Unknown type (%d)\n", value->type->type);
922 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\n");
928 DEBUG_DumpTypes(void)
930 struct datatype * dt = NULL;
937 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
939 for( dt = type_hash_table[hash]; dt; dt = dt->next )
942 if( dt->name != NULL )
949 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - BASIC(%s)\n",
950 (unsigned long)dt, name);
953 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - POINTER(%s)(%08lx)\n",
954 (unsigned long)dt, name, (unsigned long)dt->un.pointer.pointsto);
957 member_name = "none";
959 if( dt->un.structure.members != NULL
960 && dt->un.structure.members->name != NULL )
962 member_name = dt->un.structure.members->name;
963 for( m = dt->un.structure.members; m; m = m->next)
968 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - STRUCT(%s) %d %d %s\n",
969 (unsigned long)dt, name, dt->un.structure.size, nm, member_name);
972 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - ARRAY(%s)(%08lx)\n",
973 (unsigned long)dt, name, (unsigned long)dt->un.array.basictype);
976 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - ENUM(%s)\n",
977 (unsigned long)dt, name);
980 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - BITFIELD(%s)\n",
981 (unsigned long)dt, name);
984 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - FUNC(%s)(%08lx)\n",
985 (unsigned long)dt, name, (unsigned long)dt->un.funct.rettype);
988 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
997 enum debug_type DEBUG_GetType(struct datatype * dt)
1003 DEBUG_TypeCast(enum debug_type type, const char * name)
1008 * The last bucket is special, and is used to hold typeless names.
1012 hash = NR_TYPE_HASH;
1016 hash = type_hash(name);
1019 return DEBUG_LookupDataType(type, hash, name);
1023 DEBUG_PrintTypeCast(const struct datatype * dt)
1025 const char* name = "none";
1027 if( dt->name != NULL )
1035 DEBUG_Printf(DBG_CHN_MESG, "%s", name);
1038 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
1039 DEBUG_Printf(DBG_CHN_MESG, "*");
1042 DEBUG_Printf(DBG_CHN_MESG, "struct %s", name);
1045 DEBUG_Printf(DBG_CHN_MESG, "%s[]", name);
1048 DEBUG_Printf(DBG_CHN_MESG, "enum %s", name);
1051 DEBUG_Printf(DBG_CHN_MESG, "unsigned %s", name);
1054 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1055 DEBUG_Printf(DBG_CHN_MESG, "(*%s)()", name);
1058 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
1065 int DEBUG_PrintType( const DBG_VALUE *value )
1067 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
1071 DEBUG_Printf(DBG_CHN_MESG, "Unknown type\n");
1074 if (!DEBUG_PrintTypeCast(value->type))
1076 DEBUG_Printf(DBG_CHN_MESG, "\n");