Release 970120
[wine] / debugger / types.c
1 /*
2  * File types.c - datatype handling stuff for internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
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  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <assert.h>
14 #ifndef __EMX__
15 #include <sys/mman.h>
16 #endif
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <limits.h>
21 #include <strings.h>
22 #include <unistd.h>
23 #include <malloc.h>
24
25 #include "win.h"
26 #include "pe_image.h"
27 #include "peexe.h"
28 #include "debugger.h"
29 #include "peexe.h"
30 #include "xmalloc.h"
31
32 #define NR_TYPE_HASH 521
33
34 int               DEBUG_nchar;
35 static int        DEBUG_maxchar = 1024;
36
37 struct en_values
38 {
39   struct en_values* next;
40   char            * name;
41   int               value;
42 };
43
44 struct member
45 {
46   struct member   * next;
47   char            * name;
48   struct datatype * type;
49   int               offset;
50   int               size;
51 };
52
53 struct datatype
54 {
55   enum  debug_type type;
56   struct datatype * next;
57   char * name;
58   union
59   {
60     struct
61     {
62       char    basic_type;
63       char  * output_format;
64       char    basic_size;
65       unsigned b_signed:1;
66     } basic;
67     struct
68     {
69       unsigned short bitoff;
70       unsigned short nbits;
71       struct datatype * basetype;
72     } bitfield;
73
74     struct
75     {
76       struct datatype * pointsto;
77     } pointer;
78     struct
79     {
80       struct datatype * rettype;
81     } funct;
82     struct
83     {
84       int               start;
85       int               end;
86       struct datatype * basictype;
87     } array;
88     struct
89     {
90       int               size;
91       struct member * members;
92     } structure;
93     struct
94     {
95       struct en_values * members;
96     } enumeration;
97   } un;
98 };
99
100 #define BASIC_INT               1
101 #define BASIC_CHAR              2
102 #define BASIC_LONG              3
103 #define BASIC_UINT              4
104 #define BASIC_LUI               5
105 #define BASIC_LONGLONG          6
106 #define BASIC_ULONGLONGI        7
107 #define BASIC_SHORT             8
108 #define BASIC_SHORTUI           9
109 #define BASIC_SCHAR             10
110 #define BASIC_UCHAR             11
111 #define BASIC_FLT               12
112 #define BASIC_LONG_DOUBLE       13
113 #define BASIC_DOUBLE            14
114 #define BASIC_CMPLX_INT         15
115 #define BASIC_CMPLX_FLT         16
116 #define BASIC_CMPLX_DBL         17
117 #define BASIC_CMPLX_LONG_DBL    18
118 #define BASIC_VOID              19
119
120 struct datatype * DEBUG_TypeInt = NULL;
121 struct datatype * DEBUG_TypeIntConst = NULL;
122 struct datatype * DEBUG_TypeUSInt = NULL;
123 struct datatype * DEBUG_TypeString = NULL;
124
125 /*
126  * All of the types that have been defined so far.
127  */
128 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
129 static struct datatype * pointer_types = NULL;
130
131 static unsigned int type_hash( const char * name )
132 {
133     unsigned int hash = 0;
134     unsigned int tmp;
135     const char * p;
136
137     p = name;
138
139     while (*p) 
140       {
141         hash = (hash << 4) + *p++;
142
143         if( (tmp = (hash & 0xf0000000)) )
144           {
145             hash ^= tmp >> 24;
146           }
147         hash &= ~tmp;
148       }
149     return hash % NR_TYPE_HASH;
150 }
151
152
153 static struct datatype *
154 DEBUG_InitBasic(int type, char * name, int size, int b_signed, 
155                             char * output_format)
156 {
157   int hash;
158
159   struct datatype * dt;
160   dt = (struct datatype *) xmalloc(sizeof(struct datatype));
161
162   if( dt != NULL )
163     {
164       if( name != NULL )
165         {
166           hash = type_hash(name);
167         }
168       else
169         {
170           hash = NR_TYPE_HASH;
171         }
172
173       dt->type = BASIC;
174       dt->name = name;
175       dt->next = type_hash_table[hash];
176       type_hash_table[hash] = dt;
177       dt->un.basic.basic_type = type;
178       dt->un.basic.basic_size = size;
179       dt->un.basic.b_signed = b_signed;
180       dt->un.basic.output_format = output_format;
181     }
182
183   return dt;
184 }
185
186 static
187 struct datatype *
188 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
189 {
190   struct datatype * dt = NULL;
191
192   if( typename != NULL )
193     {
194       for( dt = type_hash_table[hash]; dt; dt = dt->next )
195         {
196           if( xtype != dt->type || dt->name == NULL 
197               || dt->name[0] != typename[0])
198             {
199               continue;
200             }
201                   
202           if( strcmp(dt->name, typename) == 0 )
203             {
204               return dt;
205             }
206         }
207     }
208
209   return dt;
210 }
211
212 struct datatype *
213 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
214 {
215   struct datatype * dt = NULL;
216   int hash;
217
218   /*
219    * The last bucket is special, and is used to hold typeless names.
220    */
221   if( typename == NULL )
222     {
223       hash = NR_TYPE_HASH;
224     }
225   else
226     {
227       hash = type_hash(typename);
228     }
229
230   dt = DEBUG_LookupDataType(xtype, hash, typename);
231
232   if( dt == NULL )
233     {
234       dt = (struct datatype *) xmalloc(sizeof(struct datatype));
235       
236       if( dt != NULL )
237         {
238           memset(dt, 0, sizeof(*dt));
239       
240           dt->type = xtype;
241           if( typename != NULL )
242             {
243               dt->name = xstrdup(typename);
244             }
245           else
246             {
247               dt->name = NULL;
248             }
249           if( xtype == POINTER )
250             {
251               dt->next = pointer_types;
252               pointer_types = dt;
253             }
254           else
255             {
256               dt->next = type_hash_table[hash];
257               type_hash_table[hash] = dt;
258             }
259         }
260     }
261
262   return dt;
263 }
264
265 struct datatype *
266 DEBUG_FindOrMakePointerType(struct datatype * reftype)
267 {
268   struct datatype * dt = NULL;
269
270   if( reftype != NULL )
271     {
272       for( dt = pointer_types; dt; dt = dt->next )
273         {
274           if( dt->type != POINTER )
275             {
276               continue;
277             }
278                   
279           if( dt->un.pointer.pointsto == reftype )
280             {
281               return dt;
282             }
283         }
284     }
285
286   if( dt == NULL )
287     {
288       dt = (struct datatype *) xmalloc(sizeof(struct datatype));
289       
290       if( dt != NULL )
291         {
292           dt->type = POINTER;
293           dt->un.pointer.pointsto = reftype;
294           dt->next = pointer_types;
295           pointer_types = dt;
296         }
297     }
298
299   return dt;
300 }
301
302 void
303 DEBUG_InitTypes()
304 {
305   static int beenhere = 0;
306   struct datatype * chartype;
307
308   if( beenhere++ != 0 )
309     {
310       return;
311     }
312
313   /*
314    * Special version of int used with constants of various kinds.
315    */
316   DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
317
318   /*
319    * Initialize a few builtin types.
320    */
321
322
323   DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
324   chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
325   DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
326   DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
327   DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
328   DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
329   DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
330   DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
331   DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
332   DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
333   DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
334   DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
335   DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
336   DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
337   DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
338   DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
339   DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
340   DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
341   DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
342
343   DEBUG_TypeString = DEBUG_NewDataType(POINTER, NULL);
344   DEBUG_SetPointerType(DEBUG_TypeString, chartype);
345
346   /*
347    * Now initialize the builtins for codeview.
348    */
349   DEBUG_InitCVDataTypes();
350
351 }
352
353 long long int
354 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
355 {
356   unsigned int rtn;
357   struct datatype * type2 = NULL;
358   struct en_values * e;
359   char * def_format = "0x%x";
360
361   rtn = 0;
362   assert(addr->type != NULL);
363
364   switch(addr->type->type)
365     {
366     case BASIC:
367       if (!DBG_CHECK_READ_PTR( addr,  addr->type->un.basic.basic_size)) 
368         {
369           return 0;
370         }
371
372       memcpy(&rtn, (char *) addr->off, addr->type->un.basic.basic_size);
373       if(    (addr->type->un.basic.b_signed)
374           && ((addr->type->un.basic.basic_size & 3) != 0)
375           && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
376         {
377           rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
378         }
379       if( addr->type->un.basic.output_format != NULL )
380         {
381           def_format = addr->type->un.basic.output_format;
382         }
383
384       /*
385        * Check for single character prints that are out of range.
386        */
387       if( addr->type->un.basic.basic_size == 1
388           && strcmp(def_format, "'%c'") == 0 
389           && ((rtn < 0x20) || (rtn > 0x80)) )
390         {
391           def_format = "%d";
392         }
393       break;
394     case POINTER:
395       if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
396       rtn = (unsigned int)  *((unsigned char **)addr->off);
397       type2 = addr->type->un.pointer.pointsto;
398       if( type2->type == BASIC && type2->un.basic.basic_size == 1 )
399         {
400           def_format = "\"%s\"";
401           break;
402         }
403       else
404         {
405           def_format = "0x%8.8x";
406         }
407       break;
408     case ARRAY:
409     case STRUCT:
410       if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
411       rtn = (unsigned int)  *((unsigned char **)addr->off);
412       def_format = "0x%8.8x";
413       break;
414     case ENUM:
415       rtn = (unsigned int)  *((unsigned char **)addr->off);
416       for(e = addr->type->un.enumeration.members; e; e = e->next )
417         {
418           if( e->value == rtn )
419             {
420               break;
421             }
422         }
423       if( e != NULL )
424         {
425           rtn = (int) e->name;
426           def_format = "%s";
427         }
428       else
429         {
430           def_format = "%d";
431         }
432       break;
433     default:
434       rtn = 0;
435       break;
436     }
437
438
439   if( format != NULL )
440     {
441       *format = def_format;
442     }
443   return rtn;
444 }
445
446 unsigned int
447 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
448 {
449   /*
450    * Make sure that this really makes sense.
451    */
452   if( addr->type->type != POINTER )
453     {
454       *newtype = NULL;
455       return 0;
456     }
457
458   *newtype = addr->type->un.pointer.pointsto;
459   return *(unsigned int*) (addr->off);
460 }
461
462 unsigned int
463 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
464 {
465   struct member * m;
466   unsigned int    mask;
467
468   /*
469    * Make sure that this really makes sense.
470    */
471   if( addr->type->type != STRUCT )
472     {
473       addr->type = NULL;
474       return FALSE;
475     }
476
477   for(m = addr->type->un.structure.members; m; m = m->next)
478     {
479       if( strcmp(m->name, ele_name) == 0 )
480         {
481           addr->type = m->type;
482           if( (m->offset & 7) != 0 || (m->size & 7) != 0)
483             {
484               /*
485                * Bitfield operation.  We have to extract the field and store
486                * it in a temporary buffer so that we get it all right.
487                */
488               *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
489               addr->off = (int) tmpbuf;
490
491               mask = 0xffffffff << (m->size);
492               *tmpbuf &= ~mask;
493               /*
494                * OK, now we have the correct part of the number.
495                * Check to see whether the basic type is signed or not, and if so,
496                * we need to sign extend the number.
497                */
498               if( m->type->type == BASIC && m->type->un.basic.b_signed != 0
499                   && (*tmpbuf & (1 << (m->size - 1))) != 0 )
500                 {
501                   *tmpbuf |= mask;
502                 }
503             }
504           else
505             {
506               addr->off += (m->offset >> 3);
507             }
508           return TRUE;
509         }
510     }
511
512   addr->type = NULL;
513   return FALSE;
514 }
515
516 int
517 DEBUG_SetStructSize(struct datatype * dt, int size)
518 {
519   assert(dt->type == STRUCT);
520
521   if( dt->un.structure.members != NULL )
522     {
523       return FALSE;
524     }
525
526   dt->un.structure.size = size;
527   dt->un.structure.members = NULL;
528
529   return TRUE;
530 }
531
532 int
533 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
534 {
535
536   assert( dt->type == dt2->type && ((dt->type == STRUCT) || (dt->type == ENUM)));
537
538   if( dt->type == STRUCT )
539     {
540       dt->un.structure.members = dt2->un.structure.members;
541     }
542   else
543     {
544       dt->un.enumeration.members = dt2->un.enumeration.members;
545     }
546
547   return TRUE;
548 }
549
550 int
551 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type, 
552                        int offset, int size)
553 {
554   struct member * m;
555   struct member * last;
556   struct en_values * e;
557
558   if( dt->type == STRUCT )
559     {
560       for(last = dt->un.structure.members; last; last = last->next)
561         {
562           if(    (last->name[0] == name[0]) 
563               && (strcmp(last->name, name) == 0) )
564             {
565               return TRUE;
566             }
567           if( last->next == NULL )
568             {
569               break;
570             }
571         }
572       m = (struct member *) xmalloc(sizeof(struct member));
573       if( m == FALSE )
574         {
575           return FALSE;
576         }
577       
578       m->name = xstrdup(name);
579       m->type = type;
580       m->offset = offset;
581       m->size = size;
582       if( last == NULL )
583         {
584           m->next = dt->un.structure.members;
585           dt->un.structure.members = m;
586         }
587       else
588         {
589           last->next = m;
590           m->next = NULL;
591         }
592       /*
593        * If the base type is bitfield, then adjust the offsets here so that we
594        * are able to look things up without lots of falter-all.
595        */
596       if( type->type == BITFIELD )
597         {
598           m->offset += m->type->un.bitfield.bitoff;
599           m->size = m->type->un.bitfield.nbits;
600           m->type = m->type->un.bitfield.basetype;
601         }
602     }
603   else if( dt->type == ENUM )
604     {
605       e = (struct en_values *) xmalloc(sizeof(struct en_values));
606       if( e == FALSE )
607         {
608           return FALSE;
609         }
610       
611       e->name = xstrdup(name);
612       e->value = offset;
613       e->next = dt->un.enumeration.members;
614       dt->un.enumeration.members = e;
615     }
616   else
617     {
618       assert(FALSE);
619     }
620   return TRUE;
621 }
622
623 struct datatype * 
624 DEBUG_GetPointerType(struct datatype * dt)
625 {
626   if( dt->type == POINTER )
627     {
628       return dt->un.pointer.pointsto;
629     }
630
631   return NULL;
632 }
633
634 int
635 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
636 {
637   switch(dt->type)
638     {
639     case POINTER:
640       dt->un.pointer.pointsto = dt2;
641       break;
642     case FUNC:
643       dt->un.funct.rettype = dt2;
644       break;
645     default:
646       assert(FALSE);
647     }
648
649   return TRUE;
650 }
651
652 int
653 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
654 {
655   assert(dt->type == ARRAY);
656   dt->un.array.start = min;
657   dt->un.array.end   = max;
658   dt->un.array.basictype = dt2;
659
660   return TRUE;
661 }
662
663 int
664 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits, 
665                         struct datatype * dt2)
666 {
667   assert(dt->type == BITFIELD);
668   dt->un.bitfield.bitoff   = offset;
669   dt->un.bitfield.nbits    = nbits;
670   dt->un.bitfield.basetype = dt2;
671
672   return TRUE;
673 }
674
675 int DEBUG_GetObjectSize(struct datatype * dt)
676 {
677   if( dt == NULL )
678     {
679       return 0;
680     }
681
682   switch(dt->type)
683     {
684     case BASIC:
685       return dt->un.basic.basic_size;
686     case POINTER:
687       return sizeof(int *);
688     case STRUCT:
689       return dt->un.structure.size;
690     case ENUM:
691       return sizeof(int);
692     case ARRAY:
693       return (dt->un.array.end - dt->un.array.start) 
694         * DEBUG_GetObjectSize(dt->un.array.basictype);
695     case BITFIELD:
696       /*
697        * Bitfields have to be handled seperately later on
698        * when we insert the element into the structure.
699        */
700       return 0;
701     case TYPEDEF:
702     case FUNC:
703     case CONST:
704       assert(FALSE);
705     }
706   return 0;
707 }
708
709 unsigned int
710 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
711 {
712   int size;
713
714   /*
715    * Make sure that this really makes sense.
716    */
717   if( addr->type->type == POINTER )
718     {
719       /*
720        * Get the base type, so we know how much to index by.
721        */
722       size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
723       result->type = addr->type->un.pointer.pointsto;
724       result->off = (*(unsigned int*) (addr->off)) + size * index;
725     }
726   else if (addr->type->type == ARRAY)
727     {
728       size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
729       result->type = addr->type->un.array.basictype;
730       result->off = addr->off + size * (index - addr->type->un.array.start);
731     }
732
733   return TRUE;
734 }
735
736 /***********************************************************************
737  *           DEBUG_Print
738  *
739  * Implementation of the 'print' command.
740  */
741 void
742 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
743 {
744   DBG_ADDR        addr1;
745   int             i;
746   struct member * m;
747   char          * pnt;
748   int             size;
749   long long int   value;
750
751   if (count != 1)
752     {
753       fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
754       return;
755     }
756   
757   if( addr->type == NULL )
758     {
759       fprintf(stderr, "Unable to evaluate expression\n");
760       goto leave;
761     }
762   
763   if( level == 0 )
764     {
765       DEBUG_nchar = 0;
766     }
767
768   if( DEBUG_nchar > DEBUG_maxchar )
769     {
770       fprintf(stderr, "...");
771       goto leave;
772     }
773
774   if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
775     {
776       fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
777       format = '\0';
778     }
779
780   switch(addr->type->type)
781     {
782     case BASIC:
783     case ENUM:
784     case CONST:
785     case POINTER:
786       DEBUG_PrintBasic(addr, 1, format);
787       break;
788     case STRUCT:
789       DEBUG_nchar += fprintf(stderr, "{");
790       for(m = addr->type->un.structure.members; m; m = m->next)
791         {
792           addr1 = *addr;
793           DEBUG_FindStructElement(&addr1, m->name,
794                                   (int *) &value);
795           DEBUG_nchar += fprintf(stderr, "%s=", m->name);
796           DEBUG_Print(&addr1, 1, format, level + 1);
797           if( m->next != NULL )
798             {
799               DEBUG_nchar += fprintf(stderr, ", ");
800             }
801           if( DEBUG_nchar > DEBUG_maxchar )
802             {
803               fprintf(stderr, "...}");
804               goto leave;
805             }
806         }
807       DEBUG_nchar += fprintf(stderr, "}");
808       break;
809     case ARRAY:
810       /*
811        * Loop over all of the entries, printing stuff as we go.
812        */
813       size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
814       if( size == 1 )
815         {
816           /*
817            * Special handling for character arrays.
818            */
819           pnt = (char *) addr->off;
820           DEBUG_nchar += fprintf(stderr, "\"");
821           for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
822             {
823               fputc(*pnt++, stderr);
824               DEBUG_nchar++;
825               if( DEBUG_nchar > DEBUG_maxchar )
826                 {
827                   fprintf(stderr, "...\"");
828                   goto leave;
829                 }
830             }
831           DEBUG_nchar += fprintf(stderr, "\"");
832           break;
833         }
834       addr1 = *addr;
835       addr1.type = addr->type->un.array.basictype;
836       DEBUG_nchar += fprintf(stderr, "{");
837       for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
838         {
839           DEBUG_Print(&addr1, 1, format, level + 1);
840           addr1.off += size;
841           if( i == addr->type->un.array.end )
842             {
843               DEBUG_nchar += fprintf(stderr, "}");
844             }
845           else
846             {
847               DEBUG_nchar += fprintf(stderr, ", ");
848             }
849           if( DEBUG_nchar > DEBUG_maxchar )
850             {
851               fprintf(stderr, "...}");
852               goto leave;
853             }
854         }
855       break;
856     default:
857       assert(FALSE);
858       break;
859     }
860
861 leave:
862
863   if( level == 0 )
864     {
865       DEBUG_nchar += fprintf(stderr, "\n");
866     }
867   return;
868 }
869
870 int
871 DEBUG_DumpTypes()
872 {
873   struct datatype * dt = NULL;
874   struct member * m;
875   int hash;
876   int nm;
877   char * name;
878   char * member_name;
879
880   for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
881     {
882       for( dt = type_hash_table[hash]; dt; dt = dt->next )
883         {
884           name =  "none";
885           if( dt->name != NULL )
886             {
887               name = dt->name;
888             }
889           switch(dt->type)
890             {
891             case BASIC:
892               fprintf(stderr, "0x%p - BASIC(%s)\n",
893                       dt, name);
894               break;
895             case POINTER:
896               fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
897                       dt, name, dt->un.pointer.pointsto);
898               break;
899             case STRUCT:
900               member_name = "none";
901               nm = 0;
902               if( dt->un.structure.members != NULL
903                   && dt->un.structure.members->name != NULL )
904                 {
905                   member_name = dt->un.structure.members->name;
906                   for( m = dt->un.structure.members; m; m = m->next)
907                     {
908                       nm++;
909                     }
910                 }
911               fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
912                       dt->un.structure.size, nm, member_name);
913               break;
914             case ARRAY:
915               fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
916                       dt, name, dt->un.array.basictype);
917               break;
918             case ENUM:
919               fprintf(stderr, "0x%p - ENUM(%s)\n",
920                       dt, name);
921               break;
922             case BITFIELD:
923               fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
924               break;
925             case FUNC:
926               fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
927                       dt, name, dt->un.funct.rettype);
928               break;
929             case CONST:
930             case TYPEDEF:
931               fprintf(stderr, "What???\n");
932               break;
933             }
934         }
935     }
936   return TRUE;
937 }
938
939
940 enum debug_type DEBUG_GetType(struct datatype * dt)
941 {
942   return dt->type;
943 }
944
945 struct datatype *
946 DEBUG_TypeCast(enum debug_type type, const char * name)
947 {
948   int                     hash;
949   struct datatype       * rtn;
950
951   /*
952    * The last bucket is special, and is used to hold typeless names.
953    */
954   if( name == NULL )
955     {
956       hash = NR_TYPE_HASH;
957     }
958   else
959     {
960       hash = type_hash(name);
961     }
962
963   rtn = DEBUG_LookupDataType(type, hash, name);
964
965   return rtn;
966
967 }
968
969 int
970 DEBUG_PrintTypeCast(struct datatype * dt)
971 {
972   char          * name;
973
974   name =  "none";
975   if( dt->name != NULL )
976     {
977       name = dt->name;
978     }
979
980   switch(dt->type)
981     {
982     case BASIC:
983       fprintf(stderr, "%s", name);
984       break;
985     case POINTER:
986       DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
987       fprintf(stderr, "*");
988       break;
989     case STRUCT:
990       fprintf(stderr, "struct %s", name);
991       break;
992     case ARRAY:
993       fprintf(stderr, "%s[]", name);
994       break;
995     case ENUM:
996       fprintf(stderr, "enum %s", name);
997       break;
998     case BITFIELD:
999       fprintf(stderr, "unsigned %s", name);
1000       break;
1001     case FUNC:
1002       DEBUG_PrintTypeCast(dt->un.funct.rettype);
1003       fprintf(stderr, "(*%s)()", name);
1004       break;
1005     case CONST:
1006     case TYPEDEF:
1007       fprintf(stderr, "What???\n");
1008       break;
1009     }
1010
1011   return TRUE;
1012 }
1013
1014