Fixed array dereferencing.
[wine/multimedia.git] / programs / winedbg / types.c
blob1d9297203eadb29910a965da87bc17d6b49e801f
1 /*
2 * File types.c - datatype handling stuff for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
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.
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.
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
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.
24 #include "config.h"
25 #include <stdlib.h>
27 #include "debugger.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
32 /******************************************************************
33 * types_extract_as_integer
35 * Given a lvalue, try to get an integral (or pointer/address) value
36 * out of it
38 long int types_extract_as_integer(const struct dbg_lvalue* lvalue)
40 long int rtn = 0;
41 DWORD tag, size, bt;
43 if (lvalue->type.id == dbg_itype_none ||
44 !types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
45 return 0;
47 switch (tag)
49 case SymTagBaseType:
50 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size) ||
51 !types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt))
53 WINE_ERR("Couldn't get information\n");
54 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
56 if (size > sizeof(rtn))
58 WINE_ERR("Size too large (%lu)\n", size);
59 return 0;
61 /* FIXME: we have an ugly & non portable thing here !!! */
62 if (!memory_read_value(lvalue, size, &rtn)) return 0;
64 /* now let's do some promotions !! */
65 switch (bt)
67 case btInt:
68 /* propagate sign information */
69 if (((size & 3) != 0) && (rtn >> (size * 8 - 1)) != 0)
70 rtn |= (-1) << (size * 8);
71 break;
72 case btUInt:
73 case btChar:
74 break;
75 case btFloat:
76 RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL);
78 break;
79 case SymTagPointerType:
80 if (!memory_read_value(lvalue, sizeof(void*), &rtn)) return 0;
81 break;
82 case SymTagArrayType:
83 case SymTagUDT:
84 assert(lvalue->cookie == DLV_TARGET);
85 if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) return 0;
86 break;
87 case SymTagEnum:
88 assert(lvalue->cookie == DLV_TARGET);
89 if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) return 0;
90 break;
91 default:
92 WINE_FIXME("Unsupported tag %lu\n", tag);
93 rtn = 0;
94 break;
97 return rtn;
100 /******************************************************************
101 * types_deref
104 BOOL types_deref(const struct dbg_lvalue* lvalue, struct dbg_lvalue* result)
106 DWORD tag;
108 memset(result, 0, sizeof(*result));
109 result->type.id = dbg_itype_none;
110 result->type.module = 0;
113 * Make sure that this really makes sense.
115 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag) ||
116 tag != SymTagPointerType ||
117 !memory_read_value(lvalue, sizeof(result->addr.Offset), &result->addr.Offset) ||
118 !types_get_info(&lvalue->type, TI_GET_TYPE, &result->type.id))
119 return FALSE;
120 result->type.module = lvalue->type.module;
121 result->cookie = DLV_TARGET;
122 /* FIXME: this is currently buggy.
123 * there is no way to tell were the deref:ed value is...
124 * for example:
125 * x is a pointer to struct s, x being on the stack
126 * => lvalue is in debuggee, result is in debugger
127 * x is a pointer to struct s, x being optimized into a reg
128 * => lvalue is debugger, result is debuggee
129 * x is a pointer to internal variable x
130 * => lvalue is debugger, result is debuggee
131 * so we force debuggee address space, because dereferencing pointers to
132 * internal variables is very unlikely. A correct fix would be
133 * rather large.
135 result->addr.Mode = AddrModeFlat;
136 return TRUE;
139 /******************************************************************
140 * types_get_udt_element_lvalue
142 * Implement a structure derefencement
144 static BOOL types_get_udt_element_lvalue(struct dbg_lvalue* lvalue,
145 const struct dbg_type* type, long int* tmpbuf)
147 DWORD offset, length, bitoffset;
148 DWORD bt;
149 unsigned mask;
151 types_get_info(type, TI_GET_TYPE, &lvalue->type.id);
152 lvalue->type.module = type->module;
153 types_get_info(type, TI_GET_OFFSET, &offset);
155 if (types_get_info(type, TI_GET_BITPOSITION, &bitoffset))
157 if (!types_get_info(type, TI_GET_LENGTH, &length) ||
158 length > sizeof(*tmpbuf))
159 return FALSE;
161 * Bitfield operation. We have to extract the field and store
162 * it in a temporary buffer so that we get it all right.
164 lvalue->addr.Offset += offset;
165 if (!memory_read_value(lvalue, sizeof(*tmpbuf), tmpbuf)) return FALSE;
166 mask = 0xffffffff << length;
167 *tmpbuf >>= bitoffset & 7;
168 *tmpbuf &= ~mask;
170 lvalue->cookie = DLV_HOST;
171 lvalue->addr.Offset = (DWORD)tmpbuf;
174 * OK, now we have the correct part of the number.
175 * Check to see whether the basic type is signed or not, and if so,
176 * we need to sign extend the number.
178 if (types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt) &&
179 bt == btInt && (*tmpbuf & (1 << (length - 1))))
181 *tmpbuf |= mask;
183 return TRUE;
185 if (types_get_info(type, TI_GET_OFFSET, &offset))
187 lvalue->addr.Offset += offset;
188 return TRUE;
190 return FALSE;
193 /******************************************************************
194 * types_udt_find_element
197 BOOL types_udt_find_element(struct dbg_lvalue* lvalue, const char* name, long int* tmpbuf)
199 DWORD tag, count;
200 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
201 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
202 WCHAR* ptr;
203 char tmp[256];
204 int i;
205 struct dbg_type type;
207 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag) ||
208 tag != SymTagUDT)
209 return FALSE;
211 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
213 fcp->Start = 0;
214 while (count)
216 fcp->Count = min(count, 256);
217 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
219 type.module = lvalue->type.module;
220 for (i = 0; i < min(fcp->Count, count); i++)
222 ptr = NULL;
223 type.id = fcp->ChildId[i];
224 types_get_info(&type, TI_GET_SYMNAME, &ptr);
225 if (!ptr) continue;
226 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
227 HeapFree(GetProcessHeap(), 0, ptr);
228 if (strcmp(tmp, name)) continue;
230 return types_get_udt_element_lvalue(lvalue, &type, tmpbuf);
233 count -= min(count, 256);
234 fcp->Start += 256;
237 return FALSE;
240 /******************************************************************
241 * types_array_index
243 * Grab an element from an array
245 BOOL types_array_index(const struct dbg_lvalue* lvalue, int index,
246 struct dbg_lvalue* result)
248 DWORD tag, length, count;
250 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
251 return FALSE;
252 switch (tag)
254 case SymTagArrayType:
255 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
256 if (index < 0 || index >= count) return FALSE;
257 /* fall through */
258 case SymTagPointerType:
259 /* Contents of array share same data (addr mode, module...) */
260 *result = *lvalue;
262 * Get the base type, so we know how much to index by.
264 types_get_info(&lvalue->type, TI_GET_TYPE, &result->type.id);
265 types_get_info(&result->type, TI_GET_LENGTH, &length);
266 memory_read_value(lvalue, sizeof(result->addr.Offset), &result->addr.Offset);
267 result->addr.Offset += index * length;
268 break;
269 default:
270 assert(FALSE);
272 return TRUE;
275 struct type_find_t
277 unsigned long result; /* out: the found type */
278 enum SymTagEnum tag; /* in: the tag to look for */
279 union
281 unsigned long typeid; /* when tag is SymTagUDT */
282 const char* name; /* when tag is SymTagPointerType */
283 } u;
286 static BOOL CALLBACK types_cb(PSYMBOL_INFO sym, ULONG size, void* _user)
288 struct type_find_t* user = (struct type_find_t*)_user;
289 BOOL ret = TRUE;
290 struct dbg_type type;
291 DWORD type_id;
293 if (sym->Tag == user->tag)
295 switch (user->tag)
297 case SymTagUDT:
298 if (!strcmp(user->u.name, sym->Name))
300 user->result = sym->TypeIndex;
301 ret = FALSE;
303 break;
304 case SymTagPointerType:
305 type.module = sym->ModBase;
306 type.id = sym->TypeIndex;
307 if (types_get_info(&type, TI_GET_TYPE, &type_id) && type_id == user->u.typeid)
309 user->result = sym->TypeIndex;
310 ret = FALSE;
312 break;
313 default: break;
316 return ret;
319 /******************************************************************
320 * types_find_pointer
322 * Should look up in module based at linear whether (typeid*) exists
323 * Otherwise, we could create it locally
325 struct dbg_type types_find_pointer(const struct dbg_type* type)
327 struct type_find_t f;
328 struct dbg_type ret;
330 f.result = dbg_itype_none;
331 f.tag = SymTagPointerType;
332 f.u.typeid = type->id;
333 SymEnumTypes(dbg_curr_process->handle, type->module, types_cb, &f);
334 ret.module = type->module;
335 ret.id = f.result;
336 return ret;
339 /******************************************************************
340 * types_find_type
342 * Should look up in the module based at linear address whether a type
343 * named 'name' and with the correct tag exists
345 struct dbg_type types_find_type(unsigned long linear, const char* name, enum SymTagEnum tag)
348 struct type_find_t f;
349 struct dbg_type ret;
351 f.result = dbg_itype_none;
352 f.tag = tag;
353 f.u.name = name;
354 SymEnumTypes(dbg_curr_process->handle, linear, types_cb, &f);
355 ret.module = linear;
356 ret.id = f.result;
357 return ret;
360 /***********************************************************************
361 * print_value
363 * Implementation of the 'print' command.
365 void print_value(const struct dbg_lvalue* lvalue, char format, int level)
367 struct dbg_lvalue lvalue_field;
368 int i;
369 DWORD tag;
370 DWORD count;
371 DWORD size;
373 if (lvalue->type.id == dbg_itype_none)
375 /* No type, just print the addr value */
376 print_bare_address(&lvalue->addr);
377 goto leave;
380 if (format == 'i' || format == 's' || format == 'w' || format == 'b' || format == 'g')
382 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
383 format = '\0';
386 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
388 WINE_FIXME("---error\n");
389 return;
391 switch (tag)
393 case SymTagBaseType:
394 case SymTagEnum:
395 case SymTagPointerType:
396 print_basic(lvalue, 1, format);
397 break;
398 case SymTagUDT:
399 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
401 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
402 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
403 WCHAR* ptr;
404 char tmp[256];
405 long int tmpbuf;
406 struct dbg_type type;
408 dbg_printf("{");
409 fcp->Start = 0;
410 while (count)
412 fcp->Count = min(count, 256);
413 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
415 for (i = 0; i < min(fcp->Count, count); i++)
417 ptr = NULL;
418 type.module = lvalue->type.module;
419 type.id = fcp->ChildId[i];
420 types_get_info(&type, TI_GET_SYMNAME, &ptr);
421 if (!ptr) continue;
422 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
423 dbg_printf("%s=", tmp);
424 HeapFree(GetProcessHeap(), 0, ptr);
425 lvalue_field = *lvalue;
426 if (types_get_udt_element_lvalue(&lvalue_field, &type, &tmpbuf))
428 print_value(&lvalue_field, format, level + 1);
430 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
433 count -= min(count, 256);
434 fcp->Start += 256;
436 dbg_printf("}");
438 break;
439 case SymTagArrayType:
441 * Loop over all of the entries, printing stuff as we go.
443 count = 1; size = 1;
444 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
445 types_get_info(&lvalue->type, TI_GET_LENGTH, &size);
447 if (size == count)
449 unsigned len;
450 char buffer[256];
452 * Special handling for character arrays.
454 /* FIXME should check basic type here (should be a char!!!!)... */
455 len = min(count, sizeof(buffer));
456 memory_get_string(dbg_curr_process,
457 memory_to_linear_addr(&lvalue->addr),
458 lvalue->cookie == DLV_TARGET, TRUE, buffer, len);
459 dbg_printf("\"%s%s\"", buffer, (len < count) ? "..." : "");
460 break;
462 lvalue_field = *lvalue;
463 types_get_info(&lvalue->type, TI_GET_TYPE, &lvalue_field.type.id);
464 dbg_printf("{");
465 for (i = 0; i < count; i++)
467 print_value(&lvalue_field, format, level + 1);
468 lvalue_field.addr.Offset += size / count;
469 dbg_printf((i == count - 1) ? "}" : ", ");
471 break;
472 case SymTagFunctionType:
473 dbg_printf("Function ");
474 print_bare_address(&lvalue->addr);
475 dbg_printf(": ");
476 types_print_type(&lvalue->type, FALSE);
477 break;
478 default:
479 WINE_FIXME("Unknown tag (%lu)\n", tag);
480 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
481 break;
484 leave:
486 if (level == 0) dbg_printf("\n");
489 static BOOL CALLBACK print_types_cb(PSYMBOL_INFO sym, ULONG size, void* ctx)
491 struct dbg_type type;
492 type.module = sym->ModBase;
493 type.id = sym->TypeIndex;
494 dbg_printf("Mod: %08lx ID: %08lx \n", type.module, type.id);
495 types_print_type(&type, TRUE);
496 dbg_printf("\n");
497 return TRUE;
500 static BOOL CALLBACK print_types_mod_cb(PSTR mod_name, DWORD base, void* ctx)
502 return SymEnumTypes(dbg_curr_process->handle, base, print_types_cb, ctx);
505 int print_types(void)
507 SymEnumerateModules(dbg_curr_process->handle, print_types_mod_cb, NULL);
508 return 0;
511 int types_print_type(const struct dbg_type* type, BOOL details)
513 WCHAR* ptr;
514 char tmp[256];
515 const char* name;
516 DWORD tag, udt, count;
517 struct dbg_type subtype;
519 if (type->id == dbg_itype_none || !types_get_info(type, TI_GET_SYMTAG, &tag))
521 dbg_printf("--invalid--<%lxh>--", type->id);
522 return FALSE;
525 if (types_get_info(type, TI_GET_SYMNAME, &ptr) && ptr)
527 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
528 name = tmp;
529 HeapFree(GetProcessHeap(), 0, ptr);
531 else name = "--none--";
533 switch (tag)
535 case SymTagBaseType:
536 if (details) dbg_printf("Basic<%s>", name); else dbg_printf("%s", name);
537 break;
538 case SymTagPointerType:
539 types_get_info(type, TI_GET_TYPE, &subtype.id);
540 subtype.module = type->module;
541 types_print_type(&subtype, FALSE);
542 dbg_printf("*");
543 break;
544 case SymTagUDT:
545 types_get_info(type, TI_GET_UDTKIND, &udt);
546 switch (udt)
548 case UdtStruct: dbg_printf("struct %s", name); break;
549 case UdtUnion: dbg_printf("union %s", name); break;
550 case UdtClass: dbg_printf("class %s", name); break;
551 default: WINE_ERR("Unsupported UDT type (%ld) for %s", udt, name); break;
553 if (details &&
554 types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
556 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
557 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
558 WCHAR* ptr;
559 char tmp[256];
560 int i;
561 struct dbg_type type_elt;
562 dbg_printf(" {");
564 fcp->Start = 0;
565 while (count)
567 fcp->Count = min(count, 256);
568 if (types_get_info(type, TI_FINDCHILDREN, fcp))
570 for (i = 0; i < min(fcp->Count, count); i++)
572 ptr = NULL;
573 type_elt.module = type->module;
574 type_elt.id = fcp->ChildId[i];
575 types_get_info(&type_elt, TI_GET_SYMNAME, &ptr);
576 if (!ptr) continue;
577 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
578 HeapFree(GetProcessHeap(), 0, ptr);
579 dbg_printf("%s", tmp);
580 if (types_get_info(&type_elt, TI_GET_TYPE, &type_elt.id))
582 dbg_printf(":");
583 types_print_type(&type_elt, details);
585 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
588 count -= min(count, 256);
589 fcp->Start += 256;
591 dbg_printf("}");
593 break;
594 case SymTagArrayType:
595 types_get_info(type, TI_GET_TYPE, &subtype.id);
596 subtype.module = type->module;
597 types_print_type(&subtype, details);
598 dbg_printf(" %s[]", name);
599 break;
600 case SymTagEnum:
601 dbg_printf("enum %s", name);
602 break;
603 case SymTagFunctionType:
604 types_get_info(type, TI_GET_TYPE, &subtype.id);
605 subtype.module = type->module;
606 types_print_type(&subtype, FALSE);
607 dbg_printf(" (*%s)(", name);
608 if (types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
610 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
611 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
612 int i;
614 fcp->Start = 0;
615 while (count)
617 fcp->Count = min(count, 256);
618 if (types_get_info(type, TI_FINDCHILDREN, fcp))
620 for (i = 0; i < min(fcp->Count, count); i++)
622 subtype.id = fcp->ChildId[i];
623 types_print_type(&subtype, FALSE);
624 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
627 count -= min(count, 256);
628 fcp->Start += 256;
631 dbg_printf(")");
632 break;
633 default:
634 WINE_ERR("Unknown type %lu for %s\n", tag, name);
635 break;
638 return TRUE;
641 BOOL types_get_info(const struct dbg_type* type, IMAGEHLP_SYMBOL_TYPE_INFO ti, void* pInfo)
643 if (type->id == dbg_itype_none) return FALSE;
644 if (type->module != 0)
645 return SymGetTypeInfo(dbg_curr_process->handle, type->module, type->id, ti, pInfo);
647 assert(type->id >= dbg_itype_first);
648 /* helper to typecast pInfo to its expected type (_t) */
649 #define X(_t) (*((_t*)pInfo))
651 switch (type->id)
653 case dbg_itype_unsigned_int:
654 switch (ti)
656 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
657 case TI_GET_LENGTH: X(DWORD) = 4; break;
658 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
659 default: WINE_FIXME("unsupported %u for u-int\n", ti); return FALSE;
661 break;
662 case dbg_itype_signed_int:
663 switch (ti)
665 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
666 case TI_GET_LENGTH: X(DWORD) = 4; break;
667 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
668 default: WINE_FIXME("unsupported %u for s-int\n", ti); return FALSE;
670 break;
671 case dbg_itype_unsigned_short_int:
672 switch (ti)
674 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
675 case TI_GET_LENGTH: X(DWORD) = 2; break;
676 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
677 default: WINE_FIXME("unsupported %u for u-short int\n", ti); return FALSE;
679 break;
680 case dbg_itype_signed_short_int:
681 switch (ti)
683 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
684 case TI_GET_LENGTH: X(DWORD) = 2; break;
685 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
686 default: WINE_FIXME("unsupported %u for s-short int\n", ti); return FALSE;
688 break;
689 case dbg_itype_unsigned_char_int:
690 switch (ti)
692 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
693 case TI_GET_LENGTH: X(DWORD) = 1; break;
694 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
695 default: WINE_FIXME("unsupported %u for u-char int\n", ti); return FALSE;
697 break;
698 case dbg_itype_signed_char_int:
699 switch (ti)
701 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
702 case TI_GET_LENGTH: X(DWORD) = 1; break;
703 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
704 default: WINE_FIXME("unsupported %u for s-char int\n", ti); return FALSE;
706 break;
707 case dbg_itype_char:
708 switch (ti)
710 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
711 case TI_GET_LENGTH: X(DWORD) = 1; break;
712 case TI_GET_BASETYPE: X(DWORD) = btChar; break;
713 default: WINE_FIXME("unsupported %u for char int\n", ti); return FALSE;
715 break;
716 case dbg_itype_astring:
717 switch (ti)
719 case TI_GET_SYMTAG: X(DWORD) = SymTagPointerType; break;
720 case TI_GET_LENGTH: X(DWORD) = 4; break;
721 case TI_GET_TYPE: X(DWORD) = dbg_itype_char; break;
722 default: WINE_FIXME("unsupported %u for a string\n", ti); return FALSE;
724 break;
725 default: WINE_FIXME("unsupported type id 0x%lx\n", type->id);
728 #undef X
729 return TRUE;