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