msvcrt: Use EnumSystemLocalesEx instead of directly accessing kernel32 resources.
[wine.git] / dlls / dbghelp / type.c
blob67f4265ddc91479644266bf910717697ed41bc3d
1 /*
2 * File types.c - management of types (hierarchical tree)
4 * Copyright (C) 1997, Eric Youngdale.
5 * 2004, Eric Pouech.
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.
25 #define NONAMELESSUNION
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "wine/debug.h"
35 #include "dbghelp_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
38 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
40 static const char* symt_get_tag_str(DWORD tag)
42 switch (tag)
44 case SymTagNull: return "SymTagNull";
45 case SymTagExe: return "SymTagExe";
46 case SymTagCompiland: return "SymTagCompiland";
47 case SymTagCompilandDetails: return "SymTagCompilandDetails";
48 case SymTagCompilandEnv: return "SymTagCompilandEnv";
49 case SymTagFunction: return "SymTagFunction";
50 case SymTagBlock: return "SymTagBlock";
51 case SymTagData: return "SymTagData";
52 case SymTagAnnotation: return "SymTagAnnotation";
53 case SymTagLabel: return "SymTagLabel";
54 case SymTagPublicSymbol: return "SymTagPublicSymbol";
55 case SymTagUDT: return "SymTagUDT";
56 case SymTagEnum: return "SymTagEnum";
57 case SymTagFunctionType: return "SymTagFunctionType";
58 case SymTagPointerType: return "SymTagPointerType";
59 case SymTagArrayType: return "SymTagArrayType";
60 case SymTagBaseType: return "SymTagBaseType";
61 case SymTagTypedef: return "SymTagTypedef,";
62 case SymTagBaseClass: return "SymTagBaseClass";
63 case SymTagFriend: return "SymTagFriend";
64 case SymTagFunctionArgType: return "SymTagFunctionArgType,";
65 case SymTagFuncDebugStart: return "SymTagFuncDebugStart";
66 case SymTagFuncDebugEnd: return "SymTagFuncDebugEnd";
67 case SymTagUsingNamespace: return "SymTagUsingNamespace";
68 case SymTagVTableShape: return "SymTagVTableShape";
69 case SymTagVTable: return "SymTagVTable";
70 case SymTagCustom: return "SymTagCustom";
71 case SymTagThunk: return "SymTagThunk";
72 case SymTagCustomType: return "SymTagCustomType";
73 case SymTagManagedType: return "SymTagManagedType";
74 case SymTagDimension: return "SymTagDimension";
75 case SymTagCallSite: return "SymTagCallSite";
76 case SymTagInlineSite: return "SymTagInlineSite";
77 case SymTagBaseInterface: return "SymTagBaseInterface";
78 case SymTagVectorType: return "SymTagVectorType";
79 case SymTagMatrixType: return "SymTagMatrixType";
80 case SymTagHLSLType: return "SymTagHLSLType";
81 case SymTagCaller: return "SymTagCaller";
82 case SymTagCallee: return "SymTagCallee";
83 case SymTagExport: return "SymTagExport";
84 case SymTagHeapAllocationSite: return "SymTagHeapAllocationSite";
85 case SymTagCoffGroup: return "SymTagCoffGroup";
86 case SymTagInlinee: return "SymTagInlinee";
87 default: return "---";
91 const char* symt_get_name(const struct symt* sym)
93 switch (sym->tag)
95 /* lexical tree */
96 case SymTagData: return ((const struct symt_data*)sym)->hash_elt.name;
97 case SymTagFunction: return ((const struct symt_function*)sym)->hash_elt.name;
98 case SymTagInlineSite: return ((const struct symt_inlinesite*)sym)->func.hash_elt.name;
99 case SymTagPublicSymbol: return ((const struct symt_public*)sym)->hash_elt.name;
100 case SymTagBaseType: return ((const struct symt_basic*)sym)->hash_elt.name;
101 case SymTagLabel: return ((const struct symt_hierarchy_point*)sym)->hash_elt.name;
102 case SymTagThunk: return ((const struct symt_thunk*)sym)->hash_elt.name;
103 case SymTagCustom: return ((const struct symt_custom*)sym)->hash_elt.name;
104 /* hierarchy tree */
105 case SymTagEnum: return ((const struct symt_enum*)sym)->hash_elt.name;
106 case SymTagTypedef: return ((const struct symt_typedef*)sym)->hash_elt.name;
107 case SymTagUDT: return ((const struct symt_udt*)sym)->hash_elt.name;
108 case SymTagCompiland: return source_get(((const struct symt_compiland*)sym)->container->module,
109 ((const struct symt_compiland*)sym)->source);
110 default:
111 FIXME("Unsupported sym-tag %s\n", symt_get_tag_str(sym->tag));
112 /* fall through */
113 case SymTagArrayType:
114 case SymTagPointerType:
115 case SymTagFunctionType:
116 case SymTagFunctionArgType:
117 case SymTagBlock:
118 case SymTagFuncDebugStart:
119 case SymTagFuncDebugEnd:
120 return NULL;
124 WCHAR* symt_get_nameW(const struct symt* sym)
126 const char* name = symt_get_name(sym);
127 WCHAR* nameW;
128 DWORD sz;
130 if (!name) return NULL;
131 sz = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
132 if ((nameW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
133 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sz);
134 return nameW;
137 BOOL symt_get_address(const struct symt* type, ULONG64* addr)
139 switch (type->tag)
141 case SymTagData:
142 switch (((const struct symt_data*)type)->kind)
144 case DataIsGlobal:
145 case DataIsFileStatic:
146 *addr = ((const struct symt_data*)type)->u.var.offset;
147 break;
148 default: return FALSE;
150 break;
151 case SymTagBlock:
152 *addr = ((const struct symt_block*)type)->address;
153 break;
154 case SymTagFunction:
155 *addr = ((const struct symt_function*)type)->address;
156 break;
157 case SymTagInlineSite:
158 *addr = ((const struct symt_inlinesite*)type)->func.address;
159 break;
160 case SymTagPublicSymbol:
161 *addr = ((const struct symt_public*)type)->address;
162 break;
163 case SymTagFuncDebugStart:
164 case SymTagFuncDebugEnd:
165 case SymTagLabel:
166 if (!((const struct symt_hierarchy_point*)type)->parent ||
167 !symt_get_address(((const struct symt_hierarchy_point*)type)->parent, addr))
168 *addr = 0;
169 *addr += ((const struct symt_hierarchy_point*)type)->loc.offset;
170 break;
171 case SymTagThunk:
172 *addr = ((const struct symt_thunk*)type)->address;
173 break;
174 case SymTagCustom:
175 *addr = ((const struct symt_custom*)type)->address;
176 break;
177 default:
178 FIXME("Unsupported sym-tag %s for get-address\n", symt_get_tag_str(type->tag));
179 /* fall through */
180 case SymTagExe:
181 case SymTagCompiland:
182 case SymTagFunctionType:
183 case SymTagFunctionArgType:
184 case SymTagBaseType:
185 case SymTagUDT:
186 case SymTagEnum:
187 case SymTagTypedef:
188 case SymTagPointerType:
189 case SymTagArrayType:
190 return FALSE;
192 return TRUE;
195 static struct symt* symt_find_type_by_name(const struct module* module,
196 enum SymTagEnum sym_tag,
197 const char* typename)
199 void* ptr;
200 struct symt_ht* type;
201 struct hash_table_iter hti;
203 assert(typename);
204 assert(module);
206 hash_table_iter_init(&module->ht_types, &hti, typename);
207 while ((ptr = hash_table_iter_up(&hti)))
209 type = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
211 if ((sym_tag == SymTagNull || type->symt.tag == sym_tag) &&
212 type->hash_elt.name && !strcmp(type->hash_elt.name, typename))
213 return &type->symt;
215 SetLastError(ERROR_INVALID_NAME); /* FIXME ?? */
216 return NULL;
219 static void symt_add_type(struct module* module, struct symt* symt)
221 struct symt** p;
222 p = vector_add(&module->vtypes, &module->pool);
223 assert(p);
224 *p = symt;
227 struct symt_basic* symt_new_basic(struct module* module, enum BasicType bt,
228 const char* typename, unsigned size)
230 struct symt_basic* sym;
232 if (typename)
234 sym = (struct symt_basic*)symt_find_type_by_name(module, SymTagBaseType,
235 typename);
236 if (sym && sym->bt == bt && sym->size == size)
237 return sym;
239 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
241 sym->symt.tag = SymTagBaseType;
242 if (typename)
244 sym->hash_elt.name = pool_strdup(&module->pool, typename);
245 hash_table_add(&module->ht_types, &sym->hash_elt);
246 } else sym->hash_elt.name = NULL;
247 sym->bt = bt;
248 sym->size = size;
249 symt_add_type(module, &sym->symt);
251 return sym;
254 struct symt_udt* symt_new_udt(struct module* module, const char* typename,
255 unsigned size, enum UdtKind kind)
257 struct symt_udt* sym;
259 TRACE_(dbghelp_symt)("Adding udt %s:%s\n",
260 debugstr_w(module->modulename), typename);
261 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
263 sym->symt.tag = SymTagUDT;
264 sym->kind = kind;
265 sym->size = size;
266 if (typename)
268 sym->hash_elt.name = pool_strdup(&module->pool, typename);
269 hash_table_add(&module->ht_types, &sym->hash_elt);
270 } else sym->hash_elt.name = NULL;
271 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
272 symt_add_type(module, &sym->symt);
274 return sym;
277 BOOL symt_set_udt_size(struct module* module, struct symt_udt* udt, unsigned size)
279 assert(udt->symt.tag == SymTagUDT);
280 if (vector_length(&udt->vchildren) != 0)
282 if (udt->size != size)
283 FIXME_(dbghelp_symt)("Changing size for %s from %u to %u\n",
284 udt->hash_elt.name, udt->size, size);
285 return TRUE;
287 udt->size = size;
288 return TRUE;
291 /******************************************************************
292 * symt_add_udt_element
294 * add an element to a udt (struct, class, union)
295 * the size & offset parameters are expressed in bits (not bytes) so that
296 * we can mix in the single call bytes aligned elements (regular fields) and
297 * the others (bit fields)
299 BOOL symt_add_udt_element(struct module* module, struct symt_udt* udt_type,
300 const char* name, struct symt* elt_type,
301 unsigned offset, unsigned bit_offset, unsigned bit_size)
303 struct symt_data* m;
304 struct symt** p;
306 assert(udt_type->symt.tag == SymTagUDT);
308 TRACE_(dbghelp_symt)("Adding %s to UDT %s\n", name, udt_type->hash_elt.name);
309 if (name)
311 unsigned int i;
312 for (i=0; i<vector_length(&udt_type->vchildren); i++)
314 m = *(struct symt_data**)vector_at(&udt_type->vchildren, i);
315 assert(m);
316 assert(m->symt.tag == SymTagData);
317 if (strcmp(m->hash_elt.name, name) == 0)
318 return TRUE;
322 if ((m = pool_alloc(&module->pool, sizeof(*m))) == NULL) return FALSE;
323 memset(m, 0, sizeof(*m));
324 m->symt.tag = SymTagData;
325 m->hash_elt.name = name ? pool_strdup(&module->pool, name) : "";
326 m->hash_elt.next = NULL;
328 m->kind = DataIsMember;
329 m->container = &udt_type->symt;
330 m->type = elt_type;
331 m->u.member.offset = offset;
332 m->u.member.bit_offset = bit_offset;
333 m->u.member.bit_length = bit_size;
334 p = vector_add(&udt_type->vchildren, &module->pool);
335 *p = &m->symt;
337 return TRUE;
340 struct symt_enum* symt_new_enum(struct module* module, const char* typename,
341 struct symt* basetype)
343 struct symt_enum* sym;
345 TRACE_(dbghelp_symt)("Adding enum %s:%s\n",
346 debugstr_w(module->modulename), typename);
347 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
349 sym->symt.tag = SymTagEnum;
350 if (typename)
352 sym->hash_elt.name = pool_strdup(&module->pool, typename);
353 hash_table_add(&module->ht_types, &sym->hash_elt);
354 } else sym->hash_elt.name = NULL;
355 sym->base_type = basetype;
356 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
357 symt_add_type(module, &sym->symt);
359 return sym;
362 BOOL symt_add_enum_element(struct module* module, struct symt_enum* enum_type,
363 const char* name, int value)
365 struct symt_data* e;
366 struct symt** p;
368 assert(enum_type->symt.tag == SymTagEnum);
369 e = pool_alloc(&module->pool, sizeof(*e));
370 if (e == NULL) return FALSE;
372 e->symt.tag = SymTagData;
373 e->hash_elt.name = pool_strdup(&module->pool, name);
374 e->hash_elt.next = NULL;
375 e->kind = DataIsConstant;
376 e->container = &enum_type->symt;
377 e->type = enum_type->base_type;
378 V_VT(&e->u.value) = VT_I4;
379 V_I4(&e->u.value) = value;
381 p = vector_add(&enum_type->vchildren, &module->pool);
382 if (!p) return FALSE; /* FIXME we leak e */
383 *p = &e->symt;
385 return TRUE;
388 struct symt_array* symt_new_array(struct module* module, int min, DWORD cnt,
389 struct symt* base, struct symt* index)
391 struct symt_array* sym;
393 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
395 sym->symt.tag = SymTagArrayType;
396 sym->start = min;
397 sym->count = cnt;
398 sym->base_type = base;
399 sym->index_type = index;
400 symt_add_type(module, &sym->symt);
402 return sym;
405 struct symt_function_signature* symt_new_function_signature(struct module* module,
406 struct symt* ret_type,
407 enum CV_call_e call_conv)
409 struct symt_function_signature* sym;
411 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
413 sym->symt.tag = SymTagFunctionType;
414 sym->rettype = ret_type;
415 vector_init(&sym->vchildren, sizeof(struct symt*), 4);
416 sym->call_conv = call_conv;
417 symt_add_type(module, &sym->symt);
419 return sym;
422 BOOL symt_add_function_signature_parameter(struct module* module,
423 struct symt_function_signature* sig_type,
424 struct symt* param)
426 struct symt** p;
427 struct symt_function_arg_type* arg;
429 assert(sig_type->symt.tag == SymTagFunctionType);
430 arg = pool_alloc(&module->pool, sizeof(*arg));
431 if (!arg) return FALSE;
432 arg->symt.tag = SymTagFunctionArgType;
433 arg->arg_type = param;
434 p = vector_add(&sig_type->vchildren, &module->pool);
435 if (!p) return FALSE; /* FIXME we leak arg */
436 *p = &arg->symt;
438 return TRUE;
441 struct symt_pointer* symt_new_pointer(struct module* module, struct symt* ref_type, ULONG_PTR size)
443 struct symt_pointer* sym;
445 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
447 sym->symt.tag = SymTagPointerType;
448 sym->pointsto = ref_type;
449 sym->size = size;
450 symt_add_type(module, &sym->symt);
452 return sym;
455 struct symt_typedef* symt_new_typedef(struct module* module, struct symt* ref,
456 const char* name)
458 struct symt_typedef* sym;
460 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
462 sym->symt.tag = SymTagTypedef;
463 sym->type = ref;
464 sym->hash_elt.name = pool_strdup(&module->pool, name);
465 hash_table_add(&module->ht_types, &sym->hash_elt);
466 symt_add_type(module, &sym->symt);
468 return sym;
471 /******************************************************************
472 * SymEnumTypes (DBGHELP.@)
475 BOOL WINAPI SymEnumTypes(HANDLE hProcess, ULONG64 BaseOfDll,
476 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
477 PVOID UserContext)
479 struct module_pair pair;
480 char buffer[sizeof(SYMBOL_INFO) + 256];
481 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
482 struct symt* type;
483 DWORD64 size;
484 unsigned int i;
486 TRACE("(%p %s %p %p)\n",
487 hProcess, wine_dbgstr_longlong(BaseOfDll), EnumSymbolsCallback,
488 UserContext);
490 if (!module_init_pair(&pair, hProcess, BaseOfDll)) return FALSE;
492 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
493 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
495 for (i=0; i<vector_length(&pair.effective->vtypes); i++)
497 type = *(struct symt**)vector_at(&pair.effective->vtypes, i);
498 sym_info->TypeIndex = symt_ptr2index(pair.effective, type);
499 sym_info->Index = 0; /* FIXME */
500 symt_get_info(pair.effective, type, TI_GET_LENGTH, &size);
501 sym_info->Size = size;
502 sym_info->ModBase = pair.requested->module.BaseOfImage;
503 sym_info->Flags = 0; /* FIXME */
504 sym_info->Value = 0; /* FIXME */
505 sym_info->Address = 0; /* FIXME */
506 sym_info->Register = 0; /* FIXME */
507 sym_info->Scope = 0; /* FIXME */
508 sym_info->Tag = type->tag;
509 symbol_setname(sym_info, symt_get_name(type));
510 if (!EnumSymbolsCallback(sym_info, sym_info->Size, UserContext)) break;
512 return TRUE;
515 struct enum_types_AtoW
517 char buffer[sizeof(SYMBOL_INFOW) + 256 * sizeof(WCHAR)];
518 void* user;
519 PSYM_ENUMERATESYMBOLS_CALLBACKW callback;
522 static BOOL CALLBACK enum_types_AtoW(PSYMBOL_INFO si, ULONG size, PVOID _et)
524 struct enum_types_AtoW* et = _et;
525 SYMBOL_INFOW* siW = (SYMBOL_INFOW*)et->buffer;
527 copy_symbolW(siW, si);
528 return et->callback(siW, size, et->user);
531 /******************************************************************
532 * SymEnumTypesW (DBGHELP.@)
535 BOOL WINAPI SymEnumTypesW(HANDLE hProcess, ULONG64 BaseOfDll,
536 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
537 PVOID UserContext)
539 struct enum_types_AtoW et;
541 et.callback = EnumSymbolsCallback;
542 et.user = UserContext;
544 return SymEnumTypes(hProcess, BaseOfDll, enum_types_AtoW, &et);
547 static void enum_types_of_module(struct module_pair* pair, const char* name, PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
549 char buffer[sizeof(SYMBOL_INFO) + 256];
550 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
551 struct symt* type;
552 DWORD64 size;
553 unsigned i;
554 const char* tname;
556 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
557 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
559 for (i = 0; i < vector_length(&pair->effective->vtypes); i++)
561 type = *(struct symt**)vector_at(&pair->effective->vtypes, i);
562 tname = symt_get_name(type);
563 if (tname && SymMatchStringA(tname, name, TRUE))
565 sym_info->TypeIndex = symt_ptr2index(pair->effective, type);
566 sym_info->Index = 0; /* FIXME */
567 symt_get_info(pair->effective, type, TI_GET_LENGTH, &size);
568 sym_info->Size = size;
569 sym_info->ModBase = pair->requested->module.BaseOfImage;
570 sym_info->Flags = 0; /* FIXME */
571 sym_info->Value = 0; /* FIXME */
572 sym_info->Address = 0; /* FIXME */
573 sym_info->Register = 0; /* FIXME */
574 sym_info->Scope = 0; /* FIXME */
575 sym_info->Tag = type->tag;
576 symbol_setname(sym_info, tname);
577 if (!cb(sym_info, sym_info->Size, user)) break;
582 static BOOL walk_modules(struct module_pair* pair)
584 /* first walk PE only modules */
585 if (!pair->requested || pair->requested->type == DMT_PE)
587 while ((pair->requested = pair->requested ? pair->requested->next : pair->pcs->lmodules) != NULL)
589 if (pair->requested->type == DMT_PE && module_get_debug(pair)) return TRUE;
592 /* then walk ELF or Mach-O modules, not containing PE modules */
593 while ((pair->requested = pair->requested ? pair->requested->next : pair->pcs->lmodules) != NULL)
595 if ((pair->requested->type == DMT_ELF || pair->requested->type == DMT_MACHO) &&
596 !module_get_containee(pair->pcs, pair->requested) &&
597 module_get_debug(pair))
598 return TRUE;
600 return FALSE;
603 BOOL WINAPI SymEnumTypesByName(HANDLE proc, ULONG64 base, PCSTR name, PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
605 struct module_pair pair;
606 const char* bang;
608 TRACE("(%p %I64x %s %p %p)\n", proc, base, wine_dbgstr_a(name), cb, user);
610 if (!name) return SymEnumTypes(proc, base, cb, user);
611 bang = strchr(name, '!');
612 if (bang)
614 DWORD sz;
615 WCHAR* modW;
616 pair.pcs = process_find_by_handle(proc);
617 if (bang == name) return FALSE;
618 if (!pair.pcs) return FALSE;
619 sz = MultiByteToWideChar(CP_ACP, 0, name, bang - name, NULL, 0) + 1;
620 if ((modW = malloc(sz * sizeof(WCHAR))) == NULL) return FALSE;
621 MultiByteToWideChar(CP_ACP, 0, name, bang - name, modW, sz);
622 modW[sz - 1] = L'\0';
623 pair.requested = NULL;
624 while (walk_modules(&pair))
626 if (SymMatchStringW(pair.requested->modulename, modW, FALSE))
627 enum_types_of_module(&pair, bang + 1, cb, user);
629 free(modW);
631 else
633 if (!module_init_pair(&pair, proc, base) || !module_get_debug(&pair)) return FALSE;
634 enum_types_of_module(&pair, name, cb, user);
636 return TRUE;
639 BOOL WINAPI SymEnumTypesByNameW(HANDLE proc, ULONG64 base, PCWSTR nameW, PSYM_ENUMERATESYMBOLS_CALLBACKW cb, PVOID user)
641 struct enum_types_AtoW et;
642 DWORD len = nameW ? WideCharToMultiByte(CP_ACP, 0, nameW, -1, NULL, 0, NULL, NULL) : 0;
643 char* name;
644 BOOL ret;
646 TRACE("(%p %I64x %s %p %p)\n", proc, base, wine_dbgstr_w(nameW), cb, user);
648 if (len)
650 if (!(name = malloc(len))) return FALSE;
651 WideCharToMultiByte(CP_ACP, 0, nameW, -1, name, len, NULL, NULL);
653 else name = NULL;
655 et.callback = cb;
656 et.user = user;
658 ret = SymEnumTypesByName(proc, base, name, enum_types_AtoW, &et);
659 free(name);
660 return ret;
663 /******************************************************************
664 * symt_get_info
666 * Retrieves information about a symt (either symbol or type)
668 BOOL symt_get_info(struct module* module, const struct symt* type,
669 IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo)
671 unsigned len;
673 if (!type) return FALSE;
675 /* helper to typecast pInfo to its expected type (_t) */
676 #define X(_t) (*((_t*)pInfo))
678 switch (req)
680 case TI_FINDCHILDREN:
682 const struct vector* v;
683 struct symt** pt;
684 unsigned i;
685 TI_FINDCHILDREN_PARAMS* tifp = pInfo;
687 switch (type->tag)
689 case SymTagExe: v = &((const struct symt_module*)type)->vchildren; break;
690 case SymTagCompiland: v = &((const struct symt_compiland*)type)->vchildren; break;
691 case SymTagUDT: v = &((const struct symt_udt*)type)->vchildren; break;
692 case SymTagEnum: v = &((const struct symt_enum*)type)->vchildren; break;
693 case SymTagFunctionType: v = &((const struct symt_function_signature*)type)->vchildren; break;
694 case SymTagFunction: v = &((const struct symt_function*)type)->vchildren; break;
695 case SymTagInlineSite: v = &((const struct symt_inlinesite*)type)->func.vchildren; break;
696 case SymTagBlock: v = &((const struct symt_block*)type)->vchildren; break;
697 case SymTagPointerType:
698 case SymTagArrayType:
699 case SymTagFunctionArgType:
700 case SymTagThunk:
701 case SymTagLabel:
702 case SymTagFuncDebugStart:
703 case SymTagFuncDebugEnd:
704 case SymTagTypedef:
705 case SymTagBaseType:
706 case SymTagCustom:
707 /* for those, CHILDRENCOUNT returns 0 */
708 return tifp->Count == 0;
709 default:
710 FIXME("Unsupported sym-tag %s for find-children\n",
711 symt_get_tag_str(type->tag));
712 return FALSE;
714 for (i = 0; i < tifp->Count; i++)
716 if (!(pt = vector_at(v, tifp->Start + i))) return FALSE;
717 tifp->ChildId[i] = symt_ptr2index(module, *pt);
720 break;
722 case TI_GET_ADDRESS:
723 return symt_get_address(type, (ULONG64*)pInfo);
725 case TI_GET_BASETYPE:
726 switch (type->tag)
728 case SymTagBaseType:
729 X(DWORD) = ((const struct symt_basic*)type)->bt;
730 break;
731 case SymTagEnum:
732 return symt_get_info(module, ((const struct symt_enum*)type)->base_type, req, pInfo);
733 default:
734 return FALSE;
736 break;
738 case TI_GET_BITPOSITION:
739 if (type->tag == SymTagData &&
740 ((const struct symt_data*)type)->kind == DataIsMember &&
741 ((const struct symt_data*)type)->u.member.bit_length != 0)
742 X(DWORD) = ((const struct symt_data*)type)->u.member.bit_offset;
743 else return FALSE;
744 break;
746 case TI_GET_CHILDRENCOUNT:
747 switch (type->tag)
749 case SymTagExe:
750 X(DWORD) = vector_length(&((const struct symt_module*)type)->vchildren);
751 break;
752 case SymTagCompiland:
753 X(DWORD) = vector_length(&((const struct symt_compiland*)type)->vchildren);
754 break;
755 case SymTagUDT:
756 X(DWORD) = vector_length(&((const struct symt_udt*)type)->vchildren);
757 break;
758 case SymTagEnum:
759 X(DWORD) = vector_length(&((const struct symt_enum*)type)->vchildren);
760 break;
761 case SymTagFunctionType:
762 X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
763 break;
764 case SymTagFunction:
765 X(DWORD) = vector_length(&((const struct symt_function*)type)->vchildren);
766 break;
767 case SymTagInlineSite:
768 X(DWORD) = vector_length(&((const struct symt_inlinesite*)type)->func.vchildren);
769 break;
770 case SymTagBlock:
771 X(DWORD) = vector_length(&((const struct symt_block*)type)->vchildren);
772 break;
773 /* some SymTag:s return 0 */
774 case SymTagPointerType:
775 case SymTagArrayType:
776 case SymTagFunctionArgType:
777 case SymTagThunk:
778 case SymTagFuncDebugStart:
779 case SymTagFuncDebugEnd:
780 case SymTagLabel:
781 case SymTagTypedef:
782 case SymTagBaseType:
783 case SymTagCustom:
784 X(DWORD) = 0;
785 break;
786 default:
787 FIXME("Unsupported sym-tag %s for get-children-count\n",
788 symt_get_tag_str(type->tag));
789 /* fall through */
790 /* some others return error */
791 case SymTagData:
792 case SymTagPublicSymbol:
793 return FALSE;
795 break;
797 case TI_GET_COUNT:
798 switch (type->tag)
800 case SymTagArrayType:
801 X(DWORD) = ((const struct symt_array*)type)->count;
802 break;
803 case SymTagFunctionType:
804 /* this seems to be wrong for (future) C++ methods, where 'this' parameter
805 * should be included in this value (and not in GET_CHILDREN_COUNT)
807 X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
808 break;
809 default: return FALSE;
811 break;
813 case TI_GET_DATAKIND:
814 if (type->tag != SymTagData) return FALSE;
815 X(DWORD) = ((const struct symt_data*)type)->kind;
816 break;
818 case TI_GET_LENGTH:
819 switch (type->tag)
821 case SymTagBaseType:
822 X(DWORD64) = ((const struct symt_basic*)type)->size;
823 break;
824 case SymTagFunction:
825 X(DWORD64) = ((const struct symt_function*)type)->size;
826 break;
827 case SymTagBlock:
828 X(DWORD64) = ((const struct symt_block*)type)->size;
829 break;
830 case SymTagPointerType:
831 X(DWORD64) = ((const struct symt_pointer*)type)->size;
832 break;
833 case SymTagUDT:
834 X(DWORD64) = ((const struct symt_udt*)type)->size;
835 break;
836 case SymTagEnum:
837 if (!symt_get_info(module, ((const struct symt_enum*)type)->base_type, TI_GET_LENGTH, pInfo))
838 return FALSE;
839 break;
840 case SymTagData:
841 if (((const struct symt_data*)type)->kind != DataIsMember ||
842 !((const struct symt_data*)type)->u.member.bit_length)
843 return FALSE;
844 X(DWORD64) = ((const struct symt_data*)type)->u.member.bit_length;
845 break;
846 case SymTagArrayType:
847 if (!symt_get_info(module, ((const struct symt_array*)type)->base_type,
848 TI_GET_LENGTH, pInfo))
849 return FALSE;
850 X(DWORD64) *= ((const struct symt_array*)type)->count;
851 break;
852 case SymTagPublicSymbol:
853 X(DWORD64) = ((const struct symt_public*)type)->size;
854 break;
855 case SymTagTypedef:
856 return symt_get_info(module, ((const struct symt_typedef*)type)->type, TI_GET_LENGTH, pInfo);
857 case SymTagThunk:
858 X(DWORD64) = ((const struct symt_thunk*)type)->size;
859 break;
860 case SymTagCustom:
861 X(DWORD64) = ((const struct symt_custom*)type)->size;
862 break;
863 default:
864 FIXME("Unsupported sym-tag %s for get-length\n",
865 symt_get_tag_str(type->tag));
866 /* fall through */
867 case SymTagExe:
868 case SymTagCompiland:
869 case SymTagFunctionType:
870 case SymTagFunctionArgType:
871 case SymTagInlineSite: /* native doesn't expose it, perhaps because of non-contiguous range */
872 case SymTagLabel:
873 case SymTagFuncDebugStart:
874 case SymTagFuncDebugEnd:
875 return FALSE;
877 break;
879 case TI_GET_LEXICALPARENT:
880 switch (type->tag)
882 case SymTagCompiland:
883 X(DWORD) = symt_ptr2index(module, &((const struct symt_compiland*)type)->container->symt);
884 break;
885 case SymTagBlock:
886 X(DWORD) = symt_ptr2index(module, ((const struct symt_block*)type)->container);
887 break;
888 case SymTagData:
889 X(DWORD) = symt_ptr2index(module, ((const struct symt_data*)type)->container);
890 break;
891 case SymTagFunction:
892 X(DWORD) = symt_ptr2index(module, ((const struct symt_function*)type)->container);
893 break;
894 case SymTagInlineSite:
895 X(DWORD) = symt_ptr2index(module, ((const struct symt_inlinesite*)type)->func.container);
896 break;
897 case SymTagThunk:
898 X(DWORD) = symt_ptr2index(module, ((const struct symt_thunk*)type)->container);
899 break;
900 case SymTagFuncDebugStart:
901 case SymTagFuncDebugEnd:
902 case SymTagLabel:
903 X(DWORD) = symt_ptr2index(module, ((const struct symt_hierarchy_point*)type)->parent);
904 break;
905 case SymTagUDT:
906 case SymTagEnum:
907 case SymTagFunctionType:
908 case SymTagFunctionArgType:
909 case SymTagPointerType:
910 case SymTagArrayType:
911 case SymTagBaseType:
912 case SymTagTypedef:
913 case SymTagBaseClass:
914 case SymTagPublicSymbol:
915 case SymTagCustom:
916 X(DWORD) = symt_ptr2index(module, &module->top->symt);
917 break;
918 default:
919 FIXME("Unsupported sym-tag %s for get-lexical-parent\n",
920 symt_get_tag_str(type->tag));
921 /* fall through */
922 case SymTagExe:
923 return FALSE;
925 break;
927 case TI_GET_NESTED:
928 switch (type->tag)
930 case SymTagUDT:
931 case SymTagEnum:
932 X(DWORD) = 0;
933 break;
934 default:
935 return FALSE;
937 break;
939 case TI_GET_OFFSET:
940 switch (type->tag)
942 case SymTagData:
943 switch (((const struct symt_data*)type)->kind)
945 case DataIsParam:
946 case DataIsLocal:
947 X(ULONG) = ((const struct symt_data*)type)->u.var.offset;
948 break;
949 case DataIsMember:
950 X(ULONG) = ((const struct symt_data*)type)->u.member.offset;
951 break;
952 default:
953 FIXME("Unknown kind (%u) for get-offset\n",
954 ((const struct symt_data*)type)->kind);
955 return FALSE;
957 break;
958 default:
959 FIXME("Unsupported sym-tag %s for get-offset\n",
960 symt_get_tag_str(type->tag));
961 /* fall through */
962 case SymTagExe:
963 case SymTagCompiland:
964 case SymTagUDT:
965 case SymTagFunctionType:
966 case SymTagFunctionArgType:
967 case SymTagPointerType:
968 case SymTagArrayType:
969 case SymTagBaseType:
970 case SymTagTypedef:
971 case SymTagFunction:
972 case SymTagBlock:
973 case SymTagFuncDebugStart:
974 case SymTagFuncDebugEnd:
975 case SymTagLabel:
976 case SymTagInlineSite:
977 case SymTagCustom:
978 return FALSE;
980 break;
982 case TI_GET_SYMNAME:
983 if (type->tag == SymTagExe)
985 DWORD len = (lstrlenW(module->modulename) + 1) * sizeof(WCHAR);
986 WCHAR* wname = HeapAlloc(GetProcessHeap(), 0, len);
987 if (!wname) return FALSE;
988 memcpy(wname, module->modulename, len);
989 X(WCHAR*) = wname;
991 else
993 const char* name = symt_get_name(type);
994 if (!name) return FALSE;
995 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
996 X(WCHAR*) = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
997 if (!X(WCHAR*)) return FALSE;
998 MultiByteToWideChar(CP_ACP, 0, name, -1, X(WCHAR*), len);
1000 break;
1002 case TI_GET_SYMTAG:
1003 X(DWORD) = type->tag;
1004 break;
1006 case TI_GET_TYPE:
1007 case TI_GET_TYPEID:
1008 switch (type->tag)
1010 /* hierarchical => hierarchical */
1011 case SymTagArrayType:
1012 X(DWORD) = symt_ptr2index(module, ((const struct symt_array*)type)->base_type);
1013 break;
1014 case SymTagPointerType:
1015 X(DWORD) = symt_ptr2index(module, ((const struct symt_pointer*)type)->pointsto);
1016 break;
1017 case SymTagFunctionType:
1018 X(DWORD) = symt_ptr2index(module, ((const struct symt_function_signature*)type)->rettype);
1019 break;
1020 case SymTagTypedef:
1021 X(DWORD) = symt_ptr2index(module, ((const struct symt_typedef*)type)->type);
1022 break;
1023 /* lexical => hierarchical */
1024 case SymTagData:
1025 X(DWORD) = symt_ptr2index(module, ((const struct symt_data*)type)->type);
1026 break;
1027 case SymTagFunction:
1028 X(DWORD) = symt_ptr2index(module, ((const struct symt_function*)type)->type);
1029 break;
1030 case SymTagInlineSite:
1031 X(DWORD) = symt_ptr2index(module, ((const struct symt_inlinesite*)type)->func.type);
1032 break;
1033 case SymTagEnum:
1034 X(DWORD) = symt_ptr2index(module, ((const struct symt_enum*)type)->base_type);
1035 break;
1036 case SymTagFunctionArgType:
1037 X(DWORD) = symt_ptr2index(module, ((const struct symt_function_arg_type*)type)->arg_type);
1038 break;
1039 default:
1040 FIXME("Unsupported sym-tag %s for get-type\n",
1041 symt_get_tag_str(type->tag));
1042 /* fall through */
1043 case SymTagPublicSymbol:
1044 case SymTagThunk:
1045 case SymTagBlock:
1046 case SymTagFuncDebugStart:
1047 case SymTagFuncDebugEnd:
1048 case SymTagLabel:
1049 case SymTagExe:
1050 case SymTagCompiland:
1051 case SymTagUDT:
1052 case SymTagBaseType:
1053 case SymTagCustom:
1054 return FALSE;
1056 break;
1058 case TI_GET_UDTKIND:
1059 if (type->tag != SymTagUDT) return FALSE;
1060 X(DWORD) = ((const struct symt_udt*)type)->kind;
1061 break;
1063 case TI_GET_VALUE:
1064 if (type->tag != SymTagData) return FALSE;
1065 switch (((const struct symt_data*)type)->kind)
1067 case DataIsConstant: X(VARIANT) = ((const struct symt_data*)type)->u.value; break;
1068 case DataIsLocal:
1069 case DataIsParam:
1071 struct location loc = ((const struct symt_data*)type)->u.var;
1072 unsigned i;
1073 struct module_format* modfmt;
1075 if (loc.kind < loc_user) return FALSE;
1076 for (i = 0; i < DFI_LAST; i++)
1078 modfmt = module->format_info[i];
1079 if (modfmt && modfmt->loc_compute)
1081 modfmt->loc_compute(module->process, modfmt,
1082 (const struct symt_function*)((const struct symt_data*)type)->container, &loc);
1083 break;
1086 if (loc.kind != loc_absolute) return FALSE;
1087 V_VT(&X(VARIANT)) = VT_UI4; /* FIXME */
1088 V_UI4(&X(VARIANT)) = loc.offset;
1090 break;
1091 default: return FALSE;
1093 break;
1095 case TI_GET_CALLING_CONVENTION:
1096 if (type->tag != SymTagFunctionType) return FALSE;
1097 if (((const struct symt_function_signature*)type)->call_conv == -1)
1099 FIXME("No support for calling convention for this signature\n");
1100 X(DWORD) = CV_CALL_FAR_C; /* FIXME */
1102 else X(DWORD) = ((const struct symt_function_signature*)type)->call_conv;
1103 break;
1104 case TI_GET_ARRAYINDEXTYPEID:
1105 if (type->tag != SymTagArrayType) return FALSE;
1106 X(DWORD) = symt_ptr2index(module, ((const struct symt_array*)type)->index_type);
1107 break;
1109 case TI_GET_SYMINDEX:
1110 /* not very useful as it is...
1111 * native sometimes (eg for UDT) return id of another instance
1112 * of the same UDT definition... maybe forward declaration?
1114 X(DWORD) = symt_ptr2index(module, type);
1115 break;
1117 /* FIXME: we don't support properly C++ for now */
1118 case TI_GET_VIRTUALBASECLASS:
1119 case TI_GET_VIRTUALTABLESHAPEID:
1120 case TI_GET_VIRTUALBASEPOINTEROFFSET:
1121 case TI_GET_CLASSPARENTID:
1122 case TI_GET_THISADJUST:
1123 case TI_GET_VIRTUALBASEOFFSET:
1124 case TI_GET_VIRTUALBASEDISPINDEX:
1125 case TI_GET_IS_REFERENCE:
1126 case TI_GET_INDIRECTVIRTUALBASECLASS:
1127 case TI_GET_VIRTUALBASETABLETYPE:
1128 case TI_GET_OBJECTPOINTERTYPE:
1129 return FALSE;
1131 #undef X
1133 default:
1135 static DWORD64 once;
1136 if (!(once & ((DWORD64)1 << req)))
1138 FIXME("Unsupported GetInfo request (%u)\n", req);
1139 once |= (DWORD64)1 << req;
1142 return FALSE;
1145 return TRUE;
1148 /******************************************************************
1149 * SymGetTypeInfo (DBGHELP.@)
1152 BOOL WINAPI SymGetTypeInfo(HANDLE hProcess, DWORD64 ModBase,
1153 ULONG TypeId, IMAGEHLP_SYMBOL_TYPE_INFO GetType,
1154 PVOID pInfo)
1156 struct module_pair pair;
1158 if (!module_init_pair(&pair, hProcess, ModBase)) return FALSE;
1159 return symt_get_info(pair.effective, symt_index2ptr(pair.effective, TypeId), GetType, pInfo);
1162 /******************************************************************
1163 * SymGetTypeFromName (DBGHELP.@)
1166 BOOL WINAPI SymGetTypeFromName(HANDLE hProcess, ULONG64 BaseOfDll,
1167 PCSTR Name, PSYMBOL_INFO Symbol)
1169 struct module_pair pair;
1170 struct symt* type;
1171 DWORD64 size;
1173 if (!module_init_pair(&pair, hProcess, BaseOfDll)) return FALSE;
1174 type = symt_find_type_by_name(pair.effective, SymTagNull, Name);
1175 if (!type) return FALSE;
1176 Symbol->Index = Symbol->TypeIndex = symt_ptr2index(pair.effective, type);
1177 symbol_setname(Symbol, symt_get_name(type));
1178 symt_get_info(pair.effective, type, TI_GET_LENGTH, &size);
1179 Symbol->Size = size;
1180 Symbol->ModBase = pair.requested->module.BaseOfImage;
1181 Symbol->Tag = type->tag;
1183 return TRUE;