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