2 * File types.c - management of types (hierarchical tree)
4 * Copyright (C) 1997, Eric Youngdale.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Note: This really doesn't do much at the moment, but it forms the framework
22 * upon which full support for datatype handling will eventually be built.
33 #include "wine/debug.h"
34 #include "dbghelp_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
39 static const char* symt_get_tag_str(DWORD tag)
43 case SymTagNull: return "SymTagNull";
44 case SymTagExe: return "SymTagExe";
45 case SymTagCompiland: return "SymTagCompiland";
46 case SymTagCompilandDetails: return "SymTagCompilandDetails";
47 case SymTagCompilandEnv: return "SymTagCompilandEnv";
48 case SymTagFunction: return "SymTagFunction";
49 case SymTagBlock: return "SymTagBlock";
50 case SymTagData: return "SymTagData";
51 case SymTagAnnotation: return "SymTagAnnotation";
52 case SymTagLabel: return "SymTagLabel";
53 case SymTagPublicSymbol: return "SymTagPublicSymbol";
54 case SymTagUDT: return "SymTagUDT";
55 case SymTagEnum: return "SymTagEnum";
56 case SymTagFunctionType: return "SymTagFunctionType";
57 case SymTagPointerType: return "SymTagPointerType";
58 case SymTagArrayType: return "SymTagArrayType";
59 case SymTagBaseType: return "SymTagBaseType";
60 case SymTagTypedef: return "SymTagTypedef,";
61 case SymTagBaseClass: return "SymTagBaseClass";
62 case SymTagFriend: return "SymTagFriend";
63 case SymTagFunctionArgType: return "SymTagFunctionArgType,";
64 case SymTagFuncDebugStart: return "SymTagFuncDebugStart,";
65 case SymTagFuncDebugEnd: return "SymTagFuncDebugEnd";
66 case SymTagUsingNamespace: return "SymTagUsingNamespace,";
67 case SymTagVTableShape: return "SymTagVTableShape";
68 case SymTagVTable: return "SymTagVTable";
69 case SymTagCustom: return "SymTagCustom";
70 case SymTagThunk: return "SymTagThunk";
71 case SymTagCustomType: return "SymTagCustomType";
72 case SymTagManagedType: return "SymTagManagedType";
73 case SymTagDimension: return "SymTagDimension";
74 default: return "---";
78 const char* symt_get_name(const struct symt* sym)
83 case SymTagData: return ((const struct symt_data*)sym)->hash_elt.name;
84 case SymTagFunction: return ((const struct symt_function*)sym)->hash_elt.name;
85 case SymTagPublicSymbol: return ((const struct symt_public*)sym)->hash_elt.name;
86 case SymTagBaseType: return ((const struct symt_basic*)sym)->hash_elt.name;
87 case SymTagLabel: return ((const struct symt_function_point*)sym)->name;
88 case SymTagThunk: return ((const struct symt_thunk*)sym)->hash_elt.name;
90 case SymTagEnum: return ((const struct symt_enum*)sym)->name;
91 case SymTagTypedef: return ((const struct symt_typedef*)sym)->hash_elt.name;
92 case SymTagUDT: return ((const struct symt_udt*)sym)->hash_elt.name;
94 FIXME("Unsupported sym-tag %s\n", symt_get_tag_str(sym->tag));
97 case SymTagPointerType:
98 case SymTagFunctionType:
103 static struct symt* symt_find_type_by_name(struct module* module,
104 enum SymTagEnum sym_tag,
105 const char* typename)
108 struct symt_ht* type;
109 struct hash_table_iter hti;
114 hash_table_iter_init(&module->ht_types, &hti, typename);
115 while ((ptr = hash_table_iter_up(&hti)))
117 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
119 if ((sym_tag == SymTagNull || type->symt.tag == sym_tag) &&
120 type->hash_elt.name && !strcmp(type->hash_elt.name, typename))
123 SetLastError(ERROR_INVALID_NAME); /* FIXME ?? */
127 static void symt_add_type(struct module* module, struct symt* symt)
130 p = vector_add(&module->vtypes, &module->pool);
135 struct symt_basic* symt_new_basic(struct module* module, enum BasicType bt,
136 const char* typename, unsigned size)
138 struct symt_basic* sym;
142 sym = (struct symt_basic*)symt_find_type_by_name(module, SymTagBaseType,
144 if (sym && sym->bt == bt && sym->size == size)
147 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
149 sym->symt.tag = SymTagBaseType;
152 sym->hash_elt.name = pool_strdup(&module->pool, typename);
153 hash_table_add(&module->ht_types, &sym->hash_elt);
154 } else sym->hash_elt.name = NULL;
157 symt_add_type(module, &sym->symt);
162 struct symt_udt* symt_new_udt(struct module* module, const char* typename,
163 unsigned size, enum UdtKind kind)
165 struct symt_udt* sym;
167 TRACE_(dbghelp_symt)("Adding udt %s:%s\n", module->module.ModuleName, typename);
168 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
170 sym->symt.tag = SymTagUDT;
175 sym->hash_elt.name = pool_strdup(&module->pool, typename);
176 hash_table_add(&module->ht_types, &sym->hash_elt);
177 } else sym->hash_elt.name = NULL;
178 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
179 symt_add_type(module, &sym->symt);
184 BOOL symt_set_udt_size(struct module* module, struct symt_udt* udt, unsigned size)
186 assert(udt->symt.tag == SymTagUDT);
187 if (vector_length(&udt->vchildren) != 0)
189 if (udt->size != size)
190 FIXME_(dbghelp_symt)("Changing size for %s from %u to %u\n",
191 udt->hash_elt.name, udt->size, size);
198 /******************************************************************
199 * symt_add_udt_element
201 * add an element to a udt (struct, class, union)
202 * the size & offset parameters are expressed in bits (not bytes) so that
203 * we can mix in the single call bytes aligned elements (regular fields) and
204 * the others (bit fields)
206 BOOL symt_add_udt_element(struct module* module, struct symt_udt* udt_type,
207 const char* name, struct symt* elt_type,
208 unsigned offset, unsigned size)
213 assert(udt_type->symt.tag == SymTagUDT);
215 TRACE_(dbghelp_symt)("Adding %s to UDT %s\n", name, udt_type->hash_elt.name);
217 while ((p = vector_iter_up(&udt_type->vchildren, p)))
219 m = (struct symt_data*)*p;
221 assert(m->symt.tag == SymTagData);
222 if (m->hash_elt.name[0] == name[0] && strcmp(m->hash_elt.name, name) == 0)
226 if ((m = pool_alloc(&module->pool, sizeof(*m))) == NULL) return FALSE;
227 memset(m, 0, sizeof(*m));
228 m->symt.tag = SymTagData;
229 m->hash_elt.name = pool_strdup(&module->pool, name);
230 m->hash_elt.next = NULL;
232 m->kind = DataIsMember;
233 m->container = &udt_type->symt;
235 m->u.s.offset = offset;
236 m->u.s.length = ((offset & 7) || (size & 7)) ? size : 0;
238 p = vector_add(&udt_type->vchildren, &module->pool);
244 struct symt_enum* symt_new_enum(struct module* module, const char* typename)
246 struct symt_enum* sym;
248 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
250 sym->symt.tag = SymTagEnum;
251 sym->name = (typename) ? pool_strdup(&module->pool, typename) : NULL;
252 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
257 BOOL symt_add_enum_element(struct module* module, struct symt_enum* enum_type,
258 const char* name, int value)
263 assert(enum_type->symt.tag == SymTagEnum);
264 e = pool_alloc(&module->pool, sizeof(*e));
265 if (e == NULL) return FALSE;
267 e->symt.tag = SymTagData;
268 e->hash_elt.name = pool_strdup(&module->pool, name);
269 e->hash_elt.next = NULL;
270 e->kind = DataIsConstant;
271 e->container = &enum_type->symt;
272 /* CV defines the underlying type for the enumeration */
273 e->type = &symt_new_basic(module, btInt, "int", 4)->symt;
274 e->u.value.n1.n2.vt = VT_I4;
275 e->u.value.n1.n2.n3.lVal = value;
277 p = vector_add(&enum_type->vchildren, &module->pool);
278 if (!p) return FALSE; /* FIXME we leak e */
284 struct symt_array* symt_new_array(struct module* module, int min, int max,
285 struct symt* base, struct symt* index)
287 struct symt_array* sym;
289 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
291 sym->symt.tag = SymTagArrayType;
294 sym->base_type = base;
295 sym->index_type = index;
296 symt_add_type(module, &sym->symt);
301 struct symt_function_signature* symt_new_function_signature(struct module* module,
302 struct symt* ret_type,
303 enum CV_call_e call_conv)
305 struct symt_function_signature* sym;
307 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
309 sym->symt.tag = SymTagFunctionType;
310 sym->rettype = ret_type;
311 vector_init(&sym->vchildren, sizeof(struct symt*), 4);
312 sym->call_conv = call_conv;
313 symt_add_type(module, &sym->symt);
318 BOOL symt_add_function_signature_parameter(struct module* module,
319 struct symt_function_signature* sig_type,
323 struct symt_function_arg_type* arg;
325 assert(sig_type->symt.tag == SymTagFunctionType);
326 arg = pool_alloc(&module->pool, sizeof(*arg));
327 if (!arg) return FALSE;
328 arg->symt.tag = SymTagFunctionArgType;
329 arg->arg_type = param;
330 arg->container = &sig_type->symt;
331 p = vector_add(&sig_type->vchildren, &module->pool);
332 if (!p) return FALSE; /* FIXME we leak arg */
338 struct symt_pointer* symt_new_pointer(struct module* module, struct symt* ref_type)
340 struct symt_pointer* sym;
342 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
344 sym->symt.tag = SymTagPointerType;
345 sym->pointsto = ref_type;
346 symt_add_type(module, &sym->symt);
351 struct symt_typedef* symt_new_typedef(struct module* module, struct symt* ref,
354 struct symt_typedef* sym;
356 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
358 sym->symt.tag = SymTagTypedef;
360 sym->hash_elt.name = pool_strdup(&module->pool, name);
361 hash_table_add(&module->ht_types, &sym->hash_elt);
362 symt_add_type(module, &sym->symt);
367 /******************************************************************
368 * SymEnumTypes (DBGHELP.@)
371 BOOL WINAPI SymEnumTypes(HANDLE hProcess, ULONG64 BaseOfDll,
372 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
376 struct module_pair pair;
377 char buffer[sizeof(SYMBOL_INFO) + 256];
378 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
384 TRACE("(%p %s %p %p)\n",
385 hProcess, wine_dbgstr_longlong(BaseOfDll), EnumSymbolsCallback,
388 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
389 pair.requested = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
390 if (!module_get_debug(pcs, &pair)) return FALSE;
392 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
393 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
395 while ((pos = vector_iter_up(&pair.effective->vtypes, pos)))
397 type = *(struct symt**)pos;
398 sym_info->TypeIndex = (DWORD)type;
399 sym_info->info = 0; /* FIXME */
400 symt_get_info(type, TI_GET_LENGTH, &size);
401 sym_info->Size = size;
402 sym_info->ModBase = pair.requested->module.BaseOfImage;
403 sym_info->Flags = 0; /* FIXME */
404 sym_info->Value = 0; /* FIXME */
405 sym_info->Address = 0; /* FIXME */
406 sym_info->Register = 0; /* FIXME */
407 sym_info->Scope = 0; /* FIXME */
408 sym_info->Tag = type->tag;
409 tmp = symt_get_name(type);
412 sym_info->NameLen = min(strlen(tmp),sym_info->MaxNameLen-1);
413 memcpy(sym_info->Name, tmp, sym_info->NameLen);
414 sym_info->Name[sym_info->NameLen] = '\0';
417 sym_info->Name[sym_info->NameLen = 0] = '\0';
418 if (!EnumSymbolsCallback(sym_info, sym_info->Size, UserContext)) break;
423 /******************************************************************
426 * Retrieves inforamtion about a symt (either symbol or type)
428 BOOL symt_get_info(const struct symt* type, IMAGEHLP_SYMBOL_TYPE_INFO req,
433 if (!type) return FALSE;
435 /* helper to typecast pInfo to its expected type (_t) */
436 #define X(_t) (*((_t*)pInfo))
440 case TI_FINDCHILDREN:
442 const struct vector* v;
445 TI_FINDCHILDREN_PARAMS* tifp = pInfo;
449 case SymTagUDT: v = &((const struct symt_udt*)type)->vchildren; break;
450 case SymTagEnum: v = &((const struct symt_enum*)type)->vchildren; break;
451 case SymTagFunctionType: v = &((const struct symt_function_signature*)type)->vchildren; break;
452 case SymTagFunction: v = &((const struct symt_function*)type)->vchildren; break;
454 FIXME("Unsupported sym-tag %s for find-children\n",
455 symt_get_tag_str(type->tag));
458 for (i = 0; i < tifp->Count; i++)
460 if (!(pt = vector_at(v, tifp->Start + i))) return FALSE;
461 tifp->ChildId[i] = (DWORD)*pt;
470 switch (((const struct symt_data*)type)->kind)
473 case DataIsFileStatic:
474 X(ULONG64) = ((const struct symt_data*)type)->u.address;
476 default: return FALSE;
480 X(ULONG64) = ((const struct symt_function*)type)->address;
482 case SymTagPublicSymbol:
483 X(ULONG64) = ((const struct symt_public*)type)->address;
485 case SymTagFuncDebugStart:
486 case SymTagFuncDebugEnd:
488 X(ULONG64) = ((const struct symt_function_point*)type)->parent->address +
489 ((const struct symt_function_point*)type)->offset;
492 X(ULONG64) = ((const struct symt_thunk*)type)->address;
495 FIXME("Unsupported sym-tag %s for get-address\n",
496 symt_get_tag_str(type->tag));
501 case TI_GET_BASETYPE:
505 X(DWORD) = ((const struct symt_basic*)type)->bt;
515 case TI_GET_BITPOSITION:
516 if (type->tag != SymTagData ||
517 ((const struct symt_data*)type)->kind != DataIsMember ||
518 ((const struct symt_data*)type)->u.s.length == 0)
520 X(DWORD) = ((const struct symt_data*)type)->u.s.offset & 7;
523 case TI_GET_CHILDRENCOUNT:
527 X(DWORD) = vector_length(&((const struct symt_udt*)type)->vchildren);
530 X(DWORD) = vector_length(&((const struct symt_enum*)type)->vchildren);
532 case SymTagFunctionType:
533 X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
536 X(DWORD) = vector_length(&((const struct symt_function*)type)->vchildren);
538 case SymTagPointerType: /* MS does it that way */
539 case SymTagArrayType: /* MS does it that way */
540 case SymTagThunk: /* MS does it that way */
544 FIXME("Unsupported sym-tag %s for get-children-count\n",
545 symt_get_tag_str(type->tag));
548 case SymTagPublicSymbol:
557 case SymTagArrayType:
558 X(DWORD) = ((const struct symt_array*)type)->end -
559 ((const struct symt_array*)type)->start + 1;
561 case SymTagFunctionType:
562 /* this seems to be wrong for (future) C++ methods, where 'this' parameter
563 * should be included in this value (and not in GET_CHILDREN_COUNT)
565 X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
567 default: return FALSE;
571 case TI_GET_DATAKIND:
572 if (type->tag != SymTagData) return FALSE;
573 X(DWORD) = ((const struct symt_data*)type)->kind;
580 X(DWORD64) = ((const struct symt_basic*)type)->size;
583 X(DWORD64) = ((const struct symt_function*)type)->size;
585 case SymTagPointerType:
586 X(DWORD64) = sizeof(void*);
589 X(DWORD64) = ((const struct symt_udt*)type)->size;
592 X(DWORD64) = sizeof(int); /* FIXME: should be size of base-type of enum !!! */
595 if (((const struct symt_data*)type)->kind != DataIsMember ||
596 !((const struct symt_data*)type)->u.s.length)
598 X(DWORD64) = ((const struct symt_data*)type)->u.s.length;
600 case SymTagArrayType:
601 if (!symt_get_info(((const struct symt_array*)type)->base_type,
602 TI_GET_LENGTH, pInfo))
604 X(DWORD64) *= ((const struct symt_array*)type)->end -
605 ((const struct symt_array*)type)->start + 1;
607 case SymTagPublicSymbol:
608 X(DWORD64) = ((const struct symt_public*)type)->size;
611 return symt_get_info(((const struct symt_typedef*)type)->type, TI_GET_LENGTH, pInfo);
613 X(DWORD64) = ((const struct symt_thunk*)type)->size;
616 FIXME("Unsupported sym-tag %s for get-length\n",
617 symt_get_tag_str(type->tag));
619 case SymTagFunctionType:
624 case TI_GET_LEXICALPARENT:
628 X(DWORD) = (DWORD)((const struct symt_block*)type)->container;
631 X(DWORD) = (DWORD)((const struct symt_data*)type)->container;
634 X(DWORD) = (DWORD)((const struct symt_function*)type)->container;
637 X(DWORD) = (DWORD)((const struct symt_thunk*)type)->container;
639 case SymTagFunctionArgType:
640 X(DWORD) = (DWORD)((const struct symt_function_arg_type*)type)->container;
643 FIXME("Unsupported sym-tag %s for get-lexical-parent\n",
644 symt_get_tag_str(type->tag));
665 switch (((const struct symt_data*)type)->kind)
670 X(ULONG) = ((const struct symt_data*)type)->u.s.offset >> 3;
673 FIXME("Unknown kind (%u) for get-offset\n",
674 ((const struct symt_data*)type)->kind);
679 FIXME("Unsupported sym-tag %s for get-offset\n",
680 symt_get_tag_str(type->tag));
687 const char* name = symt_get_name(type);
688 if (!name) return FALSE;
689 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
690 X(WCHAR*) = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
691 if (!X(WCHAR*)) return FALSE;
692 MultiByteToWideChar(CP_ACP, 0, name, -1, X(WCHAR*), len);
697 X(DWORD) = type->tag;
704 /* hierarchical => hierarchical */
705 case SymTagArrayType:
706 X(DWORD) = (DWORD)((const struct symt_array*)type)->base_type;
708 case SymTagPointerType:
709 X(DWORD) = (DWORD)((const struct symt_pointer*)type)->pointsto;
711 case SymTagFunctionType:
712 X(DWORD) = (DWORD)((const struct symt_function_signature*)type)->rettype;
715 X(DWORD) = (DWORD)((const struct symt_typedef*)type)->type;
717 /* lexical => hierarchical */
719 X(DWORD) = (DWORD)((const struct symt_data*)type)->type;
722 X(DWORD) = (DWORD)((const struct symt_function*)type)->type;
724 /* FIXME: should also work for enums */
725 case SymTagFunctionArgType:
726 X(DWORD) = (DWORD)((const struct symt_function_arg_type*)type)->arg_type;
729 FIXME("Unsupported sym-tag %s for get-type\n",
730 symt_get_tag_str(type->tag));
731 case SymTagPublicSymbol:
738 if (type->tag != SymTagUDT) return FALSE;
739 X(DWORD) = ((const struct symt_udt*)type)->kind;
743 if (type->tag != SymTagData || ((const struct symt_data*)type)->kind != DataIsConstant)
745 X(VARIANT) = ((const struct symt_data*)type)->u.value;
748 case TI_GET_CALLING_CONVENTION:
749 if (type->tag != SymTagFunctionType) return FALSE;
750 if (((const struct symt_function_signature*)type)->call_conv == -1)
752 FIXME("No support for calling convention for this signature\n");
753 X(DWORD) = CV_CALL_FAR_C; /* FIXME */
755 else X(DWORD) = ((const struct symt_function_signature*)type)->call_conv;
757 case TI_GET_ARRAYINDEXTYPEID:
758 if (type->tag != SymTagArrayType) return FALSE;
759 X(DWORD) = (DWORD)((const struct symt_array*)type)->index_type;
764 case TI_GET_ADDRESSOFFSET:
765 case TI_GET_CLASSPARENTID:
766 case TI_GET_SYMINDEX:
767 case TI_GET_THISADJUST:
768 case TI_GET_VIRTUALBASECLASS:
769 case TI_GET_VIRTUALBASEPOINTEROFFSET:
770 case TI_GET_VIRTUALTABLESHAPEID:
772 FIXME("Unsupported GetInfo request (%u)\n", req);
775 FIXME("Unknown GetInfo request (%u)\n", req);
782 /******************************************************************
783 * SymGetTypeInfo (DBGHELP.@)
786 BOOL WINAPI SymGetTypeInfo(HANDLE hProcess, DWORD64 ModBase,
787 ULONG TypeId, IMAGEHLP_SYMBOL_TYPE_INFO GetType,
790 struct process* pcs = process_find_by_handle(hProcess);
791 struct module_pair pair;
793 if (!pcs) return FALSE;
795 pair.requested = module_find_by_addr(pcs, ModBase, DMT_UNKNOWN);
796 if (!module_get_debug(pcs, &pair))
798 FIXME("Someone didn't properly set ModBase (%s)\n", wine_dbgstr_longlong(ModBase));
802 return symt_get_info((struct symt*)TypeId, GetType, pInfo);
805 /******************************************************************
806 * SymGetTypeFromName (DBGHELP.@)
809 BOOL WINAPI SymGetTypeFromName(HANDLE hProcess, ULONG64 BaseOfDll,
810 PCSTR Name, PSYMBOL_INFO Symbol)
812 struct process* pcs = process_find_by_handle(hProcess);
813 struct module* module;
816 if (!pcs) return FALSE;
817 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
818 if (!module) return FALSE;
819 type = symt_find_type_by_name(module, SymTagNull, Name);
820 if (!type) return FALSE;
821 Symbol->TypeIndex = (DWORD)type;