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
== DT_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
!= DT_POINTER
)
275 if( dt
->un
.pointer
.pointsto
== reftype
)
284 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
288 dt
->type
= DT_POINTER
;
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(DT_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
)
352 DBG_ADDR address
= *addr
;
354 struct datatype
* type2
= NULL
;
355 struct en_values
* e
;
356 char * def_format
= "0x%x";
359 address
.seg
= 0; /* FIXME? I don't quite get this... */
360 assert(addr
->type
!= NULL
);
362 switch(addr
->type
->type
)
365 if (!DBG_CHECK_READ_PTR( &address
, addr
->type
->un
.basic
.basic_size
))
370 memcpy(&rtn
, (char *) addr
->off
, addr
->type
->un
.basic
.basic_size
);
371 if( (addr
->type
->un
.basic
.b_signed
)
372 && ((addr
->type
->un
.basic
.basic_size
& 3) != 0)
373 && ((rtn
>> (addr
->type
->un
.basic
.basic_size
* 8 - 1)) != 0) )
375 rtn
= rtn
| ((-1) << (addr
->type
->un
.basic
.basic_size
* 8));
377 if( addr
->type
->un
.basic
.output_format
!= NULL
)
379 def_format
= addr
->type
->un
.basic
.output_format
;
383 * Check for single character prints that are out of range.
385 if( addr
->type
->un
.basic
.basic_size
== 1
386 && strcmp(def_format
, "'%c'") == 0
387 && ((rtn
< 0x20) || (rtn
> 0x80)) )
393 if (!DBG_CHECK_READ_PTR( &address
, 1 )) return 0;
394 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
395 type2
= addr
->type
->un
.pointer
.pointsto
;
396 if( type2
->type
== DT_BASIC
&& type2
->un
.basic
.basic_size
== 1 )
398 def_format
= "\"%s\"";
403 def_format
= "0x%8.8x";
408 if (!DBG_CHECK_READ_PTR( &address
, 1 )) return 0;
409 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
410 def_format
= "0x%8.8x";
413 if (!DBG_CHECK_READ_PTR( &address
, 1 )) return 0;
414 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
415 for(e
= addr
->type
->un
.enumeration
.members
; e
; e
= e
->next
)
417 if( e
->value
== rtn
)
440 *format
= def_format
;
446 DEBUG_TypeDerefPointer(DBG_ADDR
* addr
, struct datatype
** newtype
)
448 DBG_ADDR address
= *addr
;
451 * Make sure that this really makes sense.
453 if( addr
->type
->type
!= DT_POINTER
)
459 *newtype
= addr
->type
->un
.pointer
.pointsto
;
460 address
.off
= *(unsigned int*) (addr
->off
);
461 return (unsigned int)DBG_ADDR_TO_LIN(&address
); /* FIXME: is this right (or "better") ? */
465 DEBUG_FindStructElement(DBG_ADDR
* addr
, const char * ele_name
, int * tmpbuf
)
471 * Make sure that this really makes sense.
473 if( addr
->type
->type
!= DT_STRUCT
)
479 for(m
= addr
->type
->un
.structure
.members
; m
; m
= m
->next
)
481 if( strcmp(m
->name
, ele_name
) == 0 )
483 addr
->type
= m
->type
;
484 if( (m
->offset
& 7) != 0 || (m
->size
& 7) != 0)
487 * Bitfield operation. We have to extract the field and store
488 * it in a temporary buffer so that we get it all right.
490 *tmpbuf
= ((*(int* ) (addr
->off
+ (m
->offset
>> 3))) >> (m
->offset
& 7));
491 addr
->off
= (int) tmpbuf
;
493 mask
= 0xffffffff << (m
->size
);
496 * OK, now we have the correct part of the number.
497 * Check to see whether the basic type is signed or not, and if so,
498 * we need to sign extend the number.
500 if( m
->type
->type
== DT_BASIC
&& m
->type
->un
.basic
.b_signed
!= 0
501 && (*tmpbuf
& (1 << (m
->size
- 1))) != 0 )
508 addr
->off
+= (m
->offset
>> 3);
519 DEBUG_SetStructSize(struct datatype
* dt
, int size
)
521 assert(dt
->type
== DT_STRUCT
);
523 if( dt
->un
.structure
.members
!= NULL
)
528 dt
->un
.structure
.size
= size
;
529 dt
->un
.structure
.members
= NULL
;
535 DEBUG_CopyFieldlist(struct datatype
* dt
, struct datatype
* dt2
)
538 assert( dt
->type
== dt2
->type
&& ((dt
->type
== DT_STRUCT
) || (dt
->type
== DT_ENUM
)));
540 if( dt
->type
== DT_STRUCT
)
542 dt
->un
.structure
.members
= dt2
->un
.structure
.members
;
546 dt
->un
.enumeration
.members
= dt2
->un
.enumeration
.members
;
553 DEBUG_AddStructElement(struct datatype
* dt
, char * name
, struct datatype
* type
,
554 int offset
, int size
)
557 struct member
* last
;
558 struct en_values
* e
;
560 if( dt
->type
== DT_STRUCT
)
562 for(last
= dt
->un
.structure
.members
; last
; last
= last
->next
)
564 if( (last
->name
[0] == name
[0])
565 && (strcmp(last
->name
, name
) == 0) )
569 if( last
->next
== NULL
)
574 m
= (struct member
*) xmalloc(sizeof(struct member
));
580 m
->name
= xstrdup(name
);
586 m
->next
= dt
->un
.structure
.members
;
587 dt
->un
.structure
.members
= m
;
595 * If the base type is bitfield, then adjust the offsets here so that we
596 * are able to look things up without lots of falter-all.
598 if( type
->type
== DT_BITFIELD
)
600 m
->offset
+= m
->type
->un
.bitfield
.bitoff
;
601 m
->size
= m
->type
->un
.bitfield
.nbits
;
602 m
->type
= m
->type
->un
.bitfield
.basetype
;
605 else if( dt
->type
== DT_ENUM
)
607 e
= (struct en_values
*) xmalloc(sizeof(struct en_values
));
613 e
->name
= xstrdup(name
);
615 e
->next
= dt
->un
.enumeration
.members
;
616 dt
->un
.enumeration
.members
= e
;
626 DEBUG_GetPointerType(struct datatype
* dt
)
628 if( dt
->type
== DT_POINTER
)
630 return dt
->un
.pointer
.pointsto
;
637 DEBUG_SetPointerType(struct datatype
* dt
, struct datatype
* dt2
)
642 dt
->un
.pointer
.pointsto
= dt2
;
645 dt
->un
.funct
.rettype
= dt2
;
655 DEBUG_SetArrayParams(struct datatype
* dt
, int min
, int max
, struct datatype
* dt2
)
657 assert(dt
->type
== DT_ARRAY
);
658 dt
->un
.array
.start
= min
;
659 dt
->un
.array
.end
= max
;
660 dt
->un
.array
.basictype
= dt2
;
666 DEBUG_SetBitfieldParams(struct datatype
* dt
, int offset
, int nbits
,
667 struct datatype
* dt2
)
669 assert(dt
->type
== DT_BITFIELD
);
670 dt
->un
.bitfield
.bitoff
= offset
;
671 dt
->un
.bitfield
.nbits
= nbits
;
672 dt
->un
.bitfield
.basetype
= dt2
;
677 int DEBUG_GetObjectSize(struct datatype
* dt
)
687 return dt
->un
.basic
.basic_size
;
689 return sizeof(int *);
691 return dt
->un
.structure
.size
;
695 return (dt
->un
.array
.end
- dt
->un
.array
.start
)
696 * DEBUG_GetObjectSize(dt
->un
.array
.basictype
);
699 * Bitfields have to be handled seperately later on
700 * when we insert the element into the structure.
712 DEBUG_ArrayIndex(DBG_ADDR
* addr
, DBG_ADDR
* result
, int index
)
717 * Make sure that this really makes sense.
719 if( addr
->type
->type
== DT_POINTER
)
722 * Get the base type, so we know how much to index by.
724 size
= DEBUG_GetObjectSize(addr
->type
->un
.pointer
.pointsto
);
725 result
->type
= addr
->type
->un
.pointer
.pointsto
;
726 result
->off
= (*(unsigned int*) (addr
->off
)) + size
* index
;
728 else if (addr
->type
->type
== DT_ARRAY
)
730 size
= DEBUG_GetObjectSize(addr
->type
->un
.array
.basictype
);
731 result
->type
= addr
->type
->un
.array
.basictype
;
732 result
->off
= addr
->off
+ size
* (index
- addr
->type
->un
.array
.start
);
738 /***********************************************************************
741 * Implementation of the 'print' command.
744 DEBUG_Print( const DBG_ADDR
*addr
, int count
, char format
, int level
)
755 fprintf( stderr
, "Count other than 1 is meaningless in 'print' command\n" );
759 if( addr
->type
== NULL
)
761 /* No type, just print the addr value */
762 if (addr
->seg
&& (addr
->seg
!= 0xffffffff))
763 DEBUG_nchar
+= fprintf( stderr
, "0x%04lx: ", addr
->seg
);
764 DEBUG_nchar
+= fprintf( stderr
, "0x%08lx", addr
->off
);
773 if( DEBUG_nchar
> DEBUG_maxchar
)
775 fprintf(stderr
, "...");
779 if( format
== 'i' || format
== 's' || format
== 'w' || format
== 'b' )
781 fprintf( stderr
, "Format specifier '%c' is meaningless in 'print' command\n", format
);
785 switch(addr
->type
->type
)
791 DEBUG_PrintBasic(addr
, 1, format
);
794 DEBUG_nchar
+= fprintf(stderr
, "{");
795 for(m
= addr
->type
->un
.structure
.members
; m
; m
= m
->next
)
798 DEBUG_FindStructElement(&addr1
, m
->name
,
800 DEBUG_nchar
+= fprintf(stderr
, "%s=", m
->name
);
801 DEBUG_Print(&addr1
, 1, format
, level
+ 1);
802 if( m
->next
!= NULL
)
804 DEBUG_nchar
+= fprintf(stderr
, ", ");
806 if( DEBUG_nchar
> DEBUG_maxchar
)
808 fprintf(stderr
, "...}");
812 DEBUG_nchar
+= fprintf(stderr
, "}");
816 * Loop over all of the entries, printing stuff as we go.
818 size
= DEBUG_GetObjectSize(addr
->type
->un
.array
.basictype
);
822 * Special handling for character arrays.
824 pnt
= (char *) addr
->off
;
825 DEBUG_nchar
+= fprintf(stderr
, "\"");
826 for( i
=addr
->type
->un
.array
.start
; i
< addr
->type
->un
.array
.end
; i
++ )
828 fputc(*pnt
++, stderr
);
830 if( DEBUG_nchar
> DEBUG_maxchar
)
832 fprintf(stderr
, "...\"");
836 DEBUG_nchar
+= fprintf(stderr
, "\"");
840 addr1
.type
= addr
->type
->un
.array
.basictype
;
841 DEBUG_nchar
+= fprintf(stderr
, "{");
842 for( i
=addr
->type
->un
.array
.start
; i
<= addr
->type
->un
.array
.end
; i
++ )
844 DEBUG_Print(&addr1
, 1, format
, level
+ 1);
846 if( i
== addr
->type
->un
.array
.end
)
848 DEBUG_nchar
+= fprintf(stderr
, "}");
852 DEBUG_nchar
+= fprintf(stderr
, ", ");
854 if( DEBUG_nchar
> DEBUG_maxchar
)
856 fprintf(stderr
, "...}");
870 DEBUG_nchar
+= fprintf(stderr
, "\n");
878 struct datatype
* dt
= NULL
;
885 for(hash
= 0; hash
< NR_TYPE_HASH
+ 1; hash
++)
887 for( dt
= type_hash_table
[hash
]; dt
; dt
= dt
->next
)
890 if( dt
->name
!= NULL
)
897 fprintf(stderr
, "0x%p - BASIC(%s)\n",
901 fprintf(stderr
, "0x%p - POINTER(%s)(%p)\n",
902 dt
, name
, dt
->un
.pointer
.pointsto
);
905 member_name
= "none";
907 if( dt
->un
.structure
.members
!= NULL
908 && dt
->un
.structure
.members
->name
!= NULL
)
910 member_name
= dt
->un
.structure
.members
->name
;
911 for( m
= dt
->un
.structure
.members
; m
; m
= m
->next
)
916 fprintf(stderr
, "0x%p - STRUCT(%s) %d %d %s\n", dt
, name
,
917 dt
->un
.structure
.size
, nm
, member_name
);
920 fprintf(stderr
, "0x%p - ARRAY(%s)(%p)\n",
921 dt
, name
, dt
->un
.array
.basictype
);
924 fprintf(stderr
, "0x%p - ENUM(%s)\n",
928 fprintf(stderr
, "0x%p - BITFIELD(%s)\n", dt
, name
);
931 fprintf(stderr
, "0x%p - FUNC(%s)(%p)\n",
932 dt
, name
, dt
->un
.funct
.rettype
);
936 fprintf(stderr
, "What???\n");
945 enum debug_type
DEBUG_GetType(struct datatype
* dt
)
951 DEBUG_TypeCast(enum debug_type type
, const char * name
)
954 struct datatype
* rtn
;
957 * The last bucket is special, and is used to hold typeless names.
965 hash
= type_hash(name
);
968 rtn
= DEBUG_LookupDataType(type
, hash
, name
);
975 DEBUG_PrintTypeCast(struct datatype
* dt
)
980 if( dt
->name
!= NULL
)
988 fprintf(stderr
, "%s", name
);
991 DEBUG_PrintTypeCast(dt
->un
.pointer
.pointsto
);
992 fprintf(stderr
, "*");
995 fprintf(stderr
, "struct %s", name
);
998 fprintf(stderr
, "%s[]", name
);
1001 fprintf(stderr
, "enum %s", name
);
1004 fprintf(stderr
, "unsigned %s", name
);
1007 DEBUG_PrintTypeCast(dt
->un
.funct
.rettype
);
1008 fprintf(stderr
, "(*%s)()", name
);
1012 fprintf(stderr
, "What???\n");