Release 970112
[wine.git] / debugger / types.c
blob581d096fcc828295c9f6b042efce7d6e21fa4b2d
1 /*
2 * File types.c - datatype handling stuff for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
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 */
10 #include <stdio.h>
11 #include <stdlib.h>
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>
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"
29 #define NR_TYPE_HASH 521
31 struct en_values
33 struct en_values* next;
34 char * name;
35 int value;
38 struct member
40 struct member * next;
41 char * name;
42 struct datatype * type;
43 int offset;
44 int size;
47 struct datatype
49 enum debug_type type;
50 struct datatype * next;
51 char * name;
52 union
54 struct
56 char basic_type;
57 char * output_format;
58 char basic_size;
59 unsigned b_signed:1;
60 } basic;
61 struct
63 unsigned short bitoff;
64 unsigned short nbits;
65 struct datatype * basetype;
66 } bitfield;
68 struct
70 struct datatype * pointsto;
71 } pointer;
72 struct
74 struct datatype * rettype;
75 } funct;
76 struct
78 int start;
79 int end;
80 struct datatype * basictype;
81 } array;
82 struct
84 int size;
85 struct member * members;
86 } structure;
87 struct
89 struct en_values * members;
90 } enumeration;
91 } un;
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
114 struct datatype * DEBUG_TypeInt = NULL;
115 struct datatype * DEBUG_TypeIntConst = NULL;
116 struct datatype * DEBUG_TypeUSInt = NULL;
117 struct datatype * DEBUG_TypeString = NULL;
120 * All of the types that have been defined so far.
122 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
123 static struct datatype * pointer_types = NULL;
125 static unsigned int type_hash( const char * name )
127 unsigned int hash = 0;
128 unsigned int tmp;
129 const char * p;
131 p = name;
133 while (*p)
135 hash = (hash << 4) + *p++;
137 if( (tmp = (hash & 0xf0000000)) )
139 hash ^= tmp >> 24;
141 hash &= ~tmp;
143 return hash % NR_TYPE_HASH;
147 static struct datatype *
148 DEBUG_InitBasic(int type, char * name, int size, int b_signed,
149 char * output_format)
151 int hash;
153 struct datatype * dt;
154 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
156 if( dt != NULL )
158 if( name != NULL )
160 hash = type_hash(name);
162 else
164 hash = NR_TYPE_HASH;
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;
177 return dt;
180 struct datatype *
181 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
183 struct datatype * dt = NULL;
184 int hash;
187 * The last bucket is special, and is used to hold typeless names.
189 if( typename == NULL )
191 hash = NR_TYPE_HASH;
193 else
195 hash = type_hash(typename);
198 if( typename != NULL )
200 for( dt = type_hash_table[hash]; dt; dt = dt->next )
202 if( xtype != dt->type || dt->name == NULL
203 || dt->name[0] != typename[0])
205 continue;
208 if( strcmp(dt->name, typename) == 0 )
210 return dt;
215 if( dt == NULL )
217 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
219 if( dt != NULL )
221 memset(dt, 0, sizeof(*dt));
223 dt->type = xtype;
224 if( typename != NULL )
226 dt->name = xstrdup(typename);
228 else
230 dt->name = NULL;
232 if( xtype == POINTER )
234 dt->next = pointer_types;
235 pointer_types = dt;
237 else
239 dt->next = type_hash_table[hash];
240 type_hash_table[hash] = dt;
245 return dt;
248 struct datatype *
249 DEBUG_FindOrMakePointerType(struct datatype * reftype)
251 struct datatype * dt = NULL;
253 if( reftype != NULL )
255 for( dt = pointer_types; dt; dt = dt->next )
257 if( dt->type != POINTER )
259 continue;
262 if( dt->un.pointer.pointsto == reftype )
264 return dt;
269 if( dt == NULL )
271 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
273 if( dt != NULL )
275 dt->type = POINTER;
276 dt->un.pointer.pointsto = reftype;
277 dt->next = pointer_types;
278 pointer_types = dt;
282 return dt;
285 void
286 DEBUG_InitTypes()
288 static int beenhere = 0;
289 struct datatype * chartype;
291 if( beenhere++ != 0 )
293 return;
297 * Special version of int used with constants of various kinds.
299 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
302 * Initialize a few builtin types.
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);
326 DEBUG_TypeString = DEBUG_NewDataType(POINTER, NULL);
327 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
330 * Now initialize the builtins for codeview.
332 DEBUG_InitCVDataTypes();
336 long long int
337 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
339 unsigned int rtn;
340 struct datatype * type2 = NULL;
341 struct en_values * e;
342 char * def_format = "0x%x";
344 rtn = 0;
345 assert(addr->type != NULL);
347 switch(addr->type->type)
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) )
355 rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
357 if( addr->type->un.basic.output_format != NULL )
359 def_format = addr->type->un.basic.output_format;
363 * Check for single character prints that are out of range.
365 if( addr->type->un.basic.basic_size == 1
366 && strcmp(def_format, "'%c'") == 0
367 && ((rtn < 0x20) || (rtn > 0x80)) )
369 def_format = "%d";
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 )
378 def_format = "\"%s\"";
379 break;
381 else
383 def_format = "0x%8.8x";
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 )
396 if( e->value == rtn )
398 break;
401 if( e != NULL )
403 rtn = (int) e->name;
404 def_format = "%s";
406 else
408 def_format = "%d";
410 break;
411 default:
412 rtn = 0;
413 break;
417 if( format != NULL )
419 *format = def_format;
421 return rtn;
424 unsigned int
425 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
428 * Make sure that this really makes sense.
430 if( addr->type->type != POINTER )
432 *newtype = NULL;
433 return 0;
436 *newtype = addr->type->un.pointer.pointsto;
437 return *(unsigned int*) (addr->off);
440 unsigned int
441 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
443 struct member * m;
444 unsigned int mask;
447 * Make sure that this really makes sense.
449 if( addr->type->type != STRUCT )
451 addr->type = NULL;
452 return FALSE;
455 for(m = addr->type->un.structure.members; m; m = m->next)
457 if( strcmp(m->name, ele_name) == 0 )
459 addr->type = m->type;
460 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
463 * Bitfield operation. We have to extract the field and store
464 * it in a temporary buffer so that we get it all right.
466 *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
467 addr->off = (int) tmpbuf;
469 mask = 0xffffffff << (m->size);
470 *tmpbuf &= ~mask;
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.
476 if( m->type->type == BASIC && m->type->un.basic.b_signed != 0
477 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
479 *tmpbuf |= mask;
482 else
484 addr->off += (m->offset >> 3);
486 return TRUE;
490 addr->type = NULL;
491 return FALSE;
495 DEBUG_SetStructSize(struct datatype * dt, int size)
497 assert(dt->type == STRUCT);
498 dt->un.structure.size = size;
499 dt->un.structure.members = NULL;
501 return TRUE;
505 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
508 assert( dt->type == dt2->type && ((dt->type == STRUCT) || (dt->type == ENUM)));
510 if( dt->type == STRUCT )
512 dt->un.structure.members = dt2->un.structure.members;
514 else
516 dt->un.enumeration.members = dt2->un.enumeration.members;
519 return TRUE;
523 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
524 int offset, int size)
526 struct member * m;
527 struct member * last;
528 struct en_values * e;
530 if( dt->type == STRUCT )
532 for(last = dt->un.structure.members; last; last = last->next)
534 if( (last->name[0] == name[0])
535 && (strcmp(last->name, name) == 0) )
537 return TRUE;
539 if( last->next == NULL )
541 break;
544 m = (struct member *) xmalloc(sizeof(struct member));
545 if( m == FALSE )
547 return FALSE;
550 m->name = xstrdup(name);
551 m->type = type;
552 m->offset = offset;
553 m->size = size;
554 if( last == NULL )
556 m->next = dt->un.structure.members;
557 dt->un.structure.members = m;
559 else
561 last->next = m;
562 m->next = NULL;
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.
568 if( type->type == BITFIELD )
570 m->offset += m->type->un.bitfield.bitoff;
571 m->size = m->type->un.bitfield.nbits;
572 m->type = m->type->un.bitfield.basetype;
575 else if( dt->type == ENUM )
577 e = (struct en_values *) xmalloc(sizeof(struct en_values));
578 if( e == FALSE )
580 return FALSE;
583 e->name = xstrdup(name);
584 e->value = offset;
585 e->next = dt->un.enumeration.members;
586 dt->un.enumeration.members = e;
588 else
590 assert(FALSE);
592 return TRUE;
595 struct datatype *
596 DEBUG_GetPointerType(struct datatype * dt)
598 if( dt->type == POINTER )
600 return dt->un.pointer.pointsto;
603 return NULL;
607 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
609 switch(dt->type)
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);
621 return TRUE;
625 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
627 assert(dt->type == ARRAY);
628 dt->un.array.start = min;
629 dt->un.array.end = max;
630 dt->un.array.basictype = dt2;
632 return TRUE;
636 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
637 struct datatype * dt2)
639 assert(dt->type == BITFIELD);
640 dt->un.bitfield.bitoff = offset;
641 dt->un.bitfield.nbits = nbits;
642 dt->un.bitfield.basetype = dt2;
644 return TRUE;
647 int DEBUG_GetObjectSize(struct datatype * dt)
649 if( dt == NULL )
651 return 0;
654 switch(dt->type)
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:
669 * Bitfields have to be handled seperately later on
670 * when we insert the element into the structure.
672 return 0;
673 case TYPEDEF:
674 case FUNC:
675 case CONST:
676 assert(FALSE);
678 return 0;
681 unsigned int
682 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
684 int size;
687 * Make sure that this really makes sense.
689 if( addr->type->type == POINTER )
692 * Get the base type, so we know how much to index by.
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;
698 else if (addr->type->type == ARRAY)
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);
705 return TRUE;
708 /***********************************************************************
709 * DEBUG_Print
711 * Implementation of the 'print' command.
713 void DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
715 DBG_ADDR addr1;
716 int i;
717 struct member * m;
718 char * pnt;
719 int size;
720 long long int value;
722 if (count != 1)
724 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
725 return;
728 if( addr->type == NULL )
730 fprintf(stderr, "Unable to evaluate expression\n");
731 return;
734 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
736 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
737 format = '\0';
740 switch(addr->type->type)
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)
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 )
759 fprintf(stderr, ", ");
762 fprintf(stderr, "}");
763 break;
764 case ARRAY:
766 * Loop over all of the entries, printing stuff as we go.
768 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
769 if( size == 1 )
772 * Special handling for character arrays.
774 pnt = (char *) addr->off;
775 fprintf(stderr, "\"");
776 for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
778 fputc(*pnt++, stderr);
780 fprintf(stderr, "\"");
781 break;
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++ )
788 DEBUG_Print(&addr1, 1, format, level + 1);
789 addr1.off += size;
790 if( i == addr->type->un.array.end )
792 fprintf(stderr, "}");
794 else
796 fprintf(stderr, ", ");
799 break;
800 default:
801 assert(FALSE);
802 break;
805 if( level == 0 )
807 fprintf(stderr, "\n");