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 if (types_get_info(&type, TI_GET_TYPE, &type_id) && type_id == user->u.typeid)
310 user->result = sym->TypeIndex;
320 /******************************************************************
323 * Should look up in module based at linear whether (typeid*) exists
324 * Otherwise, we could create it locally
326 struct dbg_type types_find_pointer(const struct dbg_type* type)
328 struct type_find_t f;
331 f.result = dbg_itype_none;
332 f.tag = SymTagPointerType;
333 f.u.typeid = type->id;
334 SymEnumTypes(dbg_curr_process->handle, type->module, types_cb, &f);
335 ret.module = type->module;
340 /******************************************************************
343 * Should look up in the module based at linear address whether a type
344 * named 'name' and with the correct tag exists
346 struct dbg_type types_find_type(unsigned long linear, const char* name, enum SymTagEnum tag)
349 struct type_find_t f;
352 f.result = dbg_itype_none;
355 SymEnumTypes(dbg_curr_process->handle, linear, types_cb, &f);
361 /***********************************************************************
364 * Implementation of the 'print' command.
366 void print_value(const struct dbg_lvalue* lvalue, char format, int level)
368 struct dbg_lvalue lvalue_field;
374 if (lvalue->type.id == dbg_itype_none)
376 /* No type, just print the addr value */
377 print_bare_address(&lvalue->addr);
381 if (format == 'i' || format == 's' || format == 'w' || format == 'b' || format == 'g')
383 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
387 if (!types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
389 WINE_FIXME("---error\n");
396 case SymTagPointerType:
397 print_basic(lvalue, 1, format);
400 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
402 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
403 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
407 struct dbg_type type;
413 fcp->Count = min(count, 256);
414 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
416 for (i = 0; i < min(fcp->Count, count); i++)
419 type.module = lvalue->type.module;
420 type.id = fcp->ChildId[i];
421 types_get_info(&type, TI_GET_SYMNAME, &ptr);
423 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
424 dbg_printf("%s=", tmp);
425 HeapFree(GetProcessHeap(), 0, ptr);
426 lvalue_field = *lvalue;
427 if (types_get_udt_element_lvalue(&lvalue_field, &type, &tmpbuf))
429 print_value(&lvalue_field, format, level + 1);
431 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
434 count -= min(count, 256);
440 case SymTagArrayType:
442 * Loop over all of the entries, printing stuff as we go.
445 types_get_info(&lvalue->type, TI_GET_COUNT, &count);
446 types_get_info(&lvalue->type, TI_GET_LENGTH, &size);
453 * Special handling for character arrays.
455 /* FIXME should check basic type here (should be a char!!!!)... */
456 len = min(count, sizeof(buffer));
457 memory_get_string(dbg_curr_process,
458 memory_to_linear_addr(&lvalue->addr),
459 lvalue->cookie == DLV_TARGET, TRUE, buffer, len);
460 dbg_printf("\"%s%s\"", buffer, (len < count) ? "..." : "");
463 lvalue_field = *lvalue;
464 types_get_info(&lvalue->type, TI_GET_TYPE, &lvalue_field.type.id);
466 for (i = 0; i < count; i++)
468 print_value(&lvalue_field, format, level + 1);
469 lvalue_field.addr.Offset += size / count;
470 dbg_printf((i == count - 1) ? "}" : ", ");
473 case SymTagFunctionType:
474 dbg_printf("Function ");
475 print_bare_address(&lvalue->addr);
477 types_print_type(&lvalue->type, FALSE);
480 WINE_FIXME("Unknown tag (%lu)\n", tag);
481 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
487 if (level == 0) dbg_printf("\n");
490 static BOOL CALLBACK print_types_cb(PSYMBOL_INFO sym, ULONG size, void* ctx)
492 struct dbg_type type;
493 type.module = sym->ModBase;
494 type.id = sym->TypeIndex;
495 dbg_printf("Mod: %08lx ID: %08lx \n", type.module, type.id);
496 types_print_type(&type, TRUE);
501 static BOOL CALLBACK print_types_mod_cb(PSTR mod_name, DWORD base, void* ctx)
503 return SymEnumTypes(dbg_curr_process->handle, base, print_types_cb, ctx);
506 int print_types(void)
508 SymEnumerateModules(dbg_curr_process->handle, print_types_mod_cb, NULL);
512 int types_print_type(const struct dbg_type* type, BOOL details)
517 DWORD tag, udt, count;
518 struct dbg_type subtype;
520 if (type->id == dbg_itype_none || !types_get_info(type, TI_GET_SYMTAG, &tag))
522 dbg_printf("--invalid--<%lxh>--", type->id);
526 if (types_get_info(type, TI_GET_SYMNAME, &ptr) && ptr)
528 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
530 HeapFree(GetProcessHeap(), 0, ptr);
532 else name = "--none--";
537 if (details) dbg_printf("Basic<%s>", name); else dbg_printf("%s", name);
539 case SymTagPointerType:
540 types_get_info(type, TI_GET_TYPE, &subtype.id);
541 subtype.module = type->module;
542 types_print_type(&subtype, FALSE);
546 types_get_info(type, TI_GET_UDTKIND, &udt);
549 case UdtStruct: dbg_printf("struct %s", name); break;
550 case UdtUnion: dbg_printf("union %s", name); break;
551 case UdtClass: dbg_printf("class %s", name); break;
552 default: WINE_ERR("Unsupported UDT type (%ld) for %s", udt, name); break;
555 types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
557 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
558 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
562 struct dbg_type type_elt;
568 fcp->Count = min(count, 256);
569 if (types_get_info(type, TI_FINDCHILDREN, fcp))
571 for (i = 0; i < min(fcp->Count, count); i++)
574 type_elt.module = type->module;
575 type_elt.id = fcp->ChildId[i];
576 types_get_info(&type_elt, TI_GET_SYMNAME, &ptr);
578 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
579 HeapFree(GetProcessHeap(), 0, ptr);
580 dbg_printf("%s", tmp);
581 if (types_get_info(&type_elt, TI_GET_TYPE, &type_elt.id))
584 types_print_type(&type_elt, details);
586 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
589 count -= min(count, 256);
595 case SymTagArrayType:
596 types_get_info(type, TI_GET_TYPE, &subtype.id);
597 subtype.module = type->module;
598 types_print_type(&subtype, details);
599 dbg_printf(" %s[]", name);
602 dbg_printf("enum %s", name);
604 case SymTagFunctionType:
605 types_get_info(type, TI_GET_TYPE, &subtype.id);
606 subtype.module = type->module;
607 types_print_type(&subtype, FALSE);
608 dbg_printf(" (*%s)(", name);
609 if (types_get_info(type, TI_GET_CHILDRENCOUNT, &count))
611 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
612 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
618 fcp->Count = min(count, 256);
619 if (types_get_info(type, TI_FINDCHILDREN, fcp))
621 for (i = 0; i < min(fcp->Count, count); i++)
623 subtype.id = fcp->ChildId[i];
624 types_print_type(&subtype, FALSE);
625 if (i < min(fcp->Count, count) - 1 || count > 256) dbg_printf(", ");
628 count -= min(count, 256);
635 WINE_ERR("Unknown type %lu for %s\n", tag, name);
642 BOOL types_get_info(const struct dbg_type* type, IMAGEHLP_SYMBOL_TYPE_INFO ti, void* pInfo)
644 if (type->id == dbg_itype_none) return FALSE;
645 if (type->module != 0)
646 return SymGetTypeInfo(dbg_curr_process->handle, type->module, type->id, ti, pInfo);
648 assert(type->id >= dbg_itype_first);
649 /* helper to typecast pInfo to its expected type (_t) */
650 #define X(_t) (*((_t*)pInfo))
654 case dbg_itype_unsigned_int:
657 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
658 case TI_GET_LENGTH: X(DWORD) = 4; break;
659 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
660 default: WINE_FIXME("unsupported %u for u-int\n", ti); return FALSE;
663 case dbg_itype_signed_int:
666 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
667 case TI_GET_LENGTH: X(DWORD) = 4; break;
668 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
669 default: WINE_FIXME("unsupported %u for s-int\n", ti); return FALSE;
672 case dbg_itype_unsigned_short_int:
675 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
676 case TI_GET_LENGTH: X(DWORD) = 2; break;
677 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
678 default: WINE_FIXME("unsupported %u for u-short int\n", ti); return FALSE;
681 case dbg_itype_signed_short_int:
684 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
685 case TI_GET_LENGTH: X(DWORD) = 2; break;
686 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
687 default: WINE_FIXME("unsupported %u for s-short int\n", ti); return FALSE;
690 case dbg_itype_unsigned_char_int:
693 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
694 case TI_GET_LENGTH: X(DWORD) = 1; break;
695 case TI_GET_BASETYPE: X(DWORD) = btUInt; break;
696 default: WINE_FIXME("unsupported %u for u-char int\n", ti); return FALSE;
699 case dbg_itype_signed_char_int:
702 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
703 case TI_GET_LENGTH: X(DWORD) = 1; break;
704 case TI_GET_BASETYPE: X(DWORD) = btInt; break;
705 default: WINE_FIXME("unsupported %u for s-char int\n", ti); return FALSE;
711 case TI_GET_SYMTAG: X(DWORD) = SymTagBaseType; break;
712 case TI_GET_LENGTH: X(DWORD) = 1; break;
713 case TI_GET_BASETYPE: X(DWORD) = btChar; break;
714 default: WINE_FIXME("unsupported %u for char int\n", ti); return FALSE;
717 case dbg_itype_astring:
720 case TI_GET_SYMTAG: X(DWORD) = SymTagPointerType; break;
721 case TI_GET_LENGTH: X(DWORD) = 4; break;
722 case TI_GET_TYPE: X(DWORD) = dbg_itype_char; break;
723 default: WINE_FIXME("unsupported %u for a string\n", ti); return FALSE;
726 default: WINE_FIXME("unsupported type id 0x%lx\n", type->id);