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>
24 #define NR_TYPE_HASH 521
27 static int DEBUG_maxchar
= 1024;
31 struct en_values
* next
;
40 struct datatype
* type
;
48 struct datatype
* next
;
61 unsigned short bitoff
;
63 struct datatype
* basetype
;
68 struct datatype
* pointsto
;
72 struct datatype
* rettype
;
78 struct datatype
* basictype
;
83 struct member
* members
;
87 struct en_values
* members
;
97 #define BASIC_LONGLONG 6
98 #define BASIC_ULONGLONGI 7
100 #define BASIC_SHORTUI 9
101 #define BASIC_SCHAR 10
102 #define BASIC_UCHAR 11
104 #define BASIC_LONG_DOUBLE 13
105 #define BASIC_DOUBLE 14
106 #define BASIC_CMPLX_INT 15
107 #define BASIC_CMPLX_FLT 16
108 #define BASIC_CMPLX_DBL 17
109 #define BASIC_CMPLX_LONG_DBL 18
110 #define BASIC_VOID 19
112 struct datatype
* DEBUG_TypeInt
= NULL
;
113 struct datatype
* DEBUG_TypeIntConst
= NULL
;
114 struct datatype
* DEBUG_TypeUSInt
= NULL
;
115 struct datatype
* DEBUG_TypeString
= NULL
;
118 * All of the types that have been defined so far.
120 static struct datatype
* type_hash_table
[NR_TYPE_HASH
+ 1];
121 static struct datatype
* pointer_types
= NULL
;
123 static unsigned int type_hash( const char * name
)
125 unsigned int hash
= 0;
133 hash
= (hash
<< 4) + *p
++;
135 if( (tmp
= (hash
& 0xf0000000)) )
141 return hash
% NR_TYPE_HASH
;
145 static struct datatype
*
146 DEBUG_InitBasic(int type
, char * name
, int size
, int b_signed
,
147 char * output_format
)
151 struct datatype
* dt
;
152 dt
= (struct datatype
*) DBG_alloc(sizeof(struct datatype
));
158 hash
= type_hash(name
);
167 dt
->next
= type_hash_table
[hash
];
168 type_hash_table
[hash
] = dt
;
169 dt
->un
.basic
.basic_type
= type
;
170 dt
->un
.basic
.basic_size
= size
;
171 dt
->un
.basic
.b_signed
= b_signed
;
172 dt
->un
.basic
.output_format
= output_format
;
180 DEBUG_LookupDataType(enum debug_type xtype
, int hash
, const char * typename
)
182 struct datatype
* dt
= NULL
;
184 if( typename
!= NULL
)
186 for( dt
= type_hash_table
[hash
]; dt
; dt
= dt
->next
)
188 if( xtype
!= dt
->type
|| dt
->name
== NULL
189 || dt
->name
[0] != typename
[0])
194 if( strcmp(dt
->name
, typename
) == 0 )
205 DEBUG_NewDataType(enum debug_type xtype
, const char * typename
)
207 struct datatype
* dt
= NULL
;
211 * The last bucket is special, and is used to hold typeless names.
213 if( typename
== NULL
)
219 hash
= type_hash(typename
);
222 dt
= DEBUG_LookupDataType(xtype
, hash
, typename
);
226 dt
= (struct datatype
*) DBG_alloc(sizeof(struct datatype
));
230 memset(dt
, 0, sizeof(*dt
));
233 if( typename
!= NULL
)
235 dt
->name
= DBG_strdup(typename
);
241 if( xtype
== DT_POINTER
)
243 dt
->next
= pointer_types
;
248 dt
->next
= type_hash_table
[hash
];
249 type_hash_table
[hash
] = dt
;
258 DEBUG_FindOrMakePointerType(struct datatype
* reftype
)
260 struct datatype
* dt
= NULL
;
262 if( reftype
!= NULL
)
264 for( dt
= pointer_types
; dt
; dt
= dt
->next
)
266 if( dt
->type
!= DT_POINTER
)
271 if( dt
->un
.pointer
.pointsto
== reftype
)
280 dt
= (struct datatype
*) DBG_alloc(sizeof(struct datatype
));
284 dt
->type
= DT_POINTER
;
285 dt
->un
.pointer
.pointsto
= reftype
;
286 dt
->next
= pointer_types
;
295 DEBUG_InitTypes(void)
297 static int beenhere
= 0;
298 struct datatype
* chartype
;
300 if( beenhere
++ != 0 )
306 * Special version of int used with constants of various kinds.
308 DEBUG_TypeIntConst
= DEBUG_InitBasic(BASIC_INT
,NULL
,4,1,"%d");
311 * Initialize a few builtin types.
314 DEBUG_TypeInt
= DEBUG_InitBasic(BASIC_INT
,"int",4,1,"%d");
315 chartype
= DEBUG_InitBasic(BASIC_CHAR
,"char",1,1,"'%c'");
316 DEBUG_InitBasic(BASIC_LONG
,"long int",4,1,"%d");
317 DEBUG_TypeUSInt
= DEBUG_InitBasic(BASIC_UINT
,"unsigned int",4,0,"%d");
318 DEBUG_InitBasic(BASIC_LUI
,"long unsigned int",4,0,"%d");
319 DEBUG_InitBasic(BASIC_LONGLONG
,"long long int",8,1,"%ld");
320 DEBUG_InitBasic(BASIC_ULONGLONGI
,"long long unsigned int",8,0,"%ld");
321 DEBUG_InitBasic(BASIC_SHORT
,"short int",2,1,"%d");
322 DEBUG_InitBasic(BASIC_SHORTUI
,"short unsigned int",2,0,"%d");
323 DEBUG_InitBasic(BASIC_SCHAR
,"signed char",1,1,"'%c'");
324 DEBUG_InitBasic(BASIC_UCHAR
,"unsigned char",1,0,"'%c'");
325 DEBUG_InitBasic(BASIC_FLT
,"float",4,0,"%f");
326 DEBUG_InitBasic(BASIC_LONG_DOUBLE
,"double",8,0,"%lf");
327 DEBUG_InitBasic(BASIC_DOUBLE
,"long double",12,0,NULL
);
328 DEBUG_InitBasic(BASIC_CMPLX_INT
,"complex int",8,1,NULL
);
329 DEBUG_InitBasic(BASIC_CMPLX_FLT
,"complex float",8,0,NULL
);
330 DEBUG_InitBasic(BASIC_CMPLX_DBL
,"complex double",16,0,NULL
);
331 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL
,"complex long double",24,0,NULL
);
332 DEBUG_InitBasic(BASIC_VOID
,"void",0,0,NULL
);
334 DEBUG_TypeString
= DEBUG_NewDataType(DT_POINTER
, NULL
);
335 DEBUG_SetPointerType(DEBUG_TypeString
, chartype
);
338 * Now initialize the builtins for codeview.
340 DEBUG_InitCVDataTypes();
345 DEBUG_GetExprValue(const DBG_VALUE
*_value
, char ** format
)
348 struct datatype
* type2
= NULL
;
349 struct en_values
* e
;
350 char * def_format
= "0x%x";
351 DBG_VALUE value
= *_value
;
353 assert(_value
->cookie
== DV_TARGET
|| _value
->cookie
== DV_HOST
);
356 /* FIXME? I don't quite get this...
357 * if this is wrong, value.addr shall be linearized
360 assert(value
.type
!= NULL
);
362 switch(value
.type
->type
)
367 /* FIXME: following code implies i386 byte ordering */
368 if (_value
->cookie
== DV_TARGET
) {
369 if (!DEBUG_READ_MEM_VERBOSE((void*)value
.addr
.off
, &rtn
,
370 value
.type
->un
.basic
.basic_size
))
373 memcpy(&rtn
, (void*)value
.addr
.off
, value
.type
->un
.basic
.basic_size
);
376 if( (value
.type
->un
.basic
.b_signed
)
377 && ((value
.type
->un
.basic
.basic_size
& 3) != 0)
378 && ((rtn
>> (value
.type
->un
.basic
.basic_size
* 8 - 1)) != 0) )
380 rtn
= rtn
| ((-1) << (value
.type
->un
.basic
.basic_size
* 8));
382 if( value
.type
->un
.basic
.output_format
!= NULL
)
384 def_format
= value
.type
->un
.basic
.output_format
;
388 * Check for single character prints that are out of range.
390 if( value
.type
->un
.basic
.basic_size
== 1
391 && strcmp(def_format
, "'%c'") == 0
392 && ((rtn
< 0x20) || (rtn
> 0x80)) )
398 if (_value
->cookie
== DV_TARGET
) {
399 if (!DEBUG_READ_MEM_VERBOSE((void*)value
.addr
.off
, &rtn
, sizeof(void*)))
402 rtn
= *(unsigned int*)(value
.addr
.off
);
405 type2
= value
.type
->un
.pointer
.pointsto
;
409 def_format
= "Internal symbol error: unable to access memory location 0x%08x";
414 if( type2
->type
== DT_BASIC
&& type2
->un
.basic
.basic_size
== 1 )
416 if ( _value
->cookie
== DV_TARGET
) {
418 def_format
= "\"%S\"";
419 if (!DEBUG_READ_MEM_VERBOSE((void*)rtn
, &ch
, 1))
422 def_format
= "\"%s\"";
427 def_format
= "0x%8.8x";
432 assert(_value
->cookie
== DV_TARGET
);
433 if (!DEBUG_READ_MEM_VERBOSE((void*)value
.addr
.off
, &rtn
, sizeof(rtn
)))
435 def_format
= "0x%8.8x";
438 assert(_value
->cookie
== DV_TARGET
);
439 if (!DEBUG_READ_MEM_VERBOSE((void*)value
.addr
.off
, &rtn
, sizeof(rtn
)))
441 for(e
= value
.type
->un
.enumeration
.members
; e
; e
= e
->next
)
443 if( e
->value
== rtn
)
466 *format
= def_format
;
472 DEBUG_TypeDerefPointer(const DBG_VALUE
*value
, struct datatype
** newtype
)
474 DBG_ADDR addr
= value
->addr
;
477 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
482 * Make sure that this really makes sense.
484 if( value
->type
->type
!= DT_POINTER
)
487 if (value
->cookie
== DV_TARGET
) {
488 if (!DEBUG_READ_MEM((void*)value
->addr
.off
, &val
, sizeof(val
)))
491 val
= *(unsigned int*)value
->addr
.off
;
494 *newtype
= value
->type
->un
.pointer
.pointsto
;
496 return DEBUG_ToLinear(&addr
); /* FIXME: is this right (or "better") ? */
500 DEBUG_FindStructElement(DBG_VALUE
* value
, const char * ele_name
, int * tmpbuf
)
505 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
508 * Make sure that this really makes sense.
510 if( value
->type
->type
!= DT_STRUCT
)
516 for(m
= value
->type
->un
.structure
.members
; m
; m
= m
->next
)
518 if( strcmp(m
->name
, ele_name
) == 0 )
520 value
->type
= m
->type
;
521 if( (m
->offset
& 7) != 0 || (m
->size
& 7) != 0)
524 * Bitfield operation. We have to extract the field and store
525 * it in a temporary buffer so that we get it all right.
527 *tmpbuf
= ((*(int* ) (value
->addr
.off
+ (m
->offset
>> 3))) >> (m
->offset
& 7));
528 value
->addr
.off
= (int) tmpbuf
;
530 mask
= 0xffffffff << (m
->size
);
533 * OK, now we have the correct part of the number.
534 * Check to see whether the basic type is signed or not, and if so,
535 * we need to sign extend the number.
537 if( m
->type
->type
== DT_BASIC
&& m
->type
->un
.basic
.b_signed
!= 0
538 && (*tmpbuf
& (1 << (m
->size
- 1))) != 0 )
545 value
->addr
.off
+= (m
->offset
>> 3);
556 DEBUG_SetStructSize(struct datatype
* dt
, int size
)
558 assert(dt
->type
== DT_STRUCT
);
560 if( dt
->un
.structure
.members
!= NULL
)
565 dt
->un
.structure
.size
= size
;
566 dt
->un
.structure
.members
= NULL
;
572 DEBUG_CopyFieldlist(struct datatype
* dt
, struct datatype
* dt2
)
575 assert( dt
->type
== dt2
->type
&& ((dt
->type
== DT_STRUCT
) || (dt
->type
== DT_ENUM
)));
577 if( dt
->type
== DT_STRUCT
)
579 dt
->un
.structure
.members
= dt2
->un
.structure
.members
;
583 dt
->un
.enumeration
.members
= dt2
->un
.enumeration
.members
;
590 DEBUG_AddStructElement(struct datatype
* dt
, char * name
, struct datatype
* type
,
591 int offset
, int size
)
594 struct member
* last
;
595 struct en_values
* e
;
597 if( dt
->type
== DT_STRUCT
)
599 for(last
= dt
->un
.structure
.members
; last
; last
= last
->next
)
601 if( (last
->name
[0] == name
[0])
602 && (strcmp(last
->name
, name
) == 0) )
606 if( last
->next
== NULL
)
611 m
= (struct member
*) DBG_alloc(sizeof(struct member
));
617 m
->name
= DBG_strdup(name
);
623 m
->next
= dt
->un
.structure
.members
;
624 dt
->un
.structure
.members
= m
;
632 * If the base type is bitfield, then adjust the offsets here so that we
633 * are able to look things up without lots of falter-all.
635 if( type
&& type
->type
== DT_BITFIELD
)
637 m
->offset
+= m
->type
->un
.bitfield
.bitoff
;
638 m
->size
= m
->type
->un
.bitfield
.nbits
;
639 m
->type
= m
->type
->un
.bitfield
.basetype
;
642 else if( dt
->type
== DT_ENUM
)
644 e
= (struct en_values
*) DBG_alloc(sizeof(struct en_values
));
650 e
->name
= DBG_strdup(name
);
652 e
->next
= dt
->un
.enumeration
.members
;
653 dt
->un
.enumeration
.members
= e
;
663 DEBUG_GetPointerType(struct datatype
* dt
)
665 if( dt
->type
== DT_POINTER
)
667 return dt
->un
.pointer
.pointsto
;
674 DEBUG_SetPointerType(struct datatype
* dt
, struct datatype
* dt2
)
679 dt
->un
.pointer
.pointsto
= dt2
;
682 dt
->un
.funct
.rettype
= dt2
;
692 DEBUG_SetArrayParams(struct datatype
* dt
, int min
, int max
, struct datatype
* dt2
)
694 assert(dt
->type
== DT_ARRAY
);
695 dt
->un
.array
.start
= min
;
696 dt
->un
.array
.end
= max
;
697 dt
->un
.array
.basictype
= dt2
;
703 DEBUG_SetBitfieldParams(struct datatype
* dt
, int offset
, int nbits
,
704 struct datatype
* dt2
)
706 assert(dt
->type
== DT_BITFIELD
);
707 dt
->un
.bitfield
.bitoff
= offset
;
708 dt
->un
.bitfield
.nbits
= nbits
;
709 dt
->un
.bitfield
.basetype
= dt2
;
714 int DEBUG_GetObjectSize(struct datatype
* dt
)
724 return dt
->un
.basic
.basic_size
;
726 return sizeof(int *);
728 return dt
->un
.structure
.size
;
732 return (dt
->un
.array
.end
- dt
->un
.array
.start
)
733 * DEBUG_GetObjectSize(dt
->un
.array
.basictype
);
736 * Bitfields have to be handled seperately later on
737 * when we insert the element into the structure.
749 DEBUG_ArrayIndex(const DBG_VALUE
* value
, DBG_VALUE
* result
, int index
)
753 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
756 * Make sure that this really makes sense.
758 if( value
->type
->type
== DT_POINTER
)
761 * Get the base type, so we know how much to index by.
763 size
= DEBUG_GetObjectSize(value
->type
->un
.pointer
.pointsto
);
764 result
->type
= value
->type
->un
.pointer
.pointsto
;
765 result
->addr
.off
= (*(unsigned int*) (value
->addr
.off
)) + size
* index
;
767 else if (value
->type
->type
== DT_ARRAY
)
769 size
= DEBUG_GetObjectSize(value
->type
->un
.array
.basictype
);
770 result
->type
= value
->type
->un
.array
.basictype
;
771 result
->addr
.off
= value
->addr
.off
+ size
* (index
- value
->type
->un
.array
.start
);
777 /***********************************************************************
780 * Implementation of the 'print' command.
783 DEBUG_Print( const DBG_VALUE
*value
, int count
, char format
, int level
)
792 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
796 DEBUG_Printf( DBG_CHN_MESG
, "Count other than 1 is meaningless in 'print' command\n" );
800 if( value
->type
== NULL
)
802 /* No type, just print the addr value */
803 if (value
->addr
.seg
&& (value
->addr
.seg
!= 0xffffffff))
804 DEBUG_nchar
+= DEBUG_Printf( DBG_CHN_MESG
, "0x%04lx: ", value
->addr
.seg
);
805 DEBUG_nchar
+= DEBUG_Printf( DBG_CHN_MESG
, "0x%08lx", value
->addr
.off
);
814 if( DEBUG_nchar
> DEBUG_maxchar
)
816 DEBUG_Printf(DBG_CHN_MESG
, "...");
820 if( format
== 'i' || format
== 's' || format
== 'w' || format
== 'b' )
822 DEBUG_Printf( DBG_CHN_MESG
, "Format specifier '%c' is meaningless in 'print' command\n", format
);
826 switch(value
->type
->type
)
832 DEBUG_PrintBasic(value
, 1, format
);
835 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "{");
836 for(m
= value
->type
->un
.structure
.members
; m
; m
= m
->next
)
839 DEBUG_FindStructElement(&val1
, m
->name
, &xval
);
840 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "%s=", m
->name
);
841 DEBUG_Print(&val1
, 1, format
, level
+ 1);
842 if( m
->next
!= NULL
)
844 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, ", ");
846 if( DEBUG_nchar
> DEBUG_maxchar
)
848 DEBUG_Printf(DBG_CHN_MESG
, "...}");
852 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "}");
856 * Loop over all of the entries, printing stuff as we go.
858 size
= DEBUG_GetObjectSize(value
->type
->un
.array
.basictype
);
862 * Special handling for character arrays.
864 pnt
= (char *) value
->addr
.off
;
865 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "\"");
866 for( i
=value
->type
->un
.array
.start
; i
< value
->type
->un
.array
.end
; i
++ )
868 fputc(*pnt
++, stderr
);
870 if( DEBUG_nchar
> DEBUG_maxchar
)
872 DEBUG_Printf(DBG_CHN_MESG
, "...\"");
876 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "\"");
880 val1
.type
= value
->type
->un
.array
.basictype
;
881 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "{");
882 for( i
=value
->type
->un
.array
.start
; i
<= value
->type
->un
.array
.end
; i
++ )
884 DEBUG_Print(&val1
, 1, format
, level
+ 1);
885 val1
.addr
.off
+= size
;
886 if( i
== value
->type
->un
.array
.end
)
888 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "}");
892 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, ", ");
894 if( DEBUG_nchar
> DEBUG_maxchar
)
896 DEBUG_Printf(DBG_CHN_MESG
, "...}");
910 DEBUG_nchar
+= DEBUG_Printf(DBG_CHN_MESG
, "\n");
916 DEBUG_DumpTypes(void)
918 struct datatype
* dt
= NULL
;
925 for(hash
= 0; hash
< NR_TYPE_HASH
+ 1; hash
++)
927 for( dt
= type_hash_table
[hash
]; dt
; dt
= dt
->next
)
930 if( dt
->name
!= NULL
)
937 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - BASIC(%s)\n",
941 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - POINTER(%s)(%p)\n",
942 dt
, name
, dt
->un
.pointer
.pointsto
);
945 member_name
= "none";
947 if( dt
->un
.structure
.members
!= NULL
948 && dt
->un
.structure
.members
->name
!= NULL
)
950 member_name
= dt
->un
.structure
.members
->name
;
951 for( m
= dt
->un
.structure
.members
; m
; m
= m
->next
)
956 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - STRUCT(%s) %d %d %s\n", dt
, name
,
957 dt
->un
.structure
.size
, nm
, member_name
);
960 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - ARRAY(%s)(%p)\n",
961 dt
, name
, dt
->un
.array
.basictype
);
964 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - ENUM(%s)\n", dt
, name
);
967 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - BITFIELD(%s)\n", dt
, name
);
970 DEBUG_Printf(DBG_CHN_MESG
, "0x%p - FUNC(%s)(%p)\n",
971 dt
, name
, dt
->un
.funct
.rettype
);
975 DEBUG_Printf(DBG_CHN_MESG
, "What???\n");
984 enum debug_type
DEBUG_GetType(struct datatype
* dt
)
990 DEBUG_TypeCast(enum debug_type type
, const char * name
)
993 struct datatype
* rtn
;
996 * The last bucket is special, and is used to hold typeless names.
1000 hash
= NR_TYPE_HASH
;
1004 hash
= type_hash(name
);
1007 rtn
= DEBUG_LookupDataType(type
, hash
, name
);
1014 DEBUG_PrintTypeCast(const struct datatype
* dt
)
1016 const char* name
= "none";
1018 if( dt
->name
!= NULL
)
1026 DEBUG_Printf(DBG_CHN_MESG
, "%s", name
);
1029 DEBUG_PrintTypeCast(dt
->un
.pointer
.pointsto
);
1030 DEBUG_Printf(DBG_CHN_MESG
, "*");
1033 DEBUG_Printf(DBG_CHN_MESG
, "struct %s", name
);
1036 DEBUG_Printf(DBG_CHN_MESG
, "%s[]", name
);
1039 DEBUG_Printf(DBG_CHN_MESG
, "enum %s", name
);
1042 DEBUG_Printf(DBG_CHN_MESG
, "unsigned %s", name
);
1045 DEBUG_PrintTypeCast(dt
->un
.funct
.rettype
);
1046 DEBUG_Printf(DBG_CHN_MESG
, "(*%s)()", name
);
1050 DEBUG_Printf(DBG_CHN_MESG
, "What???\n");
1057 int DEBUG_PrintType( const DBG_VALUE
*value
)
1059 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
1063 DEBUG_Printf(DBG_CHN_MESG
, "Unknown type\n");
1066 if (!DEBUG_PrintTypeCast(value
->type
))
1068 DEBUG_Printf(DBG_CHN_MESG
, "\n");