blob: 6c94c4d50ef371cf30e14af1a46a4c23b7432ee8 [file] [log] [blame]
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001/*
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>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000014#include <fcntl.h>
Alexandre Julliard01d63461997-01-20 19:43:45 +000015#include <sys/types.h>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000016#include <sys/stat.h>
17#include <limits.h>
Alexandre Julliard349a9531997-02-02 19:01:52 +000018#include <string.h>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000019#include <unistd.h>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000020
21#include "win.h"
22#include "pe_image.h"
23#include "peexe.h"
24#include "debugger.h"
25#include "peexe.h"
26#include "xmalloc.h"
27
28#define NR_TYPE_HASH 521
29
Alexandre Julliard01d63461997-01-20 19:43:45 +000030int DEBUG_nchar;
31static int DEBUG_maxchar = 1024;
32
Alexandre Julliardc6c09441997-01-12 18:32:19 +000033struct en_values
34{
35 struct en_values* next;
36 char * name;
37 int value;
38};
39
40struct member
41{
42 struct member * next;
43 char * name;
44 struct datatype * type;
45 int offset;
46 int size;
47};
48
49struct datatype
50{
51 enum debug_type type;
52 struct datatype * next;
53 char * name;
54 union
55 {
56 struct
57 {
58 char basic_type;
59 char * output_format;
60 char basic_size;
61 unsigned b_signed:1;
62 } basic;
63 struct
64 {
65 unsigned short bitoff;
66 unsigned short nbits;
67 struct datatype * basetype;
68 } bitfield;
69
70 struct
71 {
72 struct datatype * pointsto;
73 } pointer;
74 struct
75 {
76 struct datatype * rettype;
77 } funct;
78 struct
79 {
80 int start;
81 int end;
82 struct datatype * basictype;
83 } array;
84 struct
85 {
86 int size;
87 struct member * members;
88 } structure;
89 struct
90 {
91 struct en_values * members;
92 } enumeration;
93 } un;
94};
95
96#define BASIC_INT 1
97#define BASIC_CHAR 2
98#define BASIC_LONG 3
99#define BASIC_UINT 4
100#define BASIC_LUI 5
101#define BASIC_LONGLONG 6
102#define BASIC_ULONGLONGI 7
103#define BASIC_SHORT 8
104#define BASIC_SHORTUI 9
105#define BASIC_SCHAR 10
106#define BASIC_UCHAR 11
107#define BASIC_FLT 12
108#define BASIC_LONG_DOUBLE 13
109#define BASIC_DOUBLE 14
110#define BASIC_CMPLX_INT 15
111#define BASIC_CMPLX_FLT 16
112#define BASIC_CMPLX_DBL 17
113#define BASIC_CMPLX_LONG_DBL 18
114#define BASIC_VOID 19
115
116struct datatype * DEBUG_TypeInt = NULL;
117struct datatype * DEBUG_TypeIntConst = NULL;
118struct datatype * DEBUG_TypeUSInt = NULL;
119struct datatype * DEBUG_TypeString = NULL;
120
121/*
122 * All of the types that have been defined so far.
123 */
124static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
125static struct datatype * pointer_types = NULL;
126
127static unsigned int type_hash( const char * name )
128{
129 unsigned int hash = 0;
130 unsigned int tmp;
131 const char * p;
132
133 p = name;
134
135 while (*p)
136 {
137 hash = (hash << 4) + *p++;
138
139 if( (tmp = (hash & 0xf0000000)) )
140 {
141 hash ^= tmp >> 24;
142 }
143 hash &= ~tmp;
144 }
145 return hash % NR_TYPE_HASH;
146}
147
148
149static struct datatype *
150DEBUG_InitBasic(int type, char * name, int size, int b_signed,
151 char * output_format)
152{
153 int hash;
154
155 struct datatype * dt;
156 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
157
158 if( dt != NULL )
159 {
160 if( name != NULL )
161 {
162 hash = type_hash(name);
163 }
164 else
165 {
166 hash = NR_TYPE_HASH;
167 }
168
François Gouget241c7301998-10-28 10:47:09 +0000169 dt->type = DT_BASIC;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000170 dt->name = name;
171 dt->next = type_hash_table[hash];
172 type_hash_table[hash] = dt;
173 dt->un.basic.basic_type = type;
174 dt->un.basic.basic_size = size;
175 dt->un.basic.b_signed = b_signed;
176 dt->un.basic.output_format = output_format;
177 }
178
179 return dt;
180}
181
Alexandre Julliard01d63461997-01-20 19:43:45 +0000182static
183struct datatype *
184DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
185{
186 struct datatype * dt = NULL;
187
188 if( typename != NULL )
189 {
190 for( dt = type_hash_table[hash]; dt; dt = dt->next )
191 {
192 if( xtype != dt->type || dt->name == NULL
193 || dt->name[0] != typename[0])
194 {
195 continue;
196 }
197
198 if( strcmp(dt->name, typename) == 0 )
199 {
200 return dt;
201 }
202 }
203 }
204
205 return dt;
206}
207
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000208struct datatype *
209DEBUG_NewDataType(enum debug_type xtype, const char * typename)
210{
211 struct datatype * dt = NULL;
212 int hash;
213
214 /*
215 * The last bucket is special, and is used to hold typeless names.
216 */
217 if( typename == NULL )
218 {
219 hash = NR_TYPE_HASH;
220 }
221 else
222 {
223 hash = type_hash(typename);
224 }
225
Alexandre Julliard01d63461997-01-20 19:43:45 +0000226 dt = DEBUG_LookupDataType(xtype, hash, typename);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000227
228 if( dt == NULL )
229 {
230 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
231
232 if( dt != NULL )
233 {
234 memset(dt, 0, sizeof(*dt));
235
236 dt->type = xtype;
237 if( typename != NULL )
238 {
239 dt->name = xstrdup(typename);
240 }
241 else
242 {
243 dt->name = NULL;
244 }
François Gouget241c7301998-10-28 10:47:09 +0000245 if( xtype == DT_POINTER )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000246 {
247 dt->next = pointer_types;
248 pointer_types = dt;
249 }
250 else
251 {
252 dt->next = type_hash_table[hash];
253 type_hash_table[hash] = dt;
254 }
255 }
256 }
257
258 return dt;
259}
260
261struct datatype *
262DEBUG_FindOrMakePointerType(struct datatype * reftype)
263{
264 struct datatype * dt = NULL;
265
266 if( reftype != NULL )
267 {
268 for( dt = pointer_types; dt; dt = dt->next )
269 {
François Gouget241c7301998-10-28 10:47:09 +0000270 if( dt->type != DT_POINTER )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000271 {
272 continue;
273 }
274
275 if( dt->un.pointer.pointsto == reftype )
276 {
277 return dt;
278 }
279 }
280 }
281
282 if( dt == NULL )
283 {
284 dt = (struct datatype *) xmalloc(sizeof(struct datatype));
285
286 if( dt != NULL )
287 {
François Gouget241c7301998-10-28 10:47:09 +0000288 dt->type = DT_POINTER;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000289 dt->un.pointer.pointsto = reftype;
290 dt->next = pointer_types;
291 pointer_types = dt;
292 }
293 }
294
295 return dt;
296}
297
298void
299DEBUG_InitTypes()
300{
301 static int beenhere = 0;
302 struct datatype * chartype;
303
304 if( beenhere++ != 0 )
305 {
306 return;
307 }
308
309 /*
310 * Special version of int used with constants of various kinds.
311 */
312 DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
313
314 /*
315 * Initialize a few builtin types.
316 */
317
318
319 DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
320 chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
321 DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
322 DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
323 DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
324 DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
325 DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
326 DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
327 DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
328 DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
329 DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
330 DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
331 DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
332 DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
333 DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
334 DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
335 DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
336 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
337 DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
338
François Gouget241c7301998-10-28 10:47:09 +0000339 DEBUG_TypeString = DEBUG_NewDataType(DT_POINTER, NULL);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000340 DEBUG_SetPointerType(DEBUG_TypeString, chartype);
341
342 /*
343 * Now initialize the builtins for codeview.
344 */
345 DEBUG_InitCVDataTypes();
346
347}
348
349long long int
350DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
351{
Ove Kaaven69df3711998-10-11 12:27:04 +0000352 DBG_ADDR address = *addr;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000353 unsigned int rtn;
354 struct datatype * type2 = NULL;
355 struct en_values * e;
356 char * def_format = "0x%x";
357
358 rtn = 0;
Ove Kaaven69df3711998-10-11 12:27:04 +0000359 address.seg = 0; /* FIXME? I don't quite get this... */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000360 assert(addr->type != NULL);
361
362 switch(addr->type->type)
363 {
François Gouget241c7301998-10-28 10:47:09 +0000364 case DT_BASIC:
Ove Kaaven69df3711998-10-11 12:27:04 +0000365 if (!DBG_CHECK_READ_PTR( &address, addr->type->un.basic.basic_size))
Alexandre Julliard01d63461997-01-20 19:43:45 +0000366 {
367 return 0;
368 }
369
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000370 memcpy(&rtn, (char *) addr->off, addr->type->un.basic.basic_size);
371 if( (addr->type->un.basic.b_signed)
372 && ((addr->type->un.basic.basic_size & 3) != 0)
373 && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
374 {
375 rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
376 }
377 if( addr->type->un.basic.output_format != NULL )
378 {
379 def_format = addr->type->un.basic.output_format;
380 }
381
382 /*
383 * Check for single character prints that are out of range.
384 */
385 if( addr->type->un.basic.basic_size == 1
386 && strcmp(def_format, "'%c'") == 0
387 && ((rtn < 0x20) || (rtn > 0x80)) )
388 {
389 def_format = "%d";
390 }
391 break;
François Gouget241c7301998-10-28 10:47:09 +0000392 case DT_POINTER:
Ove Kaaven69df3711998-10-11 12:27:04 +0000393 if (!DBG_CHECK_READ_PTR( &address, 1 )) return 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000394 rtn = (unsigned int) *((unsigned char **)addr->off);
395 type2 = addr->type->un.pointer.pointsto;
François Gouget241c7301998-10-28 10:47:09 +0000396 if( type2->type == DT_BASIC && type2->un.basic.basic_size == 1 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000397 {
398 def_format = "\"%s\"";
399 break;
400 }
401 else
402 {
403 def_format = "0x%8.8x";
404 }
405 break;
François Gouget241c7301998-10-28 10:47:09 +0000406 case DT_ARRAY:
407 case DT_STRUCT:
Ove Kaaven69df3711998-10-11 12:27:04 +0000408 if (!DBG_CHECK_READ_PTR( &address, 1 )) return 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000409 rtn = (unsigned int) *((unsigned char **)addr->off);
410 def_format = "0x%8.8x";
411 break;
François Gouget241c7301998-10-28 10:47:09 +0000412 case DT_ENUM:
Ove Kaaven69df3711998-10-11 12:27:04 +0000413 if (!DBG_CHECK_READ_PTR( &address, 1 )) return 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000414 rtn = (unsigned int) *((unsigned char **)addr->off);
415 for(e = addr->type->un.enumeration.members; e; e = e->next )
416 {
417 if( e->value == rtn )
418 {
419 break;
420 }
421 }
422 if( e != NULL )
423 {
424 rtn = (int) e->name;
425 def_format = "%s";
426 }
427 else
428 {
429 def_format = "%d";
430 }
431 break;
432 default:
433 rtn = 0;
434 break;
435 }
436
437
438 if( format != NULL )
439 {
440 *format = def_format;
441 }
442 return rtn;
443}
444
445unsigned int
446DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
447{
Ove Kaaven69df3711998-10-11 12:27:04 +0000448 DBG_ADDR address = *addr;
449
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000450 /*
451 * Make sure that this really makes sense.
452 */
François Gouget241c7301998-10-28 10:47:09 +0000453 if( addr->type->type != DT_POINTER )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000454 {
455 *newtype = NULL;
456 return 0;
457 }
458
459 *newtype = addr->type->un.pointer.pointsto;
Ove Kaaven69df3711998-10-11 12:27:04 +0000460 address.off = *(unsigned int*) (addr->off);
461 return (unsigned int)DBG_ADDR_TO_LIN(&address); /* FIXME: is this right (or "better") ? */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000462}
463
464unsigned int
465DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
466{
467 struct member * m;
468 unsigned int mask;
469
470 /*
471 * Make sure that this really makes sense.
472 */
François Gouget241c7301998-10-28 10:47:09 +0000473 if( addr->type->type != DT_STRUCT )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000474 {
475 addr->type = NULL;
476 return FALSE;
477 }
478
479 for(m = addr->type->un.structure.members; m; m = m->next)
480 {
481 if( strcmp(m->name, ele_name) == 0 )
482 {
483 addr->type = m->type;
484 if( (m->offset & 7) != 0 || (m->size & 7) != 0)
485 {
486 /*
487 * Bitfield operation. We have to extract the field and store
488 * it in a temporary buffer so that we get it all right.
489 */
490 *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
491 addr->off = (int) tmpbuf;
492
493 mask = 0xffffffff << (m->size);
494 *tmpbuf &= ~mask;
495 /*
496 * OK, now we have the correct part of the number.
497 * Check to see whether the basic type is signed or not, and if so,
498 * we need to sign extend the number.
499 */
François Gouget241c7301998-10-28 10:47:09 +0000500 if( m->type->type == DT_BASIC && m->type->un.basic.b_signed != 0
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000501 && (*tmpbuf & (1 << (m->size - 1))) != 0 )
502 {
503 *tmpbuf |= mask;
504 }
505 }
506 else
507 {
508 addr->off += (m->offset >> 3);
509 }
510 return TRUE;
511 }
512 }
513
514 addr->type = NULL;
515 return FALSE;
516}
517
518int
519DEBUG_SetStructSize(struct datatype * dt, int size)
520{
François Gouget241c7301998-10-28 10:47:09 +0000521 assert(dt->type == DT_STRUCT);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000522
523 if( dt->un.structure.members != NULL )
524 {
525 return FALSE;
526 }
527
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000528 dt->un.structure.size = size;
529 dt->un.structure.members = NULL;
530
531 return TRUE;
532}
533
534int
535DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
536{
537
François Gouget241c7301998-10-28 10:47:09 +0000538 assert( dt->type == dt2->type && ((dt->type == DT_STRUCT) || (dt->type == DT_ENUM)));
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000539
François Gouget241c7301998-10-28 10:47:09 +0000540 if( dt->type == DT_STRUCT )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000541 {
542 dt->un.structure.members = dt2->un.structure.members;
543 }
544 else
545 {
546 dt->un.enumeration.members = dt2->un.enumeration.members;
547 }
548
549 return TRUE;
550}
551
552int
553DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type,
554 int offset, int size)
555{
556 struct member * m;
557 struct member * last;
558 struct en_values * e;
559
François Gouget241c7301998-10-28 10:47:09 +0000560 if( dt->type == DT_STRUCT )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000561 {
562 for(last = dt->un.structure.members; last; last = last->next)
563 {
564 if( (last->name[0] == name[0])
565 && (strcmp(last->name, name) == 0) )
566 {
567 return TRUE;
568 }
569 if( last->next == NULL )
570 {
571 break;
572 }
573 }
574 m = (struct member *) xmalloc(sizeof(struct member));
575 if( m == FALSE )
576 {
577 return FALSE;
578 }
579
580 m->name = xstrdup(name);
581 m->type = type;
582 m->offset = offset;
583 m->size = size;
584 if( last == NULL )
585 {
586 m->next = dt->un.structure.members;
587 dt->un.structure.members = m;
588 }
589 else
590 {
591 last->next = m;
592 m->next = NULL;
593 }
594 /*
595 * If the base type is bitfield, then adjust the offsets here so that we
596 * are able to look things up without lots of falter-all.
597 */
François Gouget241c7301998-10-28 10:47:09 +0000598 if( type->type == DT_BITFIELD )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000599 {
600 m->offset += m->type->un.bitfield.bitoff;
601 m->size = m->type->un.bitfield.nbits;
602 m->type = m->type->un.bitfield.basetype;
603 }
604 }
François Gouget241c7301998-10-28 10:47:09 +0000605 else if( dt->type == DT_ENUM )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000606 {
607 e = (struct en_values *) xmalloc(sizeof(struct en_values));
608 if( e == FALSE )
609 {
610 return FALSE;
611 }
612
613 e->name = xstrdup(name);
614 e->value = offset;
615 e->next = dt->un.enumeration.members;
616 dt->un.enumeration.members = e;
617 }
618 else
619 {
620 assert(FALSE);
621 }
622 return TRUE;
623}
624
625struct datatype *
626DEBUG_GetPointerType(struct datatype * dt)
627{
François Gouget241c7301998-10-28 10:47:09 +0000628 if( dt->type == DT_POINTER )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000629 {
630 return dt->un.pointer.pointsto;
631 }
632
633 return NULL;
634}
635
636int
637DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
638{
639 switch(dt->type)
640 {
François Gouget241c7301998-10-28 10:47:09 +0000641 case DT_POINTER:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000642 dt->un.pointer.pointsto = dt2;
643 break;
François Gouget241c7301998-10-28 10:47:09 +0000644 case DT_FUNC:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000645 dt->un.funct.rettype = dt2;
646 break;
647 default:
648 assert(FALSE);
649 }
650
651 return TRUE;
652}
653
654int
655DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
656{
François Gouget241c7301998-10-28 10:47:09 +0000657 assert(dt->type == DT_ARRAY);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000658 dt->un.array.start = min;
659 dt->un.array.end = max;
660 dt->un.array.basictype = dt2;
661
662 return TRUE;
663}
664
665int
666DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits,
667 struct datatype * dt2)
668{
François Gouget241c7301998-10-28 10:47:09 +0000669 assert(dt->type == DT_BITFIELD);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000670 dt->un.bitfield.bitoff = offset;
671 dt->un.bitfield.nbits = nbits;
672 dt->un.bitfield.basetype = dt2;
673
674 return TRUE;
675}
676
677int DEBUG_GetObjectSize(struct datatype * dt)
678{
679 if( dt == NULL )
680 {
681 return 0;
682 }
683
684 switch(dt->type)
685 {
François Gouget241c7301998-10-28 10:47:09 +0000686 case DT_BASIC:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000687 return dt->un.basic.basic_size;
François Gouget241c7301998-10-28 10:47:09 +0000688 case DT_POINTER:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000689 return sizeof(int *);
François Gouget241c7301998-10-28 10:47:09 +0000690 case DT_STRUCT:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000691 return dt->un.structure.size;
François Gouget241c7301998-10-28 10:47:09 +0000692 case DT_ENUM:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000693 return sizeof(int);
François Gouget241c7301998-10-28 10:47:09 +0000694 case DT_ARRAY:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000695 return (dt->un.array.end - dt->un.array.start)
696 * DEBUG_GetObjectSize(dt->un.array.basictype);
François Gouget241c7301998-10-28 10:47:09 +0000697 case DT_BITFIELD:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000698 /*
699 * Bitfields have to be handled seperately later on
700 * when we insert the element into the structure.
701 */
702 return 0;
François Gouget241c7301998-10-28 10:47:09 +0000703 case DT_TYPEDEF:
704 case DT_FUNC:
705 case DT_CONST:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000706 assert(FALSE);
707 }
708 return 0;
709}
710
711unsigned int
712DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
713{
714 int size;
715
716 /*
717 * Make sure that this really makes sense.
718 */
François Gouget241c7301998-10-28 10:47:09 +0000719 if( addr->type->type == DT_POINTER )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000720 {
721 /*
722 * Get the base type, so we know how much to index by.
723 */
724 size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
725 result->type = addr->type->un.pointer.pointsto;
726 result->off = (*(unsigned int*) (addr->off)) + size * index;
727 }
François Gouget241c7301998-10-28 10:47:09 +0000728 else if (addr->type->type == DT_ARRAY)
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000729 {
730 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
731 result->type = addr->type->un.array.basictype;
732 result->off = addr->off + size * (index - addr->type->un.array.start);
733 }
734
735 return TRUE;
736}
737
738/***********************************************************************
739 * DEBUG_Print
740 *
741 * Implementation of the 'print' command.
742 */
Alexandre Julliard01d63461997-01-20 19:43:45 +0000743void
744DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000745{
746 DBG_ADDR addr1;
747 int i;
748 struct member * m;
749 char * pnt;
750 int size;
751 long long int value;
752
753 if (count != 1)
754 {
755 fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
756 return;
757 }
758
759 if( addr->type == NULL )
Alexandre Julliard491502b1997-11-01 19:08:16 +0000760 {
761 /* No type, just print the addr value */
762 if (addr->seg && (addr->seg != 0xffffffff))
763 DEBUG_nchar += fprintf( stderr, "0x%04lx: ", addr->seg );
764 DEBUG_nchar += fprintf( stderr, "0x%08lx", addr->off );
Alexandre Julliard01d63461997-01-20 19:43:45 +0000765 goto leave;
Alexandre Julliard491502b1997-11-01 19:08:16 +0000766 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000767
Alexandre Julliard01d63461997-01-20 19:43:45 +0000768 if( level == 0 )
769 {
770 DEBUG_nchar = 0;
771 }
772
773 if( DEBUG_nchar > DEBUG_maxchar )
774 {
775 fprintf(stderr, "...");
776 goto leave;
777 }
778
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000779 if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
780 {
781 fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
782 format = '\0';
783 }
784
785 switch(addr->type->type)
786 {
François Gouget241c7301998-10-28 10:47:09 +0000787 case DT_BASIC:
788 case DT_ENUM:
789 case DT_CONST:
790 case DT_POINTER:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000791 DEBUG_PrintBasic(addr, 1, format);
792 break;
François Gouget241c7301998-10-28 10:47:09 +0000793 case DT_STRUCT:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000794 DEBUG_nchar += fprintf(stderr, "{");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000795 for(m = addr->type->un.structure.members; m; m = m->next)
796 {
797 addr1 = *addr;
798 DEBUG_FindStructElement(&addr1, m->name,
799 (int *) &value);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000800 DEBUG_nchar += fprintf(stderr, "%s=", m->name);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000801 DEBUG_Print(&addr1, 1, format, level + 1);
802 if( m->next != NULL )
803 {
Alexandre Julliard01d63461997-01-20 19:43:45 +0000804 DEBUG_nchar += fprintf(stderr, ", ");
805 }
806 if( DEBUG_nchar > DEBUG_maxchar )
807 {
808 fprintf(stderr, "...}");
809 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000810 }
811 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000812 DEBUG_nchar += fprintf(stderr, "}");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000813 break;
François Gouget241c7301998-10-28 10:47:09 +0000814 case DT_ARRAY:
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000815 /*
816 * Loop over all of the entries, printing stuff as we go.
817 */
818 size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
819 if( size == 1 )
820 {
821 /*
822 * Special handling for character arrays.
823 */
824 pnt = (char *) addr->off;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000825 DEBUG_nchar += fprintf(stderr, "\"");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000826 for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
827 {
828 fputc(*pnt++, stderr);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000829 DEBUG_nchar++;
830 if( DEBUG_nchar > DEBUG_maxchar )
831 {
832 fprintf(stderr, "...\"");
833 goto leave;
834 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000835 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000836 DEBUG_nchar += fprintf(stderr, "\"");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000837 break;
838 }
839 addr1 = *addr;
840 addr1.type = addr->type->un.array.basictype;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000841 DEBUG_nchar += fprintf(stderr, "{");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000842 for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
843 {
844 DEBUG_Print(&addr1, 1, format, level + 1);
845 addr1.off += size;
846 if( i == addr->type->un.array.end )
847 {
Alexandre Julliard01d63461997-01-20 19:43:45 +0000848 DEBUG_nchar += fprintf(stderr, "}");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000849 }
850 else
851 {
Alexandre Julliard01d63461997-01-20 19:43:45 +0000852 DEBUG_nchar += fprintf(stderr, ", ");
853 }
854 if( DEBUG_nchar > DEBUG_maxchar )
855 {
856 fprintf(stderr, "...}");
857 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000858 }
859 }
860 break;
861 default:
862 assert(FALSE);
863 break;
864 }
865
Alexandre Julliard01d63461997-01-20 19:43:45 +0000866leave:
867
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000868 if( level == 0 )
869 {
Alexandre Julliard01d63461997-01-20 19:43:45 +0000870 DEBUG_nchar += fprintf(stderr, "\n");
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000871 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000872 return;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000873}
Alexandre Julliard01d63461997-01-20 19:43:45 +0000874
875int
876DEBUG_DumpTypes()
877{
878 struct datatype * dt = NULL;
879 struct member * m;
880 int hash;
881 int nm;
882 char * name;
883 char * member_name;
884
885 for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
886 {
887 for( dt = type_hash_table[hash]; dt; dt = dt->next )
888 {
889 name = "none";
890 if( dt->name != NULL )
891 {
892 name = dt->name;
893 }
894 switch(dt->type)
895 {
François Gouget241c7301998-10-28 10:47:09 +0000896 case DT_BASIC:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000897 fprintf(stderr, "0x%p - BASIC(%s)\n",
898 dt, name);
899 break;
François Gouget241c7301998-10-28 10:47:09 +0000900 case DT_POINTER:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000901 fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
902 dt, name, dt->un.pointer.pointsto);
903 break;
François Gouget241c7301998-10-28 10:47:09 +0000904 case DT_STRUCT:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000905 member_name = "none";
906 nm = 0;
907 if( dt->un.structure.members != NULL
908 && dt->un.structure.members->name != NULL )
909 {
910 member_name = dt->un.structure.members->name;
911 for( m = dt->un.structure.members; m; m = m->next)
912 {
913 nm++;
914 }
915 }
916 fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
917 dt->un.structure.size, nm, member_name);
918 break;
François Gouget241c7301998-10-28 10:47:09 +0000919 case DT_ARRAY:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000920 fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
921 dt, name, dt->un.array.basictype);
922 break;
François Gouget241c7301998-10-28 10:47:09 +0000923 case DT_ENUM:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000924 fprintf(stderr, "0x%p - ENUM(%s)\n",
925 dt, name);
926 break;
François Gouget241c7301998-10-28 10:47:09 +0000927 case DT_BITFIELD:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000928 fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
929 break;
François Gouget241c7301998-10-28 10:47:09 +0000930 case DT_FUNC:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000931 fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
932 dt, name, dt->un.funct.rettype);
933 break;
François Gouget241c7301998-10-28 10:47:09 +0000934 case DT_CONST:
935 case DT_TYPEDEF:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000936 fprintf(stderr, "What???\n");
937 break;
938 }
939 }
940 }
941 return TRUE;
942}
943
944
945enum debug_type DEBUG_GetType(struct datatype * dt)
946{
947 return dt->type;
948}
949
950struct datatype *
951DEBUG_TypeCast(enum debug_type type, const char * name)
952{
953 int hash;
954 struct datatype * rtn;
955
956 /*
957 * The last bucket is special, and is used to hold typeless names.
958 */
959 if( name == NULL )
960 {
961 hash = NR_TYPE_HASH;
962 }
963 else
964 {
965 hash = type_hash(name);
966 }
967
968 rtn = DEBUG_LookupDataType(type, hash, name);
969
970 return rtn;
971
972}
973
974int
975DEBUG_PrintTypeCast(struct datatype * dt)
976{
977 char * name;
978
979 name = "none";
980 if( dt->name != NULL )
981 {
982 name = dt->name;
983 }
984
985 switch(dt->type)
986 {
François Gouget241c7301998-10-28 10:47:09 +0000987 case DT_BASIC:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000988 fprintf(stderr, "%s", name);
989 break;
François Gouget241c7301998-10-28 10:47:09 +0000990 case DT_POINTER:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000991 DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
992 fprintf(stderr, "*");
993 break;
François Gouget241c7301998-10-28 10:47:09 +0000994 case DT_STRUCT:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000995 fprintf(stderr, "struct %s", name);
996 break;
François Gouget241c7301998-10-28 10:47:09 +0000997 case DT_ARRAY:
Alexandre Julliard01d63461997-01-20 19:43:45 +0000998 fprintf(stderr, "%s[]", name);
999 break;
François Gouget241c7301998-10-28 10:47:09 +00001000 case DT_ENUM:
Alexandre Julliard01d63461997-01-20 19:43:45 +00001001 fprintf(stderr, "enum %s", name);
1002 break;
François Gouget241c7301998-10-28 10:47:09 +00001003 case DT_BITFIELD:
Alexandre Julliard01d63461997-01-20 19:43:45 +00001004 fprintf(stderr, "unsigned %s", name);
1005 break;
François Gouget241c7301998-10-28 10:47:09 +00001006 case DT_FUNC:
Alexandre Julliard01d63461997-01-20 19:43:45 +00001007 DEBUG_PrintTypeCast(dt->un.funct.rettype);
1008 fprintf(stderr, "(*%s)()", name);
1009 break;
François Gouget241c7301998-10-28 10:47:09 +00001010 case DT_CONST:
1011 case DT_TYPEDEF:
Alexandre Julliard01d63461997-01-20 19:43:45 +00001012 fprintf(stderr, "What???\n");
1013 break;
1014 }
1015
1016 return TRUE;
1017}
1018
1019