2 * File types.c - datatype handling stuff for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: This really doesn't do much at the moment, but it forms the framework
21 * upon which full support for datatype handling will eventually be built.
28 #include <sys/types.h>
38 #define NR_TYPE_HASH 521
41 static const int DEBUG_maxchar = 1024;
45 struct en_values* next;
54 struct datatype * type;
62 struct datatype * next;
69 const char * output_format;
75 unsigned short bitoff;
77 struct datatype * basetype;
82 struct datatype * pointsto;
86 struct datatype * rettype;
92 struct datatype * basictype;
97 struct member * members;
101 struct en_values * members;
107 * All of the types that have been defined so far.
109 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
110 static struct datatype * pointer_types = NULL;
111 static struct datatype * basic_types[DT_BASIC_LAST];
113 static unsigned int type_hash( const char * name )
115 unsigned int hash = 0;
123 hash = (hash << 4) + *p++;
125 if( (tmp = (hash & 0xf0000000)) )
131 return hash % NR_TYPE_HASH;
135 static struct datatype *
136 DEBUG_InitBasic(int type, const char * name, int size, int b_signed,
137 const char * output_format)
141 struct datatype * dt;
142 dt = (struct datatype *) DBG_alloc(sizeof(struct datatype));
148 hash = type_hash(name);
157 dt->next = type_hash_table[hash];
158 type_hash_table[hash] = dt;
159 dt->un.basic.basic_type = type;
160 dt->un.basic.basic_size = size;
161 dt->un.basic.b_signed = b_signed;
162 dt->un.basic.output_format = output_format;
163 basic_types[type] = dt;
171 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
173 struct datatype * dt = NULL;
175 if( typename != NULL )
177 for( dt = type_hash_table[hash]; dt; dt = dt->next )
179 if( xtype != dt->type || dt->name == NULL
180 || dt->name[0] != typename[0])
185 if( strcmp(dt->name, typename) == 0 )
196 DEBUG_GetBasicType(enum debug_type_basic basic)
198 if (basic == 0 || basic >= DT_BASIC_LAST)
202 return basic_types[basic];
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;
300 if( beenhere++ != 0 )
306 * Initialize a few builtin types.
309 DEBUG_InitBasic(DT_BASIC_INT,"int",4,1,"%d");
310 DEBUG_InitBasic(DT_BASIC_CHAR,"char",1,1,"'%c'");
311 DEBUG_InitBasic(DT_BASIC_LONGINT,"long int",4,1,"%d");
312 DEBUG_InitBasic(DT_BASIC_UINT,"unsigned int",4,0,"%d");
313 DEBUG_InitBasic(DT_BASIC_ULONGINT,"long unsigned int",4,0,"%d");
314 DEBUG_InitBasic(DT_BASIC_LONGLONGINT,"long long int",8,1,"%ld");
315 DEBUG_InitBasic(DT_BASIC_ULONGLONGINT,"long long unsigned int",8,0,"%ld");
316 DEBUG_InitBasic(DT_BASIC_SHORTINT,"short int",2,1,"%d");
317 DEBUG_InitBasic(DT_BASIC_USHORTINT,"short unsigned int",2,0,"%d");
318 DEBUG_InitBasic(DT_BASIC_SCHAR,"signed char",1,1,"'%c'");
319 DEBUG_InitBasic(DT_BASIC_UCHAR,"unsigned char",1,0,"'%c'");
320 DEBUG_InitBasic(DT_BASIC_FLOAT,"float",4,0,"%f");
321 DEBUG_InitBasic(DT_BASIC_DOUBLE,"long double",12,0,NULL);
322 DEBUG_InitBasic(DT_BASIC_LONGDOUBLE,"double",8,0,"%lf");
323 DEBUG_InitBasic(DT_BASIC_CMPLX_INT,"complex int",8,1,NULL);
324 DEBUG_InitBasic(DT_BASIC_CMPLX_FLOAT,"complex float",8,0,NULL);
325 DEBUG_InitBasic(DT_BASIC_CMPLX_DOUBLE,"complex double",16,0,NULL);
326 DEBUG_InitBasic(DT_BASIC_CMPLX_LONGDOUBLE,"complex long double",24,0,NULL);
327 DEBUG_InitBasic(DT_BASIC_VOID,"void",0,0,NULL);
328 DEBUG_InitBasic(DT_BASIC_BOOL1,NULL,1,0,"%B");
329 DEBUG_InitBasic(DT_BASIC_BOOL2,NULL,2,0,"%B");
330 DEBUG_InitBasic(DT_BASIC_BOOL4,NULL,4,0,"%B");
332 basic_types[DT_BASIC_STRING] = DEBUG_NewDataType(DT_POINTER, NULL);
333 DEBUG_SetPointerType(basic_types[DT_BASIC_STRING], basic_types[DT_BASIC_CHAR]);
336 * Special version of int used with constants of various kinds.
338 DEBUG_InitBasic(DT_BASIC_CONST_INT,NULL,4,1,"%d");
341 * Now initialize the builtins for codeview.
343 DEBUG_InitCVDataTypes();
345 DEBUG_InitBasic(DT_BASIC_CONTEXT,NULL,4,0,"%R");
349 DEBUG_GetExprValue(const DBG_VALUE* _value, const char** format)
353 struct datatype * type2 = NULL;
354 struct en_values * e;
355 const char * def_format = "0x%x";
356 DBG_VALUE value = *_value;
358 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
361 /* FIXME? I don't quite get this...
362 * if this is wrong, value.addr shall be linearized
365 assert(value.type != NULL);
367 switch (value.type->type) {
370 if (value.type->un.basic.basic_size > sizeof(rtn)) {
371 DEBUG_Printf(DBG_CHN_ERR, "Size too large (%d)\n",
372 value.type->un.basic.basic_size);
375 /* FIXME: following code implies i386 byte ordering */
376 if (_value->cookie == DV_TARGET) {
377 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn,
378 value.type->un.basic.basic_size))
381 memcpy(&rtn, (void*)value.addr.off, value.type->un.basic.basic_size);
384 if ( (value.type->un.basic.b_signed)
385 && ((value.type->un.basic.basic_size & 3) != 0)
386 && ((rtn >> (value.type->un.basic.basic_size * 8 - 1)) != 0)) {
387 rtn = rtn | ((-1) << (value.type->un.basic.basic_size * 8));
389 /* float type has to be promoted as a double */
390 if (value.type->un.basic.basic_type == DT_BASIC_FLOAT) {
393 memcpy(&f, &rtn, sizeof(f));
395 memcpy(&rtn, &d, sizeof(rtn));
397 if (value.type->un.basic.output_format != NULL) {
398 def_format = value.type->un.basic.output_format;
402 * Check for single character prints that are out of range.
404 if ( value.type->un.basic.basic_size == 1
405 && strcmp(def_format, "'%c'") == 0
406 && ((rtn < 0x20) || (rtn > 0x80))) {
411 if (_value->cookie == DV_TARGET) {
412 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(void*)))
415 rtn2 = *(unsigned int*)(value.addr.off);
418 type2 = value.type->un.pointer.pointsto;
421 def_format = "Internal symbol error: unable to access memory location 0x%08x";
426 if (type2->type == DT_BASIC && type2->un.basic.basic_size == 1) {
427 if (_value->cookie == DV_TARGET) {
429 def_format = "\"%S\"";
430 /* FIXME: assuming little endian */
431 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn2, &ch, 1))
434 def_format = "\"%s\"";
437 def_format = "0x%8.8x";
443 assert(_value->cookie == DV_TARGET);
444 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(rtn2)))
447 def_format = "0x%8.8x";
450 assert(_value->cookie == DV_TARGET);
451 if (!DEBUG_READ_MEM_VERBOSE((void*)value.addr.off, &rtn2, sizeof(rtn2)))
455 for (e = value.type->un.enumeration.members; e; e = e->next) {
456 if (e->value == rtn) {
469 if (format != NULL) {
470 *format = def_format;
476 DEBUG_TypeDerefPointer(const DBG_VALUE *value, struct datatype ** newtype)
478 DBG_ADDR addr = value->addr;
481 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
486 * Make sure that this really makes sense.
488 if( value->type->type != DT_POINTER )
491 if (value->cookie == DV_TARGET) {
492 if (!DEBUG_READ_MEM((void*)value->addr.off, &val, sizeof(val)))
495 val = *(unsigned int*)value->addr.off;
498 *newtype = value->type->un.pointer.pointsto;
500 return DEBUG_ToLinear(&addr); /* FIXME: is this right (or "better") ? */
504 DEBUG_FindStructElement(DBG_VALUE* value, const char * ele_name, int * tmpbuf)
509 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
512 * Make sure that this really makes sense.
514 if( value->type->type != DT_STRUCT )
520 for(m = value->type->un.structure.members; m; m = m->next)
522 if( strcmp(m->name, ele_name) == 0 )
524 value->type = m->type;
525 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
528 * Bitfield operation. We have to extract the field and store
529 * it in a temporary buffer so that we get it all right.
531 *tmpbuf = ((*(int* ) (value->addr.off + (m->offset >> 3))) >> (m->offset & 7));
532 value->addr.off = (int) tmpbuf;
534 mask = 0xffffffff << (m->size);
537 * OK, now we have the correct part of the number.
538 * Check to see whether the basic type is signed or not, and if so,
539 * we need to sign extend the number.
541 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
542 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
549 value->addr.off += (m->offset >> 3);
560 DEBUG_SetStructSize(struct datatype * dt, int size)
562 assert(dt->type == DT_STRUCT);
564 if( dt->un.structure.members != NULL )
569 dt->un.structure.size = size;
570 dt->un.structure.members = NULL;
576 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
578 if (!(dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)))) {
579 DEBUG_Printf(DBG_CHN_MESG, "Error: Copyfield list mismatch (%d<>%d): ", dt->type, dt2->type);
580 DEBUG_PrintTypeCast(dt);
581 DEBUG_Printf(DBG_CHN_MESG, " ");
582 DEBUG_PrintTypeCast(dt2);
583 DEBUG_Printf(DBG_CHN_MESG, "\n");
587 if( dt->type == DT_STRUCT )
589 dt->un.structure.members = dt2->un.structure.members;
593 dt->un.enumeration.members = dt2->un.enumeration.members;
600 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
601 int offset, int size)
604 struct member * last;
605 struct en_values * e;
607 if( dt->type == DT_STRUCT )
609 for(last = dt->un.structure.members; last; last = last->next)
611 if( (last->name[0] == name[0])
612 && (strcmp(last->name, name) == 0) )
616 if( last->next == NULL )
621 m = (struct member *) DBG_alloc(sizeof(struct member));
627 m->name = DBG_strdup(name);
633 m->next = dt->un.structure.members;
634 dt->un.structure.members = m;
642 * If the base type is bitfield, then adjust the offsets here so that we
643 * are able to look things up without lots of falter-all.
645 if( type && type->type == DT_BITFIELD )
647 m->offset += m->type->un.bitfield.bitoff;
648 m->size = m->type->un.bitfield.nbits;
649 m->type = m->type->un.bitfield.basetype;
652 else if( dt->type == DT_ENUM )
654 e = (struct en_values *) DBG_alloc(sizeof(struct en_values));
660 e->name = DBG_strdup(name);
662 e->next = dt->un.enumeration.members;
663 dt->un.enumeration.members = e;
673 DEBUG_GetPointerType(struct datatype * dt)
675 if( dt->type == DT_POINTER )
677 return dt->un.pointer.pointsto;
684 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
689 dt->un.pointer.pointsto = dt2;
692 dt->un.funct.rettype = dt2;
702 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
704 assert(dt->type == DT_ARRAY);
705 dt->un.array.start = min;
706 dt->un.array.end = max;
707 dt->un.array.basictype = dt2;
713 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
714 struct datatype * dt2)
716 assert(dt->type == DT_BITFIELD);
717 dt->un.bitfield.bitoff = offset;
718 dt->un.bitfield.nbits = nbits;
719 dt->un.bitfield.basetype = dt2;
724 int DEBUG_GetObjectSize(struct datatype * dt)
734 return dt->un.basic.basic_size;
736 return sizeof(int *);
738 return dt->un.structure.size;
742 return (dt->un.array.end - dt->un.array.start)
743 * DEBUG_GetObjectSize(dt->un.array.basictype);
746 * Bitfields have to be handled separately later on
747 * when we insert the element into the structure.
753 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
760 DEBUG_ArrayIndex(const DBG_VALUE * value, DBG_VALUE * result, int index)
764 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
767 * Make sure that this really makes sense.
769 if( value->type->type == DT_POINTER )
772 * Get the base type, so we know how much to index by.
774 size = DEBUG_GetObjectSize(value->type->un.pointer.pointsto);
775 result->type = value->type->un.pointer.pointsto;
776 result->addr.off = (DWORD)DEBUG_ReadMemory(value) + size*index;
778 /* Contents of array must be on same target */
779 result->cookie = value->cookie;
781 else if (value->type->type == DT_ARRAY)
783 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
784 result->type = value->type->un.array.basictype;
785 result->addr.off = value->addr.off + size * (index - value->type->un.array.start);
787 /* Contents of array must be on same target */
788 result->cookie = value->cookie;
798 /***********************************************************************
801 * Implementation of the 'print' command.
804 DEBUG_Print( const DBG_VALUE *value, int count, char format, int level )
813 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
817 DEBUG_Printf( DBG_CHN_MESG, "Count other than 1 is meaningless in 'print' command\n" );
821 if( value->type == NULL )
823 /* No type, just print the addr value */
824 if (value->addr.seg && (value->addr.seg != 0xffffffff))
825 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%04lx: ", value->addr.seg );
826 DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", value->addr.off );
835 if( DEBUG_nchar > DEBUG_maxchar )
837 DEBUG_Printf(DBG_CHN_MESG, "...");
841 if( format == 'i' || format == 's' || format == 'w' || format == 'b' || format == 'g')
843 DEBUG_Printf( DBG_CHN_MESG, "Format specifier '%c' is meaningless in 'print' command\n", format );
847 switch(value->type->type)
852 DEBUG_PrintBasic(value, 1, format);
855 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
856 for(m = value->type->un.structure.members; m; m = m->next)
859 DEBUG_FindStructElement(&val1, m->name, &xval);
860 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "%s=", m->name);
861 DEBUG_Print(&val1, 1, format, level + 1);
862 if( m->next != NULL )
864 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
866 if( DEBUG_nchar > DEBUG_maxchar )
868 DEBUG_Printf(DBG_CHN_MESG, "...}");
872 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
876 * Loop over all of the entries, printing stuff as we go.
878 size = DEBUG_GetObjectSize(value->type->un.array.basictype);
884 * Special handling for character arrays.
886 pnt = (char *) value->addr.off;
887 len = value->type->un.array.end - value->type->un.array.start + 1;
888 clen = (DEBUG_nchar + len < DEBUG_maxchar)
889 ? len : (DEBUG_maxchar - DEBUG_nchar);
891 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\"");
892 switch (value->cookie)
895 DEBUG_nchar += DEBUG_PrintStringA(DBG_CHN_MESG, &value->addr, clen);
898 DEBUG_OutputA(DBG_CHN_MESG, pnt, clen);
902 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, (len > clen) ? "...\"" : "\"");
906 val1.type = value->type->un.array.basictype;
907 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "{");
908 for( i=value->type->un.array.start; i <= value->type->un.array.end; i++ )
910 DEBUG_Print(&val1, 1, format, level + 1);
911 val1.addr.off += size;
912 if( i == value->type->un.array.end )
914 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "}");
918 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, ", ");
920 if( DEBUG_nchar > DEBUG_maxchar )
922 DEBUG_Printf(DBG_CHN_MESG, "...}");
928 DEBUG_Printf(DBG_CHN_MESG, "Function at ???\n");
931 DEBUG_Printf(DBG_CHN_MESG, "Unknown type (%d)\n", value->type->type);
940 DEBUG_nchar += DEBUG_Printf(DBG_CHN_MESG, "\n");
944 static void DEBUG_DumpAType(struct datatype* dt, BOOL deep)
946 const char* name = (dt->name) ? dt->name : "--none--";
948 /* EPP DEBUG_Printf(DBG_CHN_MESG, "0x%08lx ", (unsigned long)dt); */
952 DEBUG_Printf(DBG_CHN_MESG, "BASIC(%s)", name);
955 DEBUG_Printf(DBG_CHN_MESG, "POINTER(%s)<", name);
956 DEBUG_DumpAType(dt->un.pointer.pointsto, FALSE);
957 DEBUG_Printf(DBG_CHN_MESG, ">");
960 DEBUG_Printf(DBG_CHN_MESG, "STRUCT(%s) %d {",
961 name, dt->un.structure.size);
962 if (dt->un.structure.members != NULL)
965 for (m = dt->un.structure.members; m; m = m->next)
967 DEBUG_Printf(DBG_CHN_MESG, " %s(%d",
968 m->name, m->offset / 8);
969 if (m->offset % 8 != 0)
970 DEBUG_Printf(DBG_CHN_MESG, ".%d", m->offset / 8);
971 DEBUG_Printf(DBG_CHN_MESG, "/%d", m->size / 8);
972 if (m->size % 8 != 0)
973 DEBUG_Printf(DBG_CHN_MESG, ".%d", m->size % 8);
974 DEBUG_Printf(DBG_CHN_MESG, ")");
977 DEBUG_Printf(DBG_CHN_MESG, " }");
980 DEBUG_Printf(DBG_CHN_MESG, "ARRAY(%s)[", name);
981 DEBUG_DumpAType(dt->un.array.basictype, FALSE);
982 DEBUG_Printf(DBG_CHN_MESG, "]");
985 DEBUG_Printf(DBG_CHN_MESG, "ENUM(%s)", name);
988 DEBUG_Printf(DBG_CHN_MESG, "BITFIELD(%s)", name);
991 DEBUG_Printf(DBG_CHN_MESG, "FUNC(%s)(", name);
992 DEBUG_DumpAType(dt->un.funct.rettype, FALSE);
993 DEBUG_Printf(DBG_CHN_MESG, ")");
996 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???");
999 if (deep) DEBUG_Printf(DBG_CHN_MESG, "\n");
1002 int DEBUG_DumpTypes(void)
1004 struct datatype * dt = NULL;
1007 for (hash = 0; hash < NR_TYPE_HASH + 1; hash++)
1009 for (dt = type_hash_table[hash]; dt; dt = dt->next)
1011 DEBUG_DumpAType(dt, TRUE);
1017 enum debug_type DEBUG_GetType(struct datatype * dt)
1022 const char* DEBUG_GetName(struct datatype * dt)
1028 DEBUG_TypeCast(enum debug_type type, const char * name)
1033 * The last bucket is special, and is used to hold typeless names.
1037 hash = NR_TYPE_HASH;
1041 hash = type_hash(name);
1044 return DEBUG_LookupDataType(type, hash, name);
1048 DEBUG_PrintTypeCast(const struct datatype * dt)
1050 const char* name = "none";
1054 DEBUG_Printf(DBG_CHN_MESG, "--invalid--");
1058 if( dt->name != NULL )
1066 DEBUG_Printf(DBG_CHN_MESG, "%s", name);
1069 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
1070 DEBUG_Printf(DBG_CHN_MESG, "*");
1073 DEBUG_Printf(DBG_CHN_MESG, "struct %s", name);
1076 DEBUG_Printf(DBG_CHN_MESG, "%s[]", name);
1079 DEBUG_Printf(DBG_CHN_MESG, "enum %s", name);
1082 DEBUG_Printf(DBG_CHN_MESG, "unsigned %s", name);
1085 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1086 DEBUG_Printf(DBG_CHN_MESG, "(*%s)()", name);
1089 DEBUG_Printf(DBG_CHN_ERR, "Unknown type???\n");
1096 int DEBUG_PrintType( const DBG_VALUE *value )
1098 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
1102 DEBUG_Printf(DBG_CHN_MESG, "Unknown type\n");
1105 if (!DEBUG_PrintTypeCast(value->type))
1107 DEBUG_Printf(DBG_CHN_MESG, "\n");