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