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