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