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)
45 if (lvalue->type.id == dbg_itype_none ||
46 !types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
49 if (lvalue->type.id == dbg_itype_segptr)
51 return (long int)memory_to_linear_addr(&lvalue->addr);
57 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size) ||
58 !types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt))
60 WINE_ERR("Couldn't get information\n");
61 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
63 if (size > sizeof(rtn))
65 WINE_ERR("Size too large (%s)\n", wine_dbgstr_longlong(size));
72 if (!be_cpu->fetch_integer(lvalue, (unsigned)size, TRUE, &val)) return 0;
76 if (!be_cpu->fetch_integer(lvalue, (unsigned)size, FALSE, &val)) return 0;
77 rtn = (DWORD)(DWORD64)val;
80 RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL);
83 case SymTagPointerType:
84 if (!memory_read_value(lvalue, sizeof(void*), &rtn)) return 0;
88 assert(lvalue->cookie == DLV_TARGET);
89 if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) return 0;
92 assert(lvalue->cookie == DLV_TARGET);
93 if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) return 0;
96 WINE_FIXME("Unsupported tag %lu\n", tag);
104 /******************************************************************
105 * types_extract_as_address
109 void types_extract_as_address(const struct dbg_lvalue* lvalue, ADDRESS* addr)
111 if (lvalue->type.id == dbg_itype_segptr && lvalue->type.module == 0)
113 *addr = lvalue->addr;
117 addr->Mode = AddrModeFlat;
118 addr->Offset = types_extract_as_integer( lvalue );
122 /******************************************************************
126 BOOL types_deref(const struct dbg_lvalue* lvalue, struct dbg_lvalue* result)
130 memset(result, 0, sizeof(*result));
131 result->type.id = dbg_itype_none;
132 result->type.module = 0;
135 * Make sure that this really makes sense.
137 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag) ||
138 tag != SymTagPointerType ||
139 !memory_read_value(lvalue, sizeof(result->addr.Offset), &result->addr.Offset) ||
140 !types_get_info(&lvalue->type, TI_GET_TYPE, &result->type.id))
142 result->type.module = lvalue->type.module;
143 result->cookie = DLV_TARGET;
144 /* FIXME: this is currently buggy.
145 * there is no way to tell were the deref:ed value is...
147 * x is a pointer to struct s, x being on the stack
148 * => lvalue is in debuggee, result is in debugger
149 * x is a pointer to struct s, x being optimized into a reg
150 * => lvalue is debugger, result is debuggee
151 * x is a pointer to internal variable x
152 * => lvalue is debugger, result is debuggee
153 * so we force debuggee address space, because dereferencing pointers to
154 * internal variables is very unlikely. A correct fix would be
157 result->addr.Mode = AddrModeFlat;
161 /******************************************************************
162 * types_get_udt_element_lvalue
164 * Implement a structure derefencement
166 static BOOL types_get_udt_element_lvalue(struct dbg_lvalue* lvalue,
167 const struct dbg_type* type, long int* tmpbuf)
169 DWORD offset, bitoffset;
175 types_get_info(type, TI_GET_TYPE, &lvalue->type.id);
176 lvalue->type.module = type->module;
177 if (!types_get_info(type, TI_GET_OFFSET, &offset)) return FALSE;
178 lvalue->addr.Offset += offset;
180 if (types_get_info(type, TI_GET_BITPOSITION, &bitoffset))
182 types_get_info(type, TI_GET_LENGTH, &length);
183 /* FIXME: this test isn't sufficient, depending on start of bitfield
184 * (ie a 32 bit field can spread across 5 bytes)
186 if (length > 8 * sizeof(*tmpbuf)) return FALSE;
187 lvalue->addr.Offset += bitoffset >> 3;
189 * Bitfield operation. We have to extract the field and store
190 * it in a temporary buffer so that we get it all right.
192 if (!memory_read_value(lvalue, sizeof(*tmpbuf), tmpbuf)) return FALSE;
193 mask = 0xffffffff << (DWORD)length;
194 *tmpbuf >>= bitoffset & 7;
197 lvalue->cookie = DLV_HOST;
198 lvalue->addr.Offset = (DWORD)tmpbuf;
201 * OK, now we have the correct part of the number.
202 * Check to see whether the basic type is signed or not, and if so,
203 * we need to sign extend the number.
205 if (types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt) &&
206 bt == btInt && (*tmpbuf & (1 << ((DWORD)length - 1))))
213 if (!memory_read_value(lvalue, sizeof(*tmpbuf), tmpbuf)) return FALSE;
219 /******************************************************************
220 * types_udt_find_element
223 BOOL types_udt_find_element(struct dbg_lvalue* lvalue, const char* name, long int* tmpbuf)
226 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
227 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
231 struct dbg_type type;
233 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag) ||
237 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
242 fcp->Count = min(count, 256);
243 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
245 type.module = lvalue->type.module;
246 for (i = 0; i < min(fcp->Count, count); i++)
249 type.id = fcp->ChildId[i];
250 types_get_info(&type, TI_GET_SYMNAME, &ptr);
252 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
253 HeapFree(GetProcessHeap(), 0, ptr);
254 if (strcmp(tmp, name)) continue;
256 return types_get_udt_element_lvalue(lvalue, &type, tmpbuf);
259 count -= min(count, 256);
266 /******************************************************************
269 * Grab an element from an array
271 BOOL types_array_index(const struct dbg_lvalue* lvalue, int index,
272 struct dbg_lvalue* result)
277 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
281 case SymTagArrayType:
282 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
283 if (index < 0 || index >= count) return FALSE;
285 case SymTagPointerType:
286 /* Contents of array share same data (addr mode, module...) */
289 * Get the base type, so we know how much to index by.
291 types_get_info(&lvalue->type, TI_GET_TYPE, &result->type.id);
292 types_get_info(&result->type, TI_GET_LENGTH, &length);
293 memory_read_value(lvalue, sizeof(result->addr.Offset), &result->addr.Offset);
294 result->addr.Offset += index * (DWORD)length;
304 unsigned long result; /* out: the found type */
305 enum SymTagEnum tag; /* in: the tag to look for */
308 unsigned long typeid; /* when tag is SymTagUDT */
309 const char* name; /* when tag is SymTagPointerType */
313 static BOOL CALLBACK types_cb(PSYMBOL_INFO sym, ULONG size, void* _user)
315 struct type_find_t* user = (struct type_find_t*)_user;
317 struct dbg_type type;
320 if (sym->Tag == user->tag)
325 if (!strcmp(user->u.name, sym->Name))
327 user->result = sym->TypeIndex;
331 case SymTagPointerType:
332 type.module = sym->ModBase;
333 type.id = sym->TypeIndex;
334 if (types_get_info(&type, TI_GET_TYPE, &type_id) && type_id == user->u.typeid)
336 user->result = sym->TypeIndex;
346 /******************************************************************
349 * Should look up in module based at linear whether (typeid*) exists
350 * Otherwise, we could create it locally
352 struct dbg_type types_find_pointer(const struct dbg_type* type)
354 struct type_find_t f;
357 f.result = dbg_itype_none;
358 f.tag = SymTagPointerType;
359 f.u.typeid = type->id;
360 SymEnumTypes(dbg_curr_process->handle, type->module, types_cb, &f);
361 ret.module = type->module;
366 /******************************************************************
369 * Should look up in the module based at linear address whether a type
370 * named 'name' and with the correct tag exists
372 struct dbg_type types_find_type(unsigned long linear, const char* name, enum SymTagEnum tag)
375 struct type_find_t f;
378 f.result = dbg_itype_none;
381 SymEnumTypes(dbg_curr_process->handle, linear, types_cb, &f);
387 /***********************************************************************
390 * Implementation of the 'print' command.
392 void print_value(const struct dbg_lvalue* lvalue, char format, int level)
394 struct dbg_lvalue lvalue_field;
400 if (lvalue->type.id == dbg_itype_none)
402 /* No type, just print the addr value */
403 print_bare_address(&lvalue->addr);
407 if (format == 'i' || format == 's' || format == 'w' || format == 'b' || format == 'g')
409 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
413 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
415 WINE_FIXME("---error\n");
422 case SymTagPointerType:
423 print_basic(lvalue, 1, format);
426 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
428 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
429 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
433 struct dbg_type type;
439 fcp->Count = min(count, 256);
440 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
442 for (i = 0; i < min(fcp->Count, count); i++)
445 type.module = lvalue->type.module;
446 type.id = fcp->ChildId[i];
447 types_get_info(&type, TI_GET_SYMNAME, &ptr);
449 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
450 dbg_printf("%s=", tmp);
451 HeapFree(GetProcessHeap(), 0, ptr);
452 lvalue_field = *lvalue;
453 if (types_get_udt_element_lvalue(&lvalue_field, &type, &tmpbuf))
455 print_value(&lvalue_field, format, level + 1);
457 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
460 count -= min(count, 256);
466 case SymTagArrayType:
468 * Loop over all of the entries, printing stuff as we go.
471 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
472 types_get_info(&lvalue->type, TI_GET_LENGTH, &size);
479 * Special handling for character arrays.
481 /* FIXME should check basic type here (should be a char!!!!)... */
482 len = min(count, sizeof(buffer));
483 memory_get_string(dbg_curr_process,
484 memory_to_linear_addr(&lvalue->addr),
485 lvalue->cookie == DLV_TARGET, TRUE, buffer, len);
486 dbg_printf("\"%s%s\"", buffer, (len < count) ? "..." : "");
489 lvalue_field = *lvalue;
490 types_get_info(&lvalue->type, TI_GET_TYPE, &lvalue_field.type.id);
492 for (i = 0; i < count; i++)
494 print_value(&lvalue_field, format, level + 1);
495 lvalue_field.addr.Offset += size / count;
496 dbg_printf((i == count - 1) ? "}" : ", ");
499 case SymTagFunctionType:
500 dbg_printf("Function ");
501 print_bare_address(&lvalue->addr);
503 types_print_type(&lvalue->type, FALSE);
506 WINE_FIXME("Unknown tag (%lu)\n", tag);
507 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
513 if (level == 0) dbg_printf("\n");
516 static BOOL CALLBACK print_types_cb(PSYMBOL_INFO sym, ULONG size, void* ctx)
518 struct dbg_type type;
519 type.module = sym->ModBase;
520 type.id = sym->TypeIndex;
521 dbg_printf("Mod: %08lx ID: %08lx \n", type.module, type.id);
522 types_print_type(&type, TRUE);
527 static BOOL CALLBACK print_types_mod_cb(PSTR mod_name, DWORD base, void* ctx)
529 return SymEnumTypes(dbg_curr_process->handle, base, print_types_cb, ctx);
532 int print_types(void)
534 SymEnumerateModules(dbg_curr_process->handle, print_types_mod_cb, NULL);
538 int types_print_type(const struct dbg_type* type, BOOL details)
543 DWORD tag, udt, count;
544 struct dbg_type subtype;
546 if (type->id == dbg_itype_none || !types_get_info(type, TI_GET_SYMTAG, &tag))
548 dbg_printf("--invalid--<%lxh>--", type->id);
552 if (types_get_info(type, TI_GET_SYMNAME, &ptr) && ptr)
554 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
556 HeapFree(GetProcessHeap(), 0, ptr);
558 else name = "--none--";
563 if (details) dbg_printf("Basic<%s>", name); else dbg_printf("%s", name);
565 case SymTagPointerType:
566 types_get_info(type, TI_GET_TYPE, &subtype.id);
567 subtype.module = type->module;
568 types_print_type(&subtype, FALSE);
572 types_get_info(type, TI_GET_UDTKIND, &udt);
575 case UdtStruct: dbg_printf("struct %s", name); break;
576 case UdtUnion: dbg_printf("union %s", name); break;
577 case UdtClass: dbg_printf("class %s", name); break;
578 default: WINE_ERR("Unsupported UDT type (%ld) for %s", udt, name); break;
581 types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
583 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
584 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
588 struct dbg_type type_elt;
594 fcp->Count = min(count, 256);
595 if (types_get_info(type, TI_FINDCHILDREN, fcp))
597 for (i = 0; i < min(fcp->Count, count); i++)
600 type_elt.module = type->module;
601 type_elt.id = fcp->ChildId[i];
602 types_get_info(&type_elt, TI_GET_SYMNAME, &ptr);
604 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
605 HeapFree(GetProcessHeap(), 0, ptr);
606 dbg_printf("%s", tmp);
607 if (types_get_info(&type_elt, TI_GET_TYPE, &type_elt.id))
610 types_print_type(&type_elt, details);
612 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
615 count -= min(count, 256);
621 case SymTagArrayType:
622 types_get_info(type, TI_GET_TYPE, &subtype.id);
623 subtype.module = type->module;
624 types_print_type(&subtype, details);
625 dbg_printf(" %s[]", name);
628 dbg_printf("enum %s", name);
630 case SymTagFunctionType:
631 types_get_info(type, TI_GET_TYPE, &subtype.id);
632 subtype.module = type->module;
633 types_print_type(&subtype, FALSE);
634 dbg_printf(" (*%s)(", name);
635 if (types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
637 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
638 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
644 fcp->Count = min(count, 256);
645 if (types_get_info(type, TI_FINDCHILDREN, fcp))
647 for (i = 0; i < min(fcp->Count, count); i++)
649 subtype.id = fcp->ChildId[i];
650 types_get_info(&subtype, TI_GET_TYPE, &subtype.id);
651 types_print_type(&subtype, FALSE);
652 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
655 count -= min(count, 256);
662 WINE_ERR("Unknown type %lu for %s\n", tag, name);
669 /* helper to typecast pInfo to its expected type (_t) */
670 #define X(_t) (*((_t*)pInfo))
672 BOOL types_get_info(const struct dbg_type* type, IMAGEHLP_SYMBOL_TYPE_INFO ti, void* pInfo)
674 if (type->id == dbg_itype_none) return FALSE;
675 if (type->module != 0)
678 ret = SymGetTypeInfo(dbg_curr_process->handle, type->module, type->id, ti, pInfo);
680 SymGetTypeInfo(dbg_curr_process->handle, type->module, type->id, TI_GET_SYMTAG, &tag) &&
681 tag == SymTagBaseType &&
682 SymGetTypeInfo(dbg_curr_process->handle, type->module, type->id, TI_GET_BASETYPE, &bt))
684 static const WCHAR voidW[] = {'v','o','i','d','\0'};
685 static const WCHAR charW[] = {'c','h','a','r','\0'};
686 static const WCHAR wcharW[] = {'W','C','H','A','R','\0'};
687 static const WCHAR intW[] = {'i','n','t','\0'};
688 static const WCHAR uintW[] = {'u','n','s','i','g','n','e','d',' ','i','n','t','\0'};
689 static const WCHAR floatW[] = {'f','l','o','a','t','\0'};
690 static const WCHAR boolW[] = {'b','o','o','l','\0'};
691 static const WCHAR longW[] = {'l','o','n','g',' ','i','n','t','\0'};
692 static const WCHAR ulongW[] = {'u','n','s','i','g','n','e','d',' ','l','o','n','g',' ','i','n','t','\0'};
693 static const WCHAR complexW[] = {'c','o','m','p','l','e','x','\0'};
694 const WCHAR* name = NULL;
698 case btVoid: name = voidW; break;
699 case btChar: name = charW; break;
700 case btWChar: name = wcharW; break;
701 case btInt: name = intW; break;
702 case btUInt: name = uintW; break;
703 case btFloat: name = floatW; break;
704 case btBool: name = boolW; break;
705 case btLong: name = longW; break;
706 case btULong: name = ulongW; break;
707 case btComplex: name = complexW; break;
708 default: WINE_FIXME("Unsupported basic type %ld\n", bt); return FALSE;
710 X(WCHAR*) = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name) + 1) * sizeof(WCHAR));
713 lstrcpyW(X(WCHAR*), name);
720 assert(type->id >= dbg_itype_first);
724 case dbg_itype_unsigned_int:
727 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
728 case TI_GET_LENGTH: X(DWORD64) = 4; break;
729 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
730 default: WINE_FIXME("unsupported %u for u-int\n", ti); return FALSE;
733 case dbg_itype_signed_int:
736 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
737 case TI_GET_LENGTH: X(DWORD64) = 4; break;
738 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
739 default: WINE_FIXME("unsupported %u for s-int\n", ti); return FALSE;
742 case dbg_itype_unsigned_short_int:
745 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
746 case TI_GET_LENGTH: X(DWORD64) = 2; break;
747 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
748 default: WINE_FIXME("unsupported %u for u-short int\n", ti); return FALSE;
751 case dbg_itype_signed_short_int:
754 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
755 case TI_GET_LENGTH: X(DWORD64) = 2; break;
756 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
757 default: WINE_FIXME("unsupported %u for s-short int\n", ti); return FALSE;
760 case dbg_itype_unsigned_char_int:
763 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
764 case TI_GET_LENGTH: X(DWORD64) = 1; break;
765 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
766 default: WINE_FIXME("unsupported %u for u-char int\n", ti); return FALSE;
769 case dbg_itype_signed_char_int:
772 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
773 case TI_GET_LENGTH: X(DWORD64) = 1; break;
774 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
775 default: WINE_FIXME("unsupported %u for s-char int\n", ti); return FALSE;
781 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
782 case TI_GET_LENGTH: X(DWORD64) = 1; break;
783 case TI_GET_BASETYPE: X(DWORD) = btChar; break;
784 default: WINE_FIXME("unsupported %u for char int\n", ti); return FALSE;
787 case dbg_itype_astring:
790 case TI_GET_SYMTAG: X(DWORD) = SymTagPointerType; break;
791 case TI_GET_LENGTH: X(DWORD64) = 4; break;
792 case TI_GET_TYPE: X(DWORD) = dbg_itype_char; break;
793 default: WINE_FIXME("unsupported %u for a string\n", ti); return FALSE;
796 case dbg_itype_segptr:
799 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
800 case TI_GET_LENGTH: X(DWORD64) = 4; break;
801 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
802 default: WINE_FIXME("unsupported %u for seg-ptr\n", ti); return FALSE;
805 default: WINE_FIXME("unsupported type id 0x%lx\n", type->id);