Release 970509
[wine/multimedia.git] / debugger / types.c
blob137b662e348a2b3d28a1002491b188998700e3f2
1 /*
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.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
13 #include <assert.h>
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <unistd.h>
21 #include "win.h"
22 #include "pe_image.h"
23 #include "peexe.h"
24 #include "debugger.h"
25 #include "peexe.h"
26 #include "xmalloc.h"
28 #define NR_TYPE_HASH 521
30 int DEBUG_nchar;
31 static int DEBUG_maxchar = 1024;
33 struct en_values
35 struct en_values* next;
36 char * name;
37 int value;
40 struct member
42 struct member * next;
43 char * name;
44 struct datatype * type;
45 int offset;
46 int size;
49 struct datatype
51 enum debug_type type;
52 struct datatype * next;
53 char * name;
54 union
56 struct
58 char basic_type;
59 char * output_format;
60 char basic_size;
61 unsigned b_signed:1;
62 } basic;
63 struct
65 unsigned short bitoff;
66 unsigned short nbits;
67 struct datatype * basetype;
68 } bitfield;
70 struct
72 struct datatype * pointsto;
73 } pointer;
74 struct
76 struct datatype * rettype;
77 } funct;
78 struct
80 int start;
81 int end;
82 struct datatype * basictype;
83 } array;
84 struct
86 int size;
87 struct member * members;
88 } structure;
89 struct
91 struct en_values * members;
92 } enumeration;
93 } un;
96 #define BASIC_INT 1
97 #define BASIC_CHAR 2
98 #define BASIC_LONG 3
99 #define BASIC_UINT 4
100 #define BASIC_LUI 5
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
107 #define BASIC_FLT 12
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;
130 unsigned int tmp;
131 const char * p;
133 p = name;
135 while (*p)
137 hash = (hash << 4) + *p++;
139 if( (tmp = (hash & 0xf0000000)) )
141 hash ^= tmp >> 24;
143 hash &= ~tmp;
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)
153 int hash;
155 struct datatype * dt;
156 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
158 if( dt != NULL )
160 if( name != NULL )
162 hash = type_hash(name);
164 else
166 hash = NR_TYPE_HASH;
169 dt->type = BASIC;
170 dt->name = 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;
179 return dt;
182 static
183 struct datatype *
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])
195 continue;
198 if( strcmp(dt->name, typename) == 0 )
200 return dt;
205 return dt;
208 struct datatype *
209 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
211 struct datatype * dt = NULL;
212 int hash;
215 * The last bucket is special, and is used to hold typeless names.
217 if( typename == NULL )
219 hash = NR_TYPE_HASH;
221 else
223 hash = type_hash(typename);
226 dt = DEBUG_LookupDataType(xtype, hash, typename);
228 if( dt == NULL )
230 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
232 if( dt != NULL )
234 memset(dt, 0, sizeof(*dt));
236 dt->type = xtype;
237 if( typename != NULL )
239 dt->name = xstrdup(typename);
241 else
243 dt->name = NULL;
245 if( xtype == POINTER )
247 dt->next = pointer_types;
248 pointer_types = dt;
250 else
252 dt->next = type_hash_table[hash];
253 type_hash_table[hash] = dt;
258 return dt;
261 struct datatype *
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 )
272 continue;
275 if( dt->un.pointer.pointsto == reftype )
277 return dt;
282 if( dt == NULL )
284 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
286 if( dt != NULL )
288 dt->type = POINTER;
289 dt->un.pointer.pointsto = reftype;
290 dt->next = pointer_types;
291 pointer_types = dt;
295 return dt;
298 void
299 DEBUG_InitTypes()
301 static int beenhere = 0;
302 struct datatype * chartype;
304 if( beenhere++ != 0 )
306 return;
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();
349 long long int
350 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
352 unsigned int rtn;
353 struct datatype * type2 = NULL;
354 struct en_values * e;
355 char * def_format = "0x%x";
357 rtn = 0;
358 assert(addr->type != NULL);
360 switch(addr->type->type)
362 case BASIC:
363 if (!DBG_CHECK_READ_PTR( addr, addr->type->un.basic.basic_size))
365 return 0;
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)) )
387 def_format = "%d";
389 break;
390 case POINTER:
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\"";
397 break;
399 else
401 def_format = "0x%8.8x";
403 break;
404 case ARRAY:
405 case STRUCT:
406 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
407 rtn = (unsigned int) *((unsigned char **)addr->off);
408 def_format = "0x%8.8x";
409 break;
410 case ENUM:
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 )
416 break;
419 if( e != NULL )
421 rtn = (int) e->name;
422 def_format = "%s";
424 else
426 def_format = "%d";
428 break;
429 default:
430 rtn = 0;
431 break;
435 if( format != NULL )
437 *format = def_format;
439 return rtn;
442 unsigned int
443 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
446 * Make sure that this really makes sense.
448 if( addr->type->type != POINTER )
450 *newtype = NULL;
451 return 0;
454 *newtype = addr->type->un.pointer.pointsto;
455 return *(unsigned int*) (addr->off);
458 unsigned int
459 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
461 struct member * m;
462 unsigned int mask;
465 * Make sure that this really makes sense.
467 if( addr->type->type != STRUCT )
469 addr->type = NULL;
470 return FALSE;
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);
488 *tmpbuf &= ~mask;
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 )
497 *tmpbuf |= mask;
500 else
502 addr->off += (m->offset >> 3);
504 return TRUE;
508 addr->type = NULL;
509 return FALSE;
513 DEBUG_SetStructSize(struct datatype * dt, int size)
515 assert(dt->type == STRUCT);
517 if( dt->un.structure.members != NULL )
519 return FALSE;
522 dt->un.structure.size = size;
523 dt->un.structure.members = NULL;
525 return TRUE;
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;
538 else
540 dt->un.enumeration.members = dt2->un.enumeration.members;
543 return TRUE;
547 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
548 int offset, int size)
550 struct member * m;
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) )
561 return TRUE;
563 if( last->next == NULL )
565 break;
568 m = (struct member *) xmalloc(sizeof(struct member));
569 if( m == FALSE )
571 return FALSE;
574 m->name = xstrdup(name);
575 m->type = type;
576 m->offset = offset;
577 m->size = size;
578 if( last == NULL )
580 m->next = dt->un.structure.members;
581 dt->un.structure.members = m;
583 else
585 last->next = m;
586 m->next = NULL;
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));
602 if( e == FALSE )
604 return FALSE;
607 e->name = xstrdup(name);
608 e->value = offset;
609 e->next = dt->un.enumeration.members;
610 dt->un.enumeration.members = e;
612 else
614 assert(FALSE);
616 return TRUE;
619 struct datatype *
620 DEBUG_GetPointerType(struct datatype * dt)
622 if( dt->type == POINTER )
624 return dt->un.pointer.pointsto;
627 return NULL;
631 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
633 switch(dt->type)
635 case POINTER:
636 dt->un.pointer.pointsto = dt2;
637 break;
638 case FUNC:
639 dt->un.funct.rettype = dt2;
640 break;
641 default:
642 assert(FALSE);
645 return TRUE;
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;
656 return TRUE;
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;
668 return TRUE;
671 int DEBUG_GetObjectSize(struct datatype * dt)
673 if( dt == NULL )
675 return 0;
678 switch(dt->type)
680 case BASIC:
681 return dt->un.basic.basic_size;
682 case POINTER:
683 return sizeof(int *);
684 case STRUCT:
685 return dt->un.structure.size;
686 case ENUM:
687 return sizeof(int);
688 case ARRAY:
689 return (dt->un.array.end - dt->un.array.start)
690 * DEBUG_GetObjectSize(dt->un.array.basictype);
691 case BITFIELD:
693 * Bitfields have to be handled seperately later on
694 * when we insert the element into the structure.
696 return 0;
697 case TYPEDEF:
698 case FUNC:
699 case CONST:
700 assert(FALSE);
702 return 0;
705 unsigned int
706 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
708 int size;
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);
729 return TRUE;
732 /***********************************************************************
733 * DEBUG_Print
735 * Implementation of the 'print' command.
737 void
738 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
740 DBG_ADDR addr1;
741 int i;
742 struct member * m;
743 char * pnt;
744 int size;
745 long long int value;
747 if (count != 1)
749 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
750 return;
753 if( addr->type == NULL )
755 fprintf(stderr, "Unable to evaluate expression\n");
756 goto leave;
759 if( level == 0 )
761 DEBUG_nchar = 0;
764 if( DEBUG_nchar > DEBUG_maxchar )
766 fprintf(stderr, "...");
767 goto leave;
770 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
772 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
773 format = '\0';
776 switch(addr->type->type)
778 case BASIC:
779 case ENUM:
780 case CONST:
781 case POINTER:
782 DEBUG_PrintBasic(addr, 1, format);
783 break;
784 case STRUCT:
785 DEBUG_nchar += fprintf(stderr, "{");
786 for(m = addr->type->un.structure.members; m; m = m->next)
788 addr1 = *addr;
789 DEBUG_FindStructElement(&addr1, m->name,
790 (int *) &value);
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, "...}");
800 goto leave;
803 DEBUG_nchar += fprintf(stderr, "}");
804 break;
805 case ARRAY:
807 * Loop over all of the entries, printing stuff as we go.
809 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
810 if( size == 1 )
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);
820 DEBUG_nchar++;
821 if( DEBUG_nchar > DEBUG_maxchar )
823 fprintf(stderr, "...\"");
824 goto leave;
827 DEBUG_nchar += fprintf(stderr, "\"");
828 break;
830 addr1 = *addr;
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);
836 addr1.off += size;
837 if( i == addr->type->un.array.end )
839 DEBUG_nchar += fprintf(stderr, "}");
841 else
843 DEBUG_nchar += fprintf(stderr, ", ");
845 if( DEBUG_nchar > DEBUG_maxchar )
847 fprintf(stderr, "...}");
848 goto leave;
851 break;
852 default:
853 assert(FALSE);
854 break;
857 leave:
859 if( level == 0 )
861 DEBUG_nchar += fprintf(stderr, "\n");
863 return;
867 DEBUG_DumpTypes()
869 struct datatype * dt = NULL;
870 struct member * m;
871 int hash;
872 int nm;
873 char * name;
874 char * member_name;
876 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
878 for( dt = type_hash_table[hash]; dt; dt = dt->next )
880 name = "none";
881 if( dt->name != NULL )
883 name = dt->name;
885 switch(dt->type)
887 case BASIC:
888 fprintf(stderr, "0x%p - BASIC(%s)\n",
889 dt, name);
890 break;
891 case POINTER:
892 fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
893 dt, name, dt->un.pointer.pointsto);
894 break;
895 case STRUCT:
896 member_name = "none";
897 nm = 0;
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)
904 nm++;
907 fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
908 dt->un.structure.size, nm, member_name);
909 break;
910 case ARRAY:
911 fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
912 dt, name, dt->un.array.basictype);
913 break;
914 case ENUM:
915 fprintf(stderr, "0x%p - ENUM(%s)\n",
916 dt, name);
917 break;
918 case BITFIELD:
919 fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
920 break;
921 case FUNC:
922 fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
923 dt, name, dt->un.funct.rettype);
924 break;
925 case CONST:
926 case TYPEDEF:
927 fprintf(stderr, "What???\n");
928 break;
932 return TRUE;
936 enum debug_type DEBUG_GetType(struct datatype * dt)
938 return dt->type;
941 struct datatype *
942 DEBUG_TypeCast(enum debug_type type, const char * name)
944 int hash;
945 struct datatype * rtn;
948 * The last bucket is special, and is used to hold typeless names.
950 if( name == NULL )
952 hash = NR_TYPE_HASH;
954 else
956 hash = type_hash(name);
959 rtn = DEBUG_LookupDataType(type, hash, name);
961 return rtn;
966 DEBUG_PrintTypeCast(struct datatype * dt)
968 char * name;
970 name = "none";
971 if( dt->name != NULL )
973 name = dt->name;
976 switch(dt->type)
978 case BASIC:
979 fprintf(stderr, "%s", name);
980 break;
981 case POINTER:
982 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
983 fprintf(stderr, "*");
984 break;
985 case STRUCT:
986 fprintf(stderr, "struct %s", name);
987 break;
988 case ARRAY:
989 fprintf(stderr, "%s[]", name);
990 break;
991 case ENUM:
992 fprintf(stderr, "enum %s", name);
993 break;
994 case BITFIELD:
995 fprintf(stderr, "unsigned %s", name);
996 break;
997 case FUNC:
998 DEBUG_PrintTypeCast(dt->un.funct.rettype);
999 fprintf(stderr, "(*%s)()", name);
1000 break;
1001 case CONST:
1002 case TYPEDEF:
1003 fprintf(stderr, "What???\n");
1004 break;
1007 return TRUE;