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;
91 * All of the types that have been defined so far.
93 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
94 static struct datatype * pointer_types = NULL;
95 static struct datatype * basic_types[DT_BASIC_LAST];
97 static unsigned int type_hash( const char * name )
99 unsigned int hash = 0;
107 hash = (hash << 4) + *p++;
109 if( (tmp = (hash & 0xf0000000)) )
115 return hash % NR_TYPE_HASH;
119 static struct datatype *
120 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
121 char * output_format)
125 struct datatype * dt;
126 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
132 hash = type_hash(name);
141 dt->next = type_hash_table[hash];
142 type_hash_table[hash] = dt;
143 dt->un.basic.basic_type = type;
144 dt->un.basic.basic_size = size;
145 dt->un.basic.b_signed = b_signed;
146 dt->un.basic.output_format = output_format;
147 basic_types[type] = dt;
155 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
157 struct datatype * dt = NULL;
159 if( typename != NULL )
161 for( dt = type_hash_table[hash]; dt; dt = dt->next )
163 if( xtype != dt->type || dt->name == NULL
164 || dt->name[0] != typename[0])
169 if( strcmp(dt->name, typename) == 0 )
180 DEBUG_GetBasicType(enum debug_type_basic basic)
182 if (basic == 0 || basic >= DT_BASIC_LAST)
186 return basic_types[basic];
190 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
192 struct datatype * dt = NULL;
196 * The last bucket is special, and is used to hold typeless names.
198 if( typename == NULL )
204 hash = type_hash(typename);
207 dt = DEBUG_LookupDataType(xtype, hash, typename);
211 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
215 memset(dt, 0, sizeof(*dt));
218 if( typename != NULL )
220 dt->name = DBG_strdup(typename);
226 if( xtype == DT_POINTER )
228 dt->next = pointer_types;
233 dt->next = type_hash_table[hash];
234 type_hash_table[hash] = dt;
243 DEBUG_FindOrMakePointerType(struct datatype * reftype)
245 struct datatype * dt = NULL;
247 if( reftype != NULL )
249 for( dt = pointer_types; dt; dt = dt->next )
251 if( dt->type != DT_POINTER )
256 if( dt->un.pointer.pointsto == reftype )
265 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
269 dt->type = DT_POINTER;
270 dt->un.pointer.pointsto = reftype;
271 dt->next = pointer_types;
280 DEBUG_InitTypes(void)
282 static int beenhere = 0;
284 if( beenhere++ != 0 )
290 * Initialize a few builtin types.
293 DEBUG_InitBasic(DT_BASIC_INT,"int",4,1,"%d");
294 DEBUG_InitBasic(DT_BASIC_CHAR,"char",1,1,"'%c'");
295 DEBUG_InitBasic(DT_BASIC_LONGINT,"long int",4,1,"%d");
296 DEBUG_InitBasic(DT_BASIC_UINT,"unsigned int",4,0,"%d");
297 DEBUG_InitBasic(DT_BASIC_ULONGINT,"long unsigned int",4,0,"%d");
298 DEBUG_InitBasic(DT_BASIC_LONGLONGINT,"long long int",8,1,"%ld");
299 DEBUG_InitBasic(DT_BASIC_ULONGLONGINT,"long long unsigned int",8,0,"%ld");
300 DEBUG_InitBasic(DT_BASIC_SHORTINT,"short int",2,1,"%d");
301 DEBUG_InitBasic(DT_BASIC_USHORTINT,"short unsigned int",2,0,"%d");
302 DEBUG_InitBasic(DT_BASIC_SCHAR,"signed char",1,1,"'%c'");
303 DEBUG_InitBasic(DT_BASIC_UCHAR,"unsigned char",1,0,"'%c'");
304 DEBUG_InitBasic(DT_BASIC_FLOAT,"float",4,0,"%f");
305 DEBUG_InitBasic(DT_BASIC_DOUBLE,"long double",12,0,NULL);
306 DEBUG_InitBasic(DT_BASIC_LONGDOUBLE,"double",8,0,"%lf");
307 DEBUG_InitBasic(DT_BASIC_CMPLX_INT,"complex int",8,1,NULL);
308 DEBUG_InitBasic(DT_BASIC_CMPLX_FLOAT,"complex float",8,0,NULL);
309 DEBUG_InitBasic(DT_BASIC_CMPLX_DOUBLE,"complex double",16,0,NULL);
310 DEBUG_InitBasic(DT_BASIC_CMPLX_LONGDOUBLE,"complex long double",24,0,NULL);
311 DEBUG_InitBasic(DT_BASIC_VOID,"void",0,0,NULL);
312 DEBUG_InitBasic(DT_BASIC_BOOL1,NULL,1,0,"%B");
313 DEBUG_InitBasic(DT_BASIC_BOOL2,NULL,2,0,"%B");
314 DEBUG_InitBasic(DT_BASIC_BOOL4,NULL,4,0,"%B");
316 basic_types[DT_BASIC_STRING] = DEBUG_NewDataType(DT_POINTER, NULL);
317 DEBUG_SetPointerType(basic_types[DT_BASIC_STRING], basic_types[DT_BASIC_CHAR]);
320 * Special version of int used with constants of various kinds.
322 DEBUG_InitBasic(DT_BASIC_CONST_INT,NULL,4,1,"%d");
325 * Now initialize the builtins for codeview.
327 DEBUG_InitCVDataTypes();
332 DEBUG_GetExprValue(const DBG_VALUE* _value, char** format)
336 struct datatype * type2 = NULL;
337 struct en_values * e;
338 char * def_format = "0x%x";
339 DBG_VALUE value = *_value;
341 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
344 /* FIXME? I don't quite get this...
345 * if this is wrong, value.addr shall be linearized
348 assert(value.type != NULL);
350 switch (value.type->type) {
353 if (value.type->un.basic.basic_size > sizeof(rtn)) {
354 DEBUG_Printf(DBG_CHN_ERR, "Size too large (%d)\n",
355 value.type->un.basic.basic_size);
358 /* FIXME: following code implies i386 byte ordering */
359 if (_value->cookie == DV_TARGET) {
360 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn,
361 value.type->un.basic.basic_size))
364 memcpy(&rtn, (void*)value.addr.off, value.type->un.basic.basic_size);
367 if ( (value.type->un.basic.b_signed)
368 && ((value.type->un.basic.basic_size & 3) != 0)
369 && ((rtn >> (value.type->un.basic.basic_size * 8 - 1)) != 0)) {
370 rtn = rtn | ((-1) << (value.type->un.basic.basic_size * 8));
372 /* float type has to be promoted as a double */
373 if (value.type->un.basic.basic_type == DT_BASIC_FLOAT) {
376 memcpy(&f, &rtn, sizeof(f));
378 memcpy(&rtn, &d, sizeof(rtn));
380 if (value.type->un.basic.output_format != NULL) {
381 def_format = value.type->un.basic.output_format;
385 * Check for single character prints that are out of range.
387 if ( value.type->un.basic.basic_size == 1
388 && strcmp(def_format, "'%c'") == 0
389 && ((rtn < 0x20) || (rtn > 0x80))) {
394 if (_value->cookie == DV_TARGET) {
395 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(void*)))
398 rtn2 = *(unsigned int*)(value.addr.off);
401 type2 = value.type->un.pointer.pointsto;
404 def_format = "Internal symbol error: unable to access memory location 0x%08x";
409 if (type2->type == DT_BASIC && type2->un.basic.basic_size == 1) {
410 if (_value->cookie == DV_TARGET) {
412 def_format = "\"%S\"";
413 /* FIXME: assuming little endian */
414 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn2, &ch, 1))
417 def_format = "\"%s\"";
420 def_format = "0x%8.8x";
426 assert(_value->cookie == DV_TARGET);
427 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(rtn2)))
430 def_format = "0x%8.8x";
433 assert(_value->cookie == DV_TARGET);
434 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(rtn2)))
438 for (e = value.type->un.enumeration.members; e; e = e->next) {
439 if (e->value == rtn) {
452 if (format != NULL) {
453 *format = def_format;
459 DEBUG_TypeDerefPointer(const DBG_VALUE *value, struct datatype ** newtype)
461 DBG_ADDR addr = value->addr;
464 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
469 * Make sure that this really makes sense.
471 if( value->type->type != DT_POINTER )
474 if (value->cookie == DV_TARGET) {
475 if (!DEBUG_READ_MEM((void*)value->addr.off, &val, sizeof(val)))
478 val = *(unsigned int*)value->addr.off;
481 *newtype = value->type->un.pointer.pointsto;
483 return DEBUG_ToLinear(&addr); /* FIXME: is this right (or "better") ? */
487 DEBUG_FindStructElement(DBG_VALUE* value, const char * ele_name, int * tmpbuf)
492 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
495 * Make sure that this really makes sense.
497 if( value->type->type != DT_STRUCT )
503 for(m = value->type->un.structure.members; m; m = m->next)
505 if( strcmp(m->name, ele_name) == 0 )
507 value->type = m->type;
508 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
511 * Bitfield operation. We have to extract the field and store
512 * it in a temporary buffer so that we get it all right.
514 *tmpbuf = ((*(int* ) (value->addr.off + (m->offset >> 3))) >> (m->offset & 7));
515 value->addr.off = (int) tmpbuf;
517 mask = 0xffffffff << (m->size);
520 * OK, now we have the correct part of the number.
521 * Check to see whether the basic type is signed or not, and if so,
522 * we need to sign extend the number.
524 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
525 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
532 value->addr.off += (m->offset >> 3);
543 DEBUG_SetStructSize(struct datatype * dt, int size)
545 assert(dt->type == DT_STRUCT);
547 if( dt->un.structure.members != NULL )
552 dt->un.structure.size = size;
553 dt->un.structure.members = NULL;
559 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
561 if (!(dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)))) {
562 DEBUG_Printf(DBG_CHN_MESG, "Error: Copyfield list mismatch (%d<>%d): ", dt->type, dt2->type);
563 DEBUG_PrintTypeCast(dt);
564 DEBUG_Printf(DBG_CHN_MESG, " ");
565 DEBUG_PrintTypeCast(dt2);
566 DEBUG_Printf(DBG_CHN_MESG, "\n");
570 if( dt->type == DT_STRUCT )
572 dt->un.structure.members = dt2->un.structure.members;
576 dt->un.enumeration.members = dt2->un.enumeration.members;
583 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
584 int offset, int size)
587 struct member * last;
588 struct en_values * e;
590 if( dt->type == DT_STRUCT )
592 for(last = dt->un.structure.members; last; last = last->next)
594 if( (last->name[0] == name[0])
595 && (strcmp(last->name, name) == 0) )
599 if( last->next == NULL )
604 m = (struct member *) DBG_alloc(sizeof(struct member));
610 m->name = DBG_strdup(name);
616 m->next = dt->un.structure.members;
617 dt->un.structure.members = m;
625 * If the base type is bitfield, then adjust the offsets here so that we
626 * are able to look things up without lots of falter-all.
628 if( type && type->type == DT_BITFIELD )
630 m->offset += m->type->un.bitfield.bitoff;
631 m->size = m->type->un.bitfield.nbits;
632 m->type = m->type->un.bitfield.basetype;
635 else if( dt->type == DT_ENUM )
637 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
643 e->name = DBG_strdup(name);
645 e->next = dt->un.enumeration.members;
646 dt->un.enumeration.members = e;
656 DEBUG_GetPointerType(struct datatype * dt)
658 if( dt->type == DT_POINTER )
660 return dt->un.pointer.pointsto;
667 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
672 dt->un.pointer.pointsto = dt2;
675 dt->un.funct.rettype = dt2;
685 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
687 assert(dt->type == DT_ARRAY);
688 dt->un.array.start = min;
689 dt->un.array.end = max;
690 dt->un.array.basictype = dt2;
696 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
697 struct datatype * dt2)
699 assert(dt->type == DT_BITFIELD);
700 dt->un.bitfield.bitoff = offset;
701 dt->un.bitfield.nbits = nbits;
702 dt->un.bitfield.basetype = dt2;
707 int DEBUG_GetObjectSize(struct datatype * dt)
717 return dt->un.basic.basic_size;
719 return sizeof(int *);
721 return dt->un.structure.size;
725 return (dt->un.array.end - dt->un.array.start)
726 * DEBUG_GetObjectSize(dt->un.array.basictype);
729 * Bitfields have to be handled separately later on
730 * when we insert the element into the structure.
736 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
743 DEBUG_ArrayIndex(const DBG_VALUE * value, DBG_VALUE * result, int index)
747 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
750 * Make sure that this really makes sense.
752 if( value->type->type == DT_POINTER )
755 * Get the base type, so we know how much to index by.
757 size = DEBUG_GetObjectSize(value->type->un.pointer.pointsto);
758 result->type = value->type->un.pointer.pointsto;
759 result->addr.off = (DWORD)DEBUG_ReadMemory(value) + size*index;
761 /* Contents of array must be on same target */
762 result->cookie = value->cookie;
764 else if (value->type->type == DT_ARRAY)
766 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
767 result->type = value->type->un.array.basictype;
768 result->addr.off = value->addr.off + size * (index - value->type->un.array.start);
770 /* Contents of array must be on same target */
771 result->cookie = value->cookie;
781 /***********************************************************************
784 * Implementation of the 'print' command.
787 DEBUG_Print( const DBG_VALUE *value, int count, char format, int level )
796 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
800 DEBUG_Printf( DBG_CHN_MESG, "Count other than 1 is meaningless in 'print' command\n" );
804 if( value->type == NULL )
806 /* No type, just print the addr value */
807 if (value->addr.seg && (value->addr.seg != 0xffffffff))
808 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%04lx: ", value->addr.seg );
809 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", value->addr.off );
818 if( DEBUG_nchar > DEBUG_maxchar )
820 DEBUG_Printf(DBG_CHN_MESG, "...");
824 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
826 DEBUG_Printf( DBG_CHN_MESG, "Format specifier '%c' is meaningless in 'print' command\n", format );
830 switch(value->type->type)
835 DEBUG_PrintBasic(value, 1, format);
838 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
839 for(m = value->type->un.structure.members; m; m = m->next)
842 DEBUG_FindStructElement(&val1, m->name, &xval);
843 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "%s=", m->name);
844 DEBUG_Print(&val1, 1, format, level + 1);
845 if( m->next != NULL )
847 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
849 if( DEBUG_nchar > DEBUG_maxchar )
851 DEBUG_Printf(DBG_CHN_MESG, "...}");
855 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
859 * Loop over all of the entries, printing stuff as we go.
861 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
868 * Special handling for character arrays.
870 pnt = (char *) value->addr.off;
871 len = value->type->un.array.end - value->type->un.array.start + 1;
872 clen = (DEBUG_nchar + len < DEBUG_maxchar)
873 ? len : (DEBUG_maxchar - DEBUG_nchar);
875 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
876 switch (value->cookie)
879 for (i = clen; i > 0; i -= sizeof(ach))
881 DEBUG_READ_MEM(pnt, ach, min(sizeof(ach), i));
882 DEBUG_Output(DBG_CHN_MESG, ach, min(sizeof(ach), i));
886 DEBUG_Output(DBG_CHN_MESG, pnt, clen);
893 DEBUG_Printf(DBG_CHN_MESG, "...\"");
896 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
900 val1.type = value->type->un.array.basictype;
901 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
902 for( i=value->type->un.array.start; i <= value->type->un.array.end; i++ )
904 DEBUG_Print(&val1, 1, format, level + 1);
905 val1.addr.off += size;
906 if( i == value->type->un.array.end )
908 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
912 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
914 if( DEBUG_nchar > DEBUG_maxchar )
916 DEBUG_Printf(DBG_CHN_MESG, "...}");
922 DEBUG_Printf(DBG_CHN_MESG, "Function at ???\n");
925 DEBUG_Printf(DBG_CHN_MESG, "Unknown type (%d)\n", value->type->type);
934 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\n");
940 DEBUG_DumpTypes(void)
942 struct datatype * dt = NULL;
949 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
951 for( dt = type_hash_table[hash]; dt; dt = dt->next )
954 if( dt->name != NULL )
961 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - BASIC(%s)\n",
962 (unsigned long)dt, name);
965 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - POINTER(%s)(%08lx)\n",
966 (unsigned long)dt, name, (unsigned long)dt->un.pointer.pointsto);
969 member_name = "none";
971 if( dt->un.structure.members != NULL
972 && dt->un.structure.members->name != NULL )
974 member_name = dt->un.structure.members->name;
975 for( m = dt->un.structure.members; m; m = m->next)
980 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - STRUCT(%s) %d %d %s\n",
981 (unsigned long)dt, name, dt->un.structure.size, nm, member_name);
984 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - ARRAY(%s)(%08lx)\n",
985 (unsigned long)dt, name, (unsigned long)dt->un.array.basictype);
988 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - ENUM(%s)\n",
989 (unsigned long)dt, name);
992 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - BITFIELD(%s)\n",
993 (unsigned long)dt, name);
996 DEBUG_Printf(DBG_CHN_MESG, "0x%08lx - FUNC(%s)(%08lx)\n",
997 (unsigned long)dt, name, (unsigned long)dt->un.funct.rettype);
1000 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
1009 enum debug_type DEBUG_GetType(struct datatype * dt)
1015 DEBUG_TypeCast(enum debug_type type, const char * name)
1020 * The last bucket is special, and is used to hold typeless names.
1024 hash = NR_TYPE_HASH;
1028 hash = type_hash(name);
1031 return DEBUG_LookupDataType(type, hash, name);
1035 DEBUG_PrintTypeCast(const struct datatype * dt)
1037 const char* name = "none";
1041 DEBUG_Printf(DBG_CHN_MESG, "--invalid--");
1045 if( dt->name != NULL )
1053 DEBUG_Printf(DBG_CHN_MESG, "%s", name);
1056 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
1057 DEBUG_Printf(DBG_CHN_MESG, "*");
1060 DEBUG_Printf(DBG_CHN_MESG, "struct %s", name);
1063 DEBUG_Printf(DBG_CHN_MESG, "%s[]", name);
1066 DEBUG_Printf(DBG_CHN_MESG, "enum %s", name);
1069 DEBUG_Printf(DBG_CHN_MESG, "unsigned %s", name);
1072 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1073 DEBUG_Printf(DBG_CHN_MESG, "(*%s)()", name);
1076 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
1083 int DEBUG_PrintType( const DBG_VALUE *value )
1085 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
1089 DEBUG_Printf(DBG_CHN_MESG, "Unknown type\n");
1092 if (!DEBUG_PrintTypeCast(value->type))
1094 DEBUG_Printf(DBG_CHN_MESG, "\n");