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.
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
38 long int types_extract_as_integer(const struct dbg_lvalue* lvalue)
43 if (lvalue->type.id == dbg_itype_none ||
44 !types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
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);
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 !! */
68 /* propagate sign information */
69 if (((size & 3) != 0) && (rtn >> (size * 8 - 1)) != 0)
70 rtn |= (-1) << (size * 8);
76 RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL);
79 case SymTagPointerType:
80 if (!memory_read_value(lvalue, sizeof(void*), &rtn)) return 0;
84 assert(lvalue->cookie == DLV_TARGET);
85 if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) return 0;
88 assert(lvalue->cookie == DLV_TARGET);
89 if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) return 0;
92 WINE_FIXME("Unsupported tag %lu\n", tag);
100 /******************************************************************
104 BOOL types_deref(const struct dbg_lvalue* lvalue, struct dbg_lvalue* result)
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))
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...
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
135 result->addr.Mode = AddrModeFlat;
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;
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))
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;
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))))
185 if (types_get_info(type, TI_GET_OFFSET, &offset))
187 lvalue->addr.Offset += offset;
193 /******************************************************************
194 * types_udt_find_element
197 BOOL types_udt_find_element(struct dbg_lvalue* lvalue, const char* name, long int* tmpbuf)
200 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
201 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
205 struct dbg_type type;
207 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag) ||
211 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &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++)
223 type.id = fcp->ChildId[i];
224 types_get_info(&type, TI_GET_SYMNAME, &ptr);
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);
240 /******************************************************************
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))
254 case SymTagArrayType:
255 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
256 if (index < 0 || index >= count) return FALSE;
258 case SymTagPointerType:
260 * Get the base type, so we know how much to index by.
262 types_get_info(&lvalue->type, TI_GET_TYPE, &result->type.id);
263 result->type.module = lvalue->type.module;
264 types_get_info(&result->type, TI_GET_LENGTH, &length);
265 /* Contents of array must be on same target */
266 result->addr.Mode = lvalue->addr.Mode;
267 memory_read_value(lvalue, sizeof(result->addr.Offset), &result->addr.Offset);
268 result->addr.Offset += index * length;
278 unsigned long result; /* out: the found type */
279 enum SymTagEnum tag; /* in: the tag to look for */
282 unsigned long typeid; /* when tag is SymTagUDT */
283 const char* name; /* when tag is SymTagPointerType */
287 static BOOL CALLBACK types_cb(PSYMBOL_INFO sym, ULONG size, void* _user)
289 struct type_find_t* user = (struct type_find_t*)_user;
291 struct dbg_type type;
294 if (sym->Tag == user->tag)
299 if (!strcmp(user->u.name, sym->Name))
301 user->result = sym->TypeIndex;
305 case SymTagPointerType:
306 type.module = sym->ModBase;
307 type.id = sym->TypeIndex;
308 types_get_info(&type, TI_GET_TYPE, &type_id);
309 if (types_get_info(&type, TI_GET_TYPE, &type_id) && type_id == user->u.typeid)
311 user->result = sym->TypeIndex;
321 /******************************************************************
324 * Should look up in module based at linear whether (typeid*) exists
325 * Otherwise, we could create it locally
327 struct dbg_type types_find_pointer(const struct dbg_type* type)
329 struct type_find_t f;
332 f.result = dbg_itype_none;
333 f.tag = SymTagPointerType;
334 f.u.typeid = type->id;
335 SymEnumTypes(dbg_curr_process->handle, type->module, types_cb, &f);
336 ret.module = type->module;
341 /******************************************************************
344 * Should look up in the module based at linear address whether a type
345 * named 'name' and with the correct tag exists
347 struct dbg_type types_find_type(unsigned long linear, const char* name, enum SymTagEnum tag)
350 struct type_find_t f;
353 f.result = dbg_itype_none;
356 SymEnumTypes(dbg_curr_process->handle, linear, types_cb, &f);
362 /***********************************************************************
365 * Implementation of the 'print' command.
367 void print_value(const struct dbg_lvalue* lvalue, char format, int level)
369 struct dbg_lvalue lvalue_field;
375 if (lvalue->type.id == dbg_itype_none)
377 /* No type, just print the addr value */
378 print_bare_address(&lvalue->addr);
382 if (format == 'i' || format == 's' || format == 'w' || format == 'b' || format == 'g')
384 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
388 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
390 WINE_FIXME("---error\n");
397 case SymTagPointerType:
398 print_basic(lvalue, 1, format);
401 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
403 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
404 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
408 struct dbg_type type;
414 fcp->Count = min(count, 256);
415 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
417 for (i = 0; i < min(fcp->Count, count); i++)
420 type.module = lvalue->type.module;
421 type.id = fcp->ChildId[i];
422 types_get_info(&type, TI_GET_SYMNAME, &ptr);
424 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
425 dbg_printf("%s=", tmp);
426 HeapFree(GetProcessHeap(), 0, ptr);
427 lvalue_field = *lvalue;
428 if (types_get_udt_element_lvalue(&lvalue_field, &type, &tmpbuf))
430 print_value(&lvalue_field, format, level + 1);
432 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
435 count -= min(count, 256);
441 case SymTagArrayType:
443 * Loop over all of the entries, printing stuff as we go.
446 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
447 types_get_info(&lvalue->type, TI_GET_LENGTH, &size);
454 * Special handling for character arrays.
456 /* FIXME should check basic type here (should be a char!!!!)... */
457 len = min(count, sizeof(buffer));
458 memory_get_string(dbg_curr_process->handle,
459 memory_to_linear_addr(&lvalue->addr),
460 lvalue->cookie == DLV_TARGET, TRUE, buffer, len);
461 dbg_printf("\"%s%s\"", buffer, (len < count) ? "..." : "");
464 lvalue_field = *lvalue;
465 types_get_info(&lvalue->type, TI_GET_TYPE, &lvalue_field.type.id);
467 for (i = 0; i < count; i++)
469 print_value(&lvalue_field, format, level + 1);
470 lvalue_field.addr.Offset += size / count;
471 dbg_printf((i == count - 1) ? "}" : ", ");
474 case SymTagFunctionType:
475 dbg_printf("Function ");
476 print_bare_address(&lvalue->addr);
478 types_print_type(&lvalue->type, FALSE);
481 WINE_FIXME("Unknown tag (%lu)\n", tag);
482 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
488 if (level == 0) dbg_printf("\n");
491 static BOOL CALLBACK print_types_cb(PSYMBOL_INFO sym, ULONG size, void* ctx)
493 struct dbg_type type;
494 type.module = sym->ModBase;
495 type.id = sym->TypeIndex;
496 dbg_printf("Mod: %08lx ID: %08lx \n", type.module, type.id);
497 types_print_type(&type, TRUE);
502 static BOOL CALLBACK print_types_mod_cb(PSTR mod_name, DWORD base, void* ctx)
504 return SymEnumTypes(dbg_curr_process->handle, base, print_types_cb, ctx);
507 int print_types(void)
509 SymEnumerateModules(dbg_curr_process->handle, print_types_mod_cb, NULL);
513 int types_print_type(const struct dbg_type* type, BOOL details)
518 DWORD tag, udt, count;
519 struct dbg_type subtype;
521 if (type->id == dbg_itype_none || !types_get_info(type, TI_GET_SYMTAG, &tag))
523 dbg_printf("--invalid--<%lxh>--", type->id);
527 if (types_get_info(type, TI_GET_SYMNAME, &ptr) && ptr)
529 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
531 HeapFree(GetProcessHeap(), 0, ptr);
533 else name = "--none--";
538 if (details) dbg_printf("Basic<%s>", name); else dbg_printf("%s", name);
540 case SymTagPointerType:
541 types_get_info(type, TI_GET_TYPE, &subtype.id);
542 subtype.module = type->module;
543 types_print_type(&subtype, FALSE);
547 types_get_info(type, TI_GET_UDTKIND, &udt);
550 case UdtStruct: dbg_printf("struct %s", name); break;
551 case UdtUnion: dbg_printf("union %s", name); break;
552 case UdtClass: dbg_printf("class %s", name); break;
553 default: WINE_ERR("Unsupported UDT type (%ld) for %s", udt, name); break;
556 types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
558 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
559 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
563 struct dbg_type type_elt;
569 fcp->Count = min(count, 256);
570 if (types_get_info(type, TI_FINDCHILDREN, fcp))
572 for (i = 0; i < min(fcp->Count, count); i++)
575 type_elt.module = type->module;
576 type_elt.id = fcp->ChildId[i];
577 types_get_info(&type_elt, TI_GET_SYMNAME, &ptr);
579 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
580 HeapFree(GetProcessHeap(), 0, ptr);
581 dbg_printf("%s", tmp);
582 if (types_get_info(&type_elt, TI_GET_TYPE, &type_elt.id))
585 types_print_type(&type_elt, details);
587 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
590 count -= min(count, 256);
596 case SymTagArrayType:
597 types_get_info(type, TI_GET_TYPE, &subtype.id);
598 subtype.module = type->module;
599 types_print_type(&subtype, details);
600 dbg_printf(" %s[]", name);
603 dbg_printf("enum %s", name);
605 case SymTagFunctionType:
606 types_get_info(type, TI_GET_TYPE, &subtype.id);
607 subtype.module = type->module;
608 types_print_type(&subtype, FALSE);
609 dbg_printf(" (*%s)(", name);
610 if (types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
612 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
613 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
619 fcp->Count = min(count, 256);
620 if (types_get_info(type, TI_FINDCHILDREN, fcp))
622 for (i = 0; i < min(fcp->Count, count); i++)
624 subtype.id = fcp->ChildId[i];
625 types_print_type(&subtype, FALSE);
626 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
629 count -= min(count, 256);
636 WINE_ERR("Unknown type %lu for %s\n", tag, name);
643 BOOL types_get_info(const struct dbg_type* type, IMAGEHLP_SYMBOL_TYPE_INFO ti, void* pInfo)
645 if (type->id == dbg_itype_none) return FALSE;
646 if (type->module != 0)
647 return SymGetTypeInfo(dbg_curr_process->handle, type->module, type->id, ti, pInfo);
649 assert(type->id >= dbg_itype_first);
650 /* helper to typecast pInfo to its expected type (_t) */
651 #define X(_t) (*((_t*)pInfo))
655 case dbg_itype_unsigned_int:
658 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
659 case TI_GET_LENGTH: X(DWORD) = 4; break;
660 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
661 default: WINE_FIXME("unsupported %u for u-int\n", ti); return FALSE;
664 case dbg_itype_signed_int:
667 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
668 case TI_GET_LENGTH: X(DWORD) = 4; break;
669 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
670 default: WINE_FIXME("unsupported %u for s-int\n", ti); return FALSE;
673 case dbg_itype_unsigned_short_int:
676 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
677 case TI_GET_LENGTH: X(DWORD) = 2; break;
678 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
679 default: WINE_FIXME("unsupported %u for u-short int\n", ti); return FALSE;
682 case dbg_itype_signed_short_int:
685 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
686 case TI_GET_LENGTH: X(DWORD) = 2; break;
687 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
688 default: WINE_FIXME("unsupported %u for s-short int\n", ti); return FALSE;
691 case dbg_itype_unsigned_char_int:
694 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
695 case TI_GET_LENGTH: X(DWORD) = 1; break;
696 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
697 default: WINE_FIXME("unsupported %u for u-char int\n", ti); return FALSE;
700 case dbg_itype_signed_char_int:
703 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
704 case TI_GET_LENGTH: X(DWORD) = 1; break;
705 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
706 default: WINE_FIXME("unsupported %u for s-char int\n", ti); return FALSE;
712 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
713 case TI_GET_LENGTH: X(DWORD) = 1; break;
714 case TI_GET_BASETYPE: X(DWORD) = btChar; break;
715 default: WINE_FIXME("unsupported %u for char int\n", ti); return FALSE;
718 case dbg_itype_astring:
721 case TI_GET_SYMTAG: X(DWORD) = SymTagPointerType; break;
722 case TI_GET_LENGTH: X(DWORD) = 4; break;
723 case TI_GET_TYPE: X(DWORD) = dbg_itype_char; break;
724 default: WINE_FIXME("unsupported %u for a string\n", ti); return FALSE;
727 default: WINE_FIXME("unsupported type id 0x%lx\n", type->id);