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>
28 #define NR_TYPE_HASH 521
31 static int DEBUG_maxchar
= 1024;
35 struct en_values
* next
;
44 struct datatype
* type
;
52 struct datatype
* next
;
65 unsigned short bitoff
;
67 struct datatype
* basetype
;
72 struct datatype
* pointsto
;
76 struct datatype
* rettype
;
82 struct datatype
* basictype
;
87 struct member
* members
;
91 struct en_values
* members
;
101 #define BASIC_LONGLONG 6
102 #define BASIC_ULONGLONGI 7
103 #define BASIC_SHORT 8
104 #define BASIC_SHORTUI 9
105 #define BASIC_SCHAR 10
106 #define BASIC_UCHAR 11
108 #define BASIC_LONG_DOUBLE 13
109 #define BASIC_DOUBLE 14
110 #define BASIC_CMPLX_INT 15
111 #define BASIC_CMPLX_FLT 16
112 #define BASIC_CMPLX_DBL 17
113 #define BASIC_CMPLX_LONG_DBL 18
114 #define BASIC_VOID 19
116 struct datatype
* DEBUG_TypeInt
= NULL
;
117 struct datatype
* DEBUG_TypeIntConst
= NULL
;
118 struct datatype
* DEBUG_TypeUSInt
= NULL
;
119 struct datatype
* DEBUG_TypeString
= NULL
;
122 * All of the types that have been defined so far.
124 static struct datatype
* type_hash_table
[NR_TYPE_HASH
+ 1];
125 static struct datatype
* pointer_types
= NULL
;
127 static unsigned int type_hash( const char * name
)
129 unsigned int hash
= 0;
137 hash
= (hash
<< 4) + *p
++;
139 if( (tmp
= (hash
& 0xf0000000)) )
145 return hash
% NR_TYPE_HASH
;
149 static struct datatype
*
150 DEBUG_InitBasic(int type
, char * name
, int size
, int b_signed
,
151 char * output_format
)
155 struct datatype
* dt
;
156 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
162 hash
= type_hash(name
);
171 dt
->next
= type_hash_table
[hash
];
172 type_hash_table
[hash
] = dt
;
173 dt
->un
.basic
.basic_type
= type
;
174 dt
->un
.basic
.basic_size
= size
;
175 dt
->un
.basic
.b_signed
= b_signed
;
176 dt
->un
.basic
.output_format
= output_format
;
184 DEBUG_LookupDataType(enum debug_type xtype
, int hash
, const char * typename
)
186 struct datatype
* dt
= NULL
;
188 if( typename
!= NULL
)
190 for( dt
= type_hash_table
[hash
]; dt
; dt
= dt
->next
)
192 if( xtype
!= dt
->type
|| dt
->name
== NULL
193 || dt
->name
[0] != typename
[0])
198 if( strcmp(dt
->name
, typename
) == 0 )
209 DEBUG_NewDataType(enum debug_type xtype
, const char * typename
)
211 struct datatype
* dt
= NULL
;
215 * The last bucket is special, and is used to hold typeless names.
217 if( typename
== NULL
)
223 hash
= type_hash(typename
);
226 dt
= DEBUG_LookupDataType(xtype
, hash
, typename
);
230 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
234 memset(dt
, 0, sizeof(*dt
));
237 if( typename
!= NULL
)
239 dt
->name
= xstrdup(typename
);
245 if( xtype
== POINTER
)
247 dt
->next
= pointer_types
;
252 dt
->next
= type_hash_table
[hash
];
253 type_hash_table
[hash
] = dt
;
262 DEBUG_FindOrMakePointerType(struct datatype
* reftype
)
264 struct datatype
* dt
= NULL
;
266 if( reftype
!= NULL
)
268 for( dt
= pointer_types
; dt
; dt
= dt
->next
)
270 if( dt
->type
!= POINTER
)
275 if( dt
->un
.pointer
.pointsto
== reftype
)
284 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
289 dt
->un
.pointer
.pointsto
= reftype
;
290 dt
->next
= pointer_types
;
301 static int beenhere
= 0;
302 struct datatype
* chartype
;
304 if( beenhere
++ != 0 )
310 * Special version of int used with constants of various kinds.
312 DEBUG_TypeIntConst
= DEBUG_InitBasic(BASIC_INT
,NULL
,4,1,"%d");
315 * Initialize a few builtin types.
319 DEBUG_TypeInt
= DEBUG_InitBasic(BASIC_INT
,"int",4,1,"%d");
320 chartype
= DEBUG_InitBasic(BASIC_CHAR
,"char",1,1,"'%c'");
321 DEBUG_InitBasic(BASIC_LONG
,"long int",4,1,"%d");
322 DEBUG_TypeUSInt
= DEBUG_InitBasic(BASIC_UINT
,"unsigned int",4,0,"%d");
323 DEBUG_InitBasic(BASIC_LUI
,"long unsigned int",4,0,"%d");
324 DEBUG_InitBasic(BASIC_LONGLONG
,"long long int",8,1,"%ld");
325 DEBUG_InitBasic(BASIC_ULONGLONGI
,"long long unsigned int",8,0,"%ld");
326 DEBUG_InitBasic(BASIC_SHORT
,"short int",2,1,"%d");
327 DEBUG_InitBasic(BASIC_SHORTUI
,"short unsigned int",2,0,"%d");
328 DEBUG_InitBasic(BASIC_SCHAR
,"signed char",1,1,"'%c'");
329 DEBUG_InitBasic(BASIC_UCHAR
,"unsigned char",1,0,"'%c'");
330 DEBUG_InitBasic(BASIC_FLT
,"float",4,0,"%f");
331 DEBUG_InitBasic(BASIC_LONG_DOUBLE
,"double",8,0,"%lf");
332 DEBUG_InitBasic(BASIC_DOUBLE
,"long double",12,0,NULL
);
333 DEBUG_InitBasic(BASIC_CMPLX_INT
,"complex int",8,1,NULL
);
334 DEBUG_InitBasic(BASIC_CMPLX_FLT
,"complex float",8,0,NULL
);
335 DEBUG_InitBasic(BASIC_CMPLX_DBL
,"complex double",16,0,NULL
);
336 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL
,"complex long double",24,0,NULL
);
337 DEBUG_InitBasic(BASIC_VOID
,"void",0,0,NULL
);
339 DEBUG_TypeString
= DEBUG_NewDataType(POINTER
, NULL
);
340 DEBUG_SetPointerType(DEBUG_TypeString
, chartype
);
343 * Now initialize the builtins for codeview.
345 DEBUG_InitCVDataTypes();
350 DEBUG_GetExprValue(DBG_ADDR
* addr
, char ** format
)
353 struct datatype
* type2
= NULL
;
354 struct en_values
* e
;
355 char * def_format
= "0x%x";
358 assert(addr
->type
!= NULL
);
360 switch(addr
->type
->type
)
363 if (!DBG_CHECK_READ_PTR( addr
, addr
->type
->un
.basic
.basic_size
))
368 memcpy(&rtn
, (char *) addr
->off
, addr
->type
->un
.basic
.basic_size
);
369 if( (addr
->type
->un
.basic
.b_signed
)
370 && ((addr
->type
->un
.basic
.basic_size
& 3) != 0)
371 && ((rtn
>> (addr
->type
->un
.basic
.basic_size
* 8 - 1)) != 0) )
373 rtn
= rtn
| ((-1) << (addr
->type
->un
.basic
.basic_size
* 8));
375 if( addr
->type
->un
.basic
.output_format
!= NULL
)
377 def_format
= addr
->type
->un
.basic
.output_format
;
381 * Check for single character prints that are out of range.
383 if( addr
->type
->un
.basic
.basic_size
== 1
384 && strcmp(def_format
, "'%c'") == 0
385 && ((rtn
< 0x20) || (rtn
> 0x80)) )
391 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return 0;
392 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
393 type2
= addr
->type
->un
.pointer
.pointsto
;
394 if( type2
->type
== BASIC
&& type2
->un
.basic
.basic_size
== 1 )
396 def_format
= "\"%s\"";
401 def_format
= "0x%8.8x";
406 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return 0;
407 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
408 def_format
= "0x%8.8x";
411 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
412 for(e
= addr
->type
->un
.enumeration
.members
; e
; e
= e
->next
)
414 if( e
->value
== rtn
)
437 *format
= def_format
;
443 DEBUG_TypeDerefPointer(DBG_ADDR
* addr
, struct datatype
** newtype
)
446 * Make sure that this really makes sense.
448 if( addr
->type
->type
!= POINTER
)
454 *newtype
= addr
->type
->un
.pointer
.pointsto
;
455 return *(unsigned int*) (addr
->off
);
459 DEBUG_FindStructElement(DBG_ADDR
* addr
, const char * ele_name
, int * tmpbuf
)
465 * Make sure that this really makes sense.
467 if( addr
->type
->type
!= STRUCT
)
473 for(m
= addr
->type
->un
.structure
.members
; m
; m
= m
->next
)
475 if( strcmp(m
->name
, ele_name
) == 0 )
477 addr
->type
= m
->type
;
478 if( (m
->offset
& 7) != 0 || (m
->size
& 7) != 0)
481 * Bitfield operation. We have to extract the field and store
482 * it in a temporary buffer so that we get it all right.
484 *tmpbuf
= ((*(int* ) (addr
->off
+ (m
->offset
>> 3))) >> (m
->offset
& 7));
485 addr
->off
= (int) tmpbuf
;
487 mask
= 0xffffffff << (m
->size
);
490 * OK, now we have the correct part of the number.
491 * Check to see whether the basic type is signed or not, and if so,
492 * we need to sign extend the number.
494 if( m
->type
->type
== BASIC
&& m
->type
->un
.basic
.b_signed
!= 0
495 && (*tmpbuf
& (1 << (m
->size
- 1))) != 0 )
502 addr
->off
+= (m
->offset
>> 3);
513 DEBUG_SetStructSize(struct datatype
* dt
, int size
)
515 assert(dt
->type
== STRUCT
);
517 if( dt
->un
.structure
.members
!= NULL
)
522 dt
->un
.structure
.size
= size
;
523 dt
->un
.structure
.members
= NULL
;
529 DEBUG_CopyFieldlist(struct datatype
* dt
, struct datatype
* dt2
)
532 assert( dt
->type
== dt2
->type
&& ((dt
->type
== STRUCT
) || (dt
->type
== ENUM
)));
534 if( dt
->type
== STRUCT
)
536 dt
->un
.structure
.members
= dt2
->un
.structure
.members
;
540 dt
->un
.enumeration
.members
= dt2
->un
.enumeration
.members
;
547 DEBUG_AddStructElement(struct datatype
* dt
, char * name
, struct datatype
* type
,
548 int offset
, int size
)
551 struct member
* last
;
552 struct en_values
* e
;
554 if( dt
->type
== STRUCT
)
556 for(last
= dt
->un
.structure
.members
; last
; last
= last
->next
)
558 if( (last
->name
[0] == name
[0])
559 && (strcmp(last
->name
, name
) == 0) )
563 if( last
->next
== NULL
)
568 m
= (struct member
*) xmalloc(sizeof(struct member
));
574 m
->name
= xstrdup(name
);
580 m
->next
= dt
->un
.structure
.members
;
581 dt
->un
.structure
.members
= m
;
589 * If the base type is bitfield, then adjust the offsets here so that we
590 * are able to look things up without lots of falter-all.
592 if( type
->type
== BITFIELD
)
594 m
->offset
+= m
->type
->un
.bitfield
.bitoff
;
595 m
->size
= m
->type
->un
.bitfield
.nbits
;
596 m
->type
= m
->type
->un
.bitfield
.basetype
;
599 else if( dt
->type
== ENUM
)
601 e
= (struct en_values
*) xmalloc(sizeof(struct en_values
));
607 e
->name
= xstrdup(name
);
609 e
->next
= dt
->un
.enumeration
.members
;
610 dt
->un
.enumeration
.members
= e
;
620 DEBUG_GetPointerType(struct datatype
* dt
)
622 if( dt
->type
== POINTER
)
624 return dt
->un
.pointer
.pointsto
;
631 DEBUG_SetPointerType(struct datatype
* dt
, struct datatype
* dt2
)
636 dt
->un
.pointer
.pointsto
= dt2
;
639 dt
->un
.funct
.rettype
= dt2
;
649 DEBUG_SetArrayParams(struct datatype
* dt
, int min
, int max
, struct datatype
* dt2
)
651 assert(dt
->type
== ARRAY
);
652 dt
->un
.array
.start
= min
;
653 dt
->un
.array
.end
= max
;
654 dt
->un
.array
.basictype
= dt2
;
660 DEBUG_SetBitfieldParams(struct datatype
* dt
, int offset
, int nbits
,
661 struct datatype
* dt2
)
663 assert(dt
->type
== BITFIELD
);
664 dt
->un
.bitfield
.bitoff
= offset
;
665 dt
->un
.bitfield
.nbits
= nbits
;
666 dt
->un
.bitfield
.basetype
= dt2
;
671 int DEBUG_GetObjectSize(struct datatype
* dt
)
681 return dt
->un
.basic
.basic_size
;
683 return sizeof(int *);
685 return dt
->un
.structure
.size
;
689 return (dt
->un
.array
.end
- dt
->un
.array
.start
)
690 * DEBUG_GetObjectSize(dt
->un
.array
.basictype
);
693 * Bitfields have to be handled seperately later on
694 * when we insert the element into the structure.
706 DEBUG_ArrayIndex(DBG_ADDR
* addr
, DBG_ADDR
* result
, int index
)
711 * Make sure that this really makes sense.
713 if( addr
->type
->type
== POINTER
)
716 * Get the base type, so we know how much to index by.
718 size
= DEBUG_GetObjectSize(addr
->type
->un
.pointer
.pointsto
);
719 result
->type
= addr
->type
->un
.pointer
.pointsto
;
720 result
->off
= (*(unsigned int*) (addr
->off
)) + size
* index
;
722 else if (addr
->type
->type
== ARRAY
)
724 size
= DEBUG_GetObjectSize(addr
->type
->un
.array
.basictype
);
725 result
->type
= addr
->type
->un
.array
.basictype
;
726 result
->off
= addr
->off
+ size
* (index
- addr
->type
->un
.array
.start
);
732 /***********************************************************************
735 * Implementation of the 'print' command.
738 DEBUG_Print( const DBG_ADDR
*addr
, int count
, char format
, int level
)
749 fprintf( stderr
, "Count other than 1 is meaningless in 'print' command\n" );
753 if( addr
->type
== NULL
)
755 fprintf(stderr
, "Unable to evaluate expression\n");
764 if( DEBUG_nchar
> DEBUG_maxchar
)
766 fprintf(stderr
, "...");
770 if( format
== 'i' || format
== 's' || format
== 'w' || format
== 'b' )
772 fprintf( stderr
, "Format specifier '%c' is meaningless in 'print' command\n", format
);
776 switch(addr
->type
->type
)
782 DEBUG_PrintBasic(addr
, 1, format
);
785 DEBUG_nchar
+= fprintf(stderr
, "{");
786 for(m
= addr
->type
->un
.structure
.members
; m
; m
= m
->next
)
789 DEBUG_FindStructElement(&addr1
, m
->name
,
791 DEBUG_nchar
+= fprintf(stderr
, "%s=", m
->name
);
792 DEBUG_Print(&addr1
, 1, format
, level
+ 1);
793 if( m
->next
!= NULL
)
795 DEBUG_nchar
+= fprintf(stderr
, ", ");
797 if( DEBUG_nchar
> DEBUG_maxchar
)
799 fprintf(stderr
, "...}");
803 DEBUG_nchar
+= fprintf(stderr
, "}");
807 * Loop over all of the entries, printing stuff as we go.
809 size
= DEBUG_GetObjectSize(addr
->type
->un
.array
.basictype
);
813 * Special handling for character arrays.
815 pnt
= (char *) addr
->off
;
816 DEBUG_nchar
+= fprintf(stderr
, "\"");
817 for( i
=addr
->type
->un
.array
.start
; i
< addr
->type
->un
.array
.end
; i
++ )
819 fputc(*pnt
++, stderr
);
821 if( DEBUG_nchar
> DEBUG_maxchar
)
823 fprintf(stderr
, "...\"");
827 DEBUG_nchar
+= fprintf(stderr
, "\"");
831 addr1
.type
= addr
->type
->un
.array
.basictype
;
832 DEBUG_nchar
+= fprintf(stderr
, "{");
833 for( i
=addr
->type
->un
.array
.start
; i
<= addr
->type
->un
.array
.end
; i
++ )
835 DEBUG_Print(&addr1
, 1, format
, level
+ 1);
837 if( i
== addr
->type
->un
.array
.end
)
839 DEBUG_nchar
+= fprintf(stderr
, "}");
843 DEBUG_nchar
+= fprintf(stderr
, ", ");
845 if( DEBUG_nchar
> DEBUG_maxchar
)
847 fprintf(stderr
, "...}");
861 DEBUG_nchar
+= fprintf(stderr
, "\n");
869 struct datatype
* dt
= NULL
;
876 for(hash
= 0; hash
< NR_TYPE_HASH
+ 1; hash
++)
878 for( dt
= type_hash_table
[hash
]; dt
; dt
= dt
->next
)
881 if( dt
->name
!= NULL
)
888 fprintf(stderr
, "0x%p - BASIC(%s)\n",
892 fprintf(stderr
, "0x%p - POINTER(%s)(%p)\n",
893 dt
, name
, dt
->un
.pointer
.pointsto
);
896 member_name
= "none";
898 if( dt
->un
.structure
.members
!= NULL
899 && dt
->un
.structure
.members
->name
!= NULL
)
901 member_name
= dt
->un
.structure
.members
->name
;
902 for( m
= dt
->un
.structure
.members
; m
; m
= m
->next
)
907 fprintf(stderr
, "0x%p - STRUCT(%s) %d %d %s\n", dt
, name
,
908 dt
->un
.structure
.size
, nm
, member_name
);
911 fprintf(stderr
, "0x%p - ARRAY(%s)(%p)\n",
912 dt
, name
, dt
->un
.array
.basictype
);
915 fprintf(stderr
, "0x%p - ENUM(%s)\n",
919 fprintf(stderr
, "0x%p - BITFIELD(%s)\n", dt
, name
);
922 fprintf(stderr
, "0x%p - FUNC(%s)(%p)\n",
923 dt
, name
, dt
->un
.funct
.rettype
);
927 fprintf(stderr
, "What???\n");
936 enum debug_type
DEBUG_GetType(struct datatype
* dt
)
942 DEBUG_TypeCast(enum debug_type type
, const char * name
)
945 struct datatype
* rtn
;
948 * The last bucket is special, and is used to hold typeless names.
956 hash
= type_hash(name
);
959 rtn
= DEBUG_LookupDataType(type
, hash
, name
);
966 DEBUG_PrintTypeCast(struct datatype
* dt
)
971 if( dt
->name
!= NULL
)
979 fprintf(stderr
, "%s", name
);
982 DEBUG_PrintTypeCast(dt
->un
.pointer
.pointsto
);
983 fprintf(stderr
, "*");
986 fprintf(stderr
, "struct %s", name
);
989 fprintf(stderr
, "%s[]", name
);
992 fprintf(stderr
, "enum %s", name
);
995 fprintf(stderr
, "unsigned %s", name
);
998 DEBUG_PrintTypeCast(dt
->un
.funct
.rettype
);
999 fprintf(stderr
, "(*%s)()", name
);
1003 fprintf(stderr
, "What???\n");