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