Release 970112
[wine] / debugger / types.c
1 /*
2  * File types.c - datatype handling stuff for internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
6  * This really doesn't do much at the moment, but it forms the framework
7  * upon which full support for datatype handling will eventually be hung.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <assert.h>
14 #include <sys/mman.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <strings.h>
19 #include <unistd.h>
20 #include <malloc.h>
21
22 #include "win.h"
23 #include "pe_image.h"
24 #include "peexe.h"
25 #include "debugger.h"
26 #include "peexe.h"
27 #include "xmalloc.h"
28
29 #define NR_TYPE_HASH 521
30
31 struct en_values
32 {
33   struct en_values* next;
34   char            * name;
35   int               value;
36 };
37
38 struct member
39 {
40   struct member   * next;
41   char            * name;
42   struct datatype * type;
43   int               offset;
44   int               size;
45 };
46
47 struct datatype
48 {
49   enum  debug_type type;
50   struct datatype * next;
51   char * name;
52   union
53   {
54     struct
55     {
56       char    basic_type;
57       char  * output_format;
58       char    basic_size;
59       unsigned b_signed:1;
60     } basic;
61     struct
62     {
63       unsigned short bitoff;
64       unsigned short nbits;
65       struct datatype * basetype;
66     } bitfield;
67
68     struct
69     {
70       struct datatype * pointsto;
71     } pointer;
72     struct
73     {
74       struct datatype * rettype;
75     } funct;
76     struct
77     {
78       int               start;
79       int               end;
80       struct datatype * basictype;
81     } array;
82     struct
83     {
84       int               size;
85       struct member * members;
86     } structure;
87     struct
88     {
89       struct en_values * members;
90     } enumeration;
91   } un;
92 };
93
94 #define BASIC_INT               1
95 #define BASIC_CHAR              2
96 #define BASIC_LONG              3
97 #define BASIC_UINT              4
98 #define BASIC_LUI               5
99 #define BASIC_LONGLONG          6
100 #define BASIC_ULONGLONGI        7
101 #define BASIC_SHORT             8
102 #define BASIC_SHORTUI           9
103 #define BASIC_SCHAR             10
104 #define BASIC_UCHAR             11
105 #define BASIC_FLT               12
106 #define BASIC_LONG_DOUBLE       13
107 #define BASIC_DOUBLE            14
108 #define BASIC_CMPLX_INT         15
109 #define BASIC_CMPLX_FLT         16
110 #define BASIC_CMPLX_DBL         17
111 #define BASIC_CMPLX_LONG_DBL    18
112 #define BASIC_VOID              19
113
114 struct datatype * DEBUG_TypeInt = NULL;
115 struct datatype * DEBUG_TypeIntConst = NULL;
116 struct datatype * DEBUG_TypeUSInt = NULL;
117 struct datatype * DEBUG_TypeString = NULL;
118
119 /*
120  * All of the types that have been defined so far.
121  */
122 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
123 static struct datatype * pointer_types = NULL;
124
125 static unsigned int type_hash( const char * name )
126 {
127     unsigned int hash = 0;
128     unsigned int tmp;
129     const char * p;
130
131     p = name;
132
133     while (*p) 
134       {
135         hash = (hash << 4) + *p++;
136
137         if( (tmp = (hash & 0xf0000000)) )
138           {
139             hash ^= tmp >> 24;
140           }
141         hash &= ~tmp;
142       }
143     return hash % NR_TYPE_HASH;
144 }
145
146
147 static struct datatype *
148 DEBUG_InitBasic(int type, char * name, int size, int b_signed, 
149                             char * output_format)
150 {
151   int hash;
152
153   struct datatype * dt;
154   dt = (struct datatype *) xmalloc(sizeof(struct datatype));
155
156   if( dt != NULL )
157     {
158       if( name != NULL )
159         {
160           hash = type_hash(name);
161         }
162       else
163         {
164           hash = NR_TYPE_HASH;
165         }
166
167       dt->type = BASIC;
168       dt->name = name;
169       dt->next = type_hash_table[hash];
170       type_hash_table[hash] = dt;
171       dt->un.basic.basic_type = type;
172       dt->un.basic.basic_size = size;
173       dt->un.basic.b_signed = b_signed;
174       dt->un.basic.output_format = output_format;
175     }
176
177   return dt;
178 }
179
180 struct datatype *
181 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
182 {
183   struct datatype * dt = NULL;
184   int hash;
185
186   /*
187    * The last bucket is special, and is used to hold typeless names.
188    */
189   if( typename == NULL )
190     {
191       hash = NR_TYPE_HASH;
192     }
193   else
194     {
195       hash = type_hash(typename);
196     }
197
198   if( typename != NULL )
199     {
200       for( dt = type_hash_table[hash]; dt; dt = dt->next )
201         {
202           if( xtype != dt->type || dt->name == NULL 
203               || dt->name[0] != typename[0])
204             {
205               continue;
206             }
207                   
208           if( strcmp(dt->name, typename) == 0 )
209             {
210               return dt;
211             }
212         }
213     }
214
215   if( dt == NULL )
216     {
217       dt = (struct datatype *) xmalloc(sizeof(struct datatype));
218       
219       if( dt != NULL )
220         {
221           memset(dt, 0, sizeof(*dt));
222       
223           dt->type = xtype;
224           if( typename != NULL )
225             {
226               dt->name = xstrdup(typename);
227             }
228           else
229             {
230               dt->name = NULL;
231             }
232           if( xtype == POINTER )
233             {
234               dt->next = pointer_types;
235               pointer_types = dt;
236             }
237           else
238             {
239               dt->next = type_hash_table[hash];
240               type_hash_table[hash] = dt;
241             }
242         }
243     }
244
245   return dt;
246 }
247
248 struct datatype *
249 DEBUG_FindOrMakePointerType(struct datatype * reftype)
250 {
251   struct datatype * dt = NULL;
252
253   if( reftype != NULL )
254     {
255       for( dt = pointer_types; dt; dt = dt->next )
256         {
257           if( dt->type != POINTER )
258             {
259               continue;
260             }
261                   
262           if( dt->un.pointer.pointsto == reftype )
263             {
264               return dt;
265             }
266         }
267     }
268
269   if( dt == NULL )
270     {
271       dt = (struct datatype *) xmalloc(sizeof(struct datatype));
272       
273       if( dt != NULL )
274         {
275           dt->type = POINTER;
276           dt->un.pointer.pointsto = reftype;
277           dt->next = pointer_types;
278           pointer_types = dt;
279         }
280     }
281
282   return dt;
283 }
284
285 void
286 DEBUG_InitTypes()
287 {
288   static int beenhere = 0;
289   struct datatype * chartype;
290
291   if( beenhere++ != 0 )
292     {
293       return;
294     }
295
296   /*
297    * Special version of int used with constants of various kinds.
298    */
299   DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
300
301   /*
302    * Initialize a few builtin types.
303    */
304
305
306   DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
307   chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
308   DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
309   DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
310   DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
311   DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
312   DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
313   DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
314   DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
315   DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
316   DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
317   DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
318   DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
319   DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
320   DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
321   DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
322   DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
323   DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
324   DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
325
326   DEBUG_TypeString = DEBUG_NewDataType(POINTER, NULL);
327   DEBUG_SetPointerType(DEBUG_TypeString, chartype);
328
329   /*
330    * Now initialize the builtins for codeview.
331    */
332   DEBUG_InitCVDataTypes();
333
334 }
335
336 long long int
337 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
338 {
339   unsigned int rtn;
340   struct datatype * type2 = NULL;
341   struct en_values * e;
342   char * def_format = "0x%x";
343
344   rtn = 0;
345   assert(addr->type != NULL);
346
347   switch(addr->type->type)
348     {
349     case BASIC:
350       memcpy(&rtn, (char *) addr->off, addr->type->un.basic.basic_size);
351       if(    (addr->type->un.basic.b_signed)
352           && ((addr->type->un.basic.basic_size & 3) != 0)
353           && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
354         {
355           rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
356         }
357       if( addr->type->un.basic.output_format != NULL )
358         {
359           def_format = addr->type->un.basic.output_format;
360         }
361
362       /*
363        * Check for single character prints that are out of range.
364        */
365       if( addr->type->un.basic.basic_size == 1
366           && strcmp(def_format, "'%c'") == 0 
367           && ((rtn < 0x20) || (rtn > 0x80)) )
368         {
369           def_format = "%d";
370         }
371       break;
372     case POINTER:
373       if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
374       rtn = (unsigned int)  *((unsigned char **)addr->off);
375       type2 = addr->type->un.pointer.pointsto;
376       if( type2->type == BASIC && type2->un.basic.basic_size == 1 )
377         {
378           def_format = "\"%s\"";
379           break;
380         }
381       else
382         {
383           def_format = "0x%8.8x";
384         }
385       break;
386     case ARRAY:
387     case STRUCT:
388       if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
389       rtn = (unsigned int)  *((unsigned char **)addr->off);
390       def_format = "0x%8.8x";
391       break;
392     case ENUM:
393       rtn = (unsigned int)  *((unsigned char **)addr->off);
394       for(e = addr->type->un.enumeration.members; e; e = e->next )
395         {
396           if( e->value == rtn )
397             {
398               break;
399             }
400         }
401       if( e != NULL )
402         {
403           rtn = (int) e->name;
404           def_format = "%s";
405         }
406       else
407         {
408           def_format = "%d";
409         }
410       break;
411     default:
412       rtn = 0;
413       break;
414     }
415
416
417   if( format != NULL )
418     {
419       *format = def_format;
420     }
421   return rtn;
422 }
423
424 unsigned int
425 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
426 {
427   /*
428    * Make sure that this really makes sense.
429    */
430   if( addr->type->type != POINTER )
431     {
432       *newtype = NULL;
433       return 0;
434     }
435
436   *newtype = addr->type->un.pointer.pointsto;
437   return *(unsigned int*) (addr->off);
438 }
439
440 unsigned int
441 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
442 {
443   struct member * m;
444   unsigned int    mask;
445
446   /*
447    * Make sure that this really makes sense.
448    */
449   if( addr->type->type != STRUCT )
450     {
451       addr->type = NULL;
452       return FALSE;
453     }
454
455   for(m = addr->type->un.structure.members; m; m = m->next)
456     {
457       if( strcmp(m->name, ele_name) == 0 )
458         {
459           addr->type = m->type;
460           if( (m->offset & 7) != 0 || (m->size & 7) != 0)
461             {
462               /*
463                * Bitfield operation.  We have to extract the field and store
464                * it in a temporary buffer so that we get it all right.
465                */
466               *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
467               addr->off = (int) tmpbuf;
468
469               mask = 0xffffffff << (m->size);
470               *tmpbuf &= ~mask;
471               /*
472                * OK, now we have the correct part of the number.
473                * Check to see whether the basic type is signed or not, and if so,
474                * we need to sign extend the number.
475                */
476               if( m->type->type == BASIC && m->type->un.basic.b_signed != 0
477                   && (*tmpbuf & (1 << (m->size - 1))) != 0 )
478                 {
479                   *tmpbuf |= mask;
480                 }
481             }
482           else
483             {
484               addr->off += (m->offset >> 3);
485             }
486           return TRUE;
487         }
488     }
489
490   addr->type = NULL;
491   return FALSE;
492 }
493
494 int
495 DEBUG_SetStructSize(struct datatype * dt, int size)
496 {
497   assert(dt->type == STRUCT);
498   dt->un.structure.size = size;
499   dt->un.structure.members = NULL;
500
501   return TRUE;
502 }
503
504 int
505 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
506 {
507
508   assert( dt->type == dt2->type && ((dt->type == STRUCT) || (dt->type == ENUM)));
509
510   if( dt->type == STRUCT )
511     {
512       dt->un.structure.members = dt2->un.structure.members;
513     }
514   else
515     {
516       dt->un.enumeration.members = dt2->un.enumeration.members;
517     }
518
519   return TRUE;
520 }
521
522 int
523 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type, 
524                        int offset, int size)
525 {
526   struct member * m;
527   struct member * last;
528   struct en_values * e;
529
530   if( dt->type == STRUCT )
531     {
532       for(last = dt->un.structure.members; last; last = last->next)
533         {
534           if(    (last->name[0] == name[0]) 
535               && (strcmp(last->name, name) == 0) )
536             {
537               return TRUE;
538             }
539           if( last->next == NULL )
540             {
541               break;
542             }
543         }
544       m = (struct member *) xmalloc(sizeof(struct member));
545       if( m == FALSE )
546         {
547           return FALSE;
548         }
549       
550       m->name = xstrdup(name);
551       m->type = type;
552       m->offset = offset;
553       m->size = size;
554       if( last == NULL )
555         {
556           m->next = dt->un.structure.members;
557           dt->un.structure.members = m;
558         }
559       else
560         {
561           last->next = m;
562           m->next = NULL;
563         }
564       /*
565        * If the base type is bitfield, then adjust the offsets here so that we
566        * are able to look things up without lots of falter-all.
567        */
568       if( type->type == BITFIELD )
569         {
570           m->offset += m->type->un.bitfield.bitoff;
571           m->size = m->type->un.bitfield.nbits;
572           m->type = m->type->un.bitfield.basetype;
573         }
574     }
575   else if( dt->type == ENUM )
576     {
577       e = (struct en_values *) xmalloc(sizeof(struct en_values));
578       if( e == FALSE )
579         {
580           return FALSE;
581         }
582       
583       e->name = xstrdup(name);
584       e->value = offset;
585       e->next = dt->un.enumeration.members;
586       dt->un.enumeration.members = e;
587     }
588   else
589     {
590       assert(FALSE);
591     }
592   return TRUE;
593 }
594
595 struct datatype * 
596 DEBUG_GetPointerType(struct datatype * dt)
597 {
598   if( dt->type == POINTER )
599     {
600       return dt->un.pointer.pointsto;
601     }
602
603   return NULL;
604 }
605
606 int
607 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
608 {
609   switch(dt->type)
610     {
611     case POINTER:
612       dt->un.pointer.pointsto = dt2;
613       break;
614     case FUNC:
615       dt->un.funct.rettype = dt2;
616       break;
617     default:
618       assert(FALSE);
619     }
620
621   return TRUE;
622 }
623
624 int
625 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
626 {
627   assert(dt->type == ARRAY);
628   dt->un.array.start = min;
629   dt->un.array.end   = max;
630   dt->un.array.basictype = dt2;
631
632   return TRUE;
633 }
634
635 int
636 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits, 
637                         struct datatype * dt2)
638 {
639   assert(dt->type == BITFIELD);
640   dt->un.bitfield.bitoff   = offset;
641   dt->un.bitfield.nbits    = nbits;
642   dt->un.bitfield.basetype = dt2;
643
644   return TRUE;
645 }
646
647 int DEBUG_GetObjectSize(struct datatype * dt)
648 {
649   if( dt == NULL )
650     {
651       return 0;
652     }
653
654   switch(dt->type)
655     {
656     case BASIC:
657       return dt->un.basic.basic_size;
658     case POINTER:
659       return sizeof(int *);
660     case STRUCT:
661       return dt->un.structure.size;
662     case ENUM:
663       return sizeof(int);
664     case ARRAY:
665       return (dt->un.array.end - dt->un.array.start) 
666         * DEBUG_GetObjectSize(dt->un.array.basictype);
667     case BITFIELD:
668       /*
669        * Bitfields have to be handled seperately later on
670        * when we insert the element into the structure.
671        */
672       return 0;
673     case TYPEDEF:
674     case FUNC:
675     case CONST:
676       assert(FALSE);
677     }
678   return 0;
679 }
680
681 unsigned int
682 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
683 {
684   int size;
685
686   /*
687    * Make sure that this really makes sense.
688    */
689   if( addr->type->type == POINTER )
690     {
691       /*
692        * Get the base type, so we know how much to index by.
693        */
694       size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
695       result->type = addr->type->un.pointer.pointsto;
696       result->off = (*(unsigned int*) (addr->off)) + size * index;
697     }
698   else if (addr->type->type == ARRAY)
699     {
700       size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
701       result->type = addr->type->un.array.basictype;
702       result->off = addr->off + size * (index - addr->type->un.array.start);
703     }
704
705   return TRUE;
706 }
707
708 /***********************************************************************
709  *           DEBUG_Print
710  *
711  * Implementation of the 'print' command.
712  */
713 void DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
714 {
715   DBG_ADDR        addr1;
716   int             i;
717   struct member * m;
718   char          * pnt;
719   int             size;
720   long long int   value;
721
722   if (count != 1)
723     {
724       fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
725       return;
726     }
727   
728   if( addr->type == NULL )
729     {
730       fprintf(stderr, "Unable to evaluate expression\n");
731       return;
732     }
733   
734   if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
735     {
736       fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
737       format = '\0';
738     }
739
740   switch(addr->type->type)
741     {
742     case BASIC:
743     case ENUM:
744     case CONST:
745     case POINTER:
746       DEBUG_PrintBasic(addr, 1, format);
747       break;
748     case STRUCT:
749       fprintf(stderr, "{");
750       for(m = addr->type->un.structure.members; m; m = m->next)
751         {
752           addr1 = *addr;
753           DEBUG_FindStructElement(&addr1, m->name,
754                                   (int *) &value);
755           fprintf(stderr, "%s=", m->name);
756           DEBUG_Print(&addr1, 1, format, level + 1);
757           if( m->next != NULL )
758             {
759               fprintf(stderr, ", ");
760             }
761         }
762       fprintf(stderr, "}");
763       break;
764     case ARRAY:
765       /*
766        * Loop over all of the entries, printing stuff as we go.
767        */
768       size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
769       if( size == 1 )
770         {
771           /*
772            * Special handling for character arrays.
773            */
774           pnt = (char *) addr->off;
775           fprintf(stderr, "\"");
776           for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
777             {
778               fputc(*pnt++, stderr);
779             }
780           fprintf(stderr, "\"");
781           break;
782         }
783       addr1 = *addr;
784       addr1.type = addr->type->un.array.basictype;
785       fprintf(stderr, "{");
786       for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
787         {
788           DEBUG_Print(&addr1, 1, format, level + 1);
789           addr1.off += size;
790           if( i == addr->type->un.array.end )
791             {
792               fprintf(stderr, "}");
793             }
794           else
795             {
796               fprintf(stderr, ", ");
797             }
798         }
799       break;
800     default:
801       assert(FALSE);
802       break;
803     }
804
805   if( level == 0 )
806     {
807       fprintf(stderr, "\n");
808     }
809 }