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