ca57d1a3e28612bb241b58473083d258347a6f0a
[reactos.git] / reactos / dll / win32 / dbghelp / type.c
1 /*
2 * File types.c - management of types (hierarchical tree)
3 *
4 * Copyright (C) 1997, Eric Youngdale.
5 * 2004, Eric Pouech.
6 *
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.
11 *
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.
16 *
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
20 *
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.
23 */
24
25 #include "dbghelp_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
28 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
29
30 static const char* symt_get_tag_str(DWORD tag)
31 {
32 switch (tag)
33 {
34 case SymTagNull: return "SymTagNull";
35 case SymTagExe: return "SymTagExe";
36 case SymTagCompiland: return "SymTagCompiland";
37 case SymTagCompilandDetails: return "SymTagCompilandDetails";
38 case SymTagCompilandEnv: return "SymTagCompilandEnv";
39 case SymTagFunction: return "SymTagFunction";
40 case SymTagBlock: return "SymTagBlock";
41 case SymTagData: return "SymTagData";
42 case SymTagAnnotation: return "SymTagAnnotation";
43 case SymTagLabel: return "SymTagLabel";
44 case SymTagPublicSymbol: return "SymTagPublicSymbol";
45 case SymTagUDT: return "SymTagUDT";
46 case SymTagEnum: return "SymTagEnum";
47 case SymTagFunctionType: return "SymTagFunctionType";
48 case SymTagPointerType: return "SymTagPointerType";
49 case SymTagArrayType: return "SymTagArrayType";
50 case SymTagBaseType: return "SymTagBaseType";
51 case SymTagTypedef: return "SymTagTypedef,";
52 case SymTagBaseClass: return "SymTagBaseClass";
53 case SymTagFriend: return "SymTagFriend";
54 case SymTagFunctionArgType: return "SymTagFunctionArgType,";
55 case SymTagFuncDebugStart: return "SymTagFuncDebugStart,";
56 case SymTagFuncDebugEnd: return "SymTagFuncDebugEnd";
57 case SymTagUsingNamespace: return "SymTagUsingNamespace,";
58 case SymTagVTableShape: return "SymTagVTableShape";
59 case SymTagVTable: return "SymTagVTable";
60 case SymTagCustom: return "SymTagCustom";
61 case SymTagThunk: return "SymTagThunk";
62 case SymTagCustomType: return "SymTagCustomType";
63 case SymTagManagedType: return "SymTagManagedType";
64 case SymTagDimension: return "SymTagDimension";
65 default: return "---";
66 }
67 }
68
69 const char* symt_get_name(const struct symt* sym)
70 {
71 switch (sym->tag)
72 {
73 /* lexical tree */
74 case SymTagData: return ((const struct symt_data*)sym)->hash_elt.name;
75 case SymTagFunction: return ((const struct symt_function*)sym)->hash_elt.name;
76 case SymTagPublicSymbol: return ((const struct symt_public*)sym)->hash_elt.name;
77 case SymTagBaseType: return ((const struct symt_basic*)sym)->hash_elt.name;
78 case SymTagLabel: return ((const struct symt_hierarchy_point*)sym)->hash_elt.name;
79 case SymTagThunk: return ((const struct symt_thunk*)sym)->hash_elt.name;
80 /* hierarchy tree */
81 case SymTagEnum: return ((const struct symt_enum*)sym)->name;
82 case SymTagTypedef: return ((const struct symt_typedef*)sym)->hash_elt.name;
83 case SymTagUDT: return ((const struct symt_udt*)sym)->hash_elt.name;
84 default:
85 FIXME("Unsupported sym-tag %s\n", symt_get_tag_str(sym->tag));
86 /* fall through */
87 case SymTagArrayType:
88 case SymTagPointerType:
89 case SymTagFunctionType:
90 return NULL;
91 }
92 }
93
94 WCHAR* symt_get_nameW(const struct symt* sym)
95 {
96 const char* name = symt_get_name(sym);
97 WCHAR* nameW;
98 DWORD sz;
99
100 if (!name) return NULL;
101 sz = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
102 if ((nameW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
103 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sz);
104 return nameW;
105 }
106
107 BOOL symt_get_address(const struct symt* type, ULONG64* addr)
108 {
109 switch (type->tag)
110 {
111 case SymTagData:
112 switch (((const struct symt_data*)type)->kind)
113 {
114 case DataIsGlobal:
115 case DataIsFileStatic:
116 *addr = ((const struct symt_data*)type)->u.var.offset;
117 break;
118 default: return FALSE;
119 }
120 break;
121 case SymTagFunction:
122 *addr = ((const struct symt_function*)type)->address;
123 break;
124 case SymTagPublicSymbol:
125 *addr = ((const struct symt_public*)type)->address;
126 break;
127 case SymTagFuncDebugStart:
128 case SymTagFuncDebugEnd:
129 case SymTagLabel:
130 if (!((const struct symt_hierarchy_point*)type)->parent ||
131 !symt_get_address(((const struct symt_hierarchy_point*)type)->parent, addr))
132 return FALSE;
133 *addr += ((const struct symt_hierarchy_point*)type)->loc.offset;
134 break;
135 case SymTagThunk:
136 *addr = ((const struct symt_thunk*)type)->address;
137 break;
138 case SymTagCompiland:
139 *addr = ((const struct symt_compiland*)type)->address;
140 break;
141 default:
142 FIXME("Unsupported sym-tag %s for get-address\n", symt_get_tag_str(type->tag));
143 return FALSE;
144 }
145 return TRUE;
146 }
147
148 static struct symt* symt_find_type_by_name(const struct module* module,
149 enum SymTagEnum sym_tag,
150 const char* typename)
151 {
152 void* ptr;
153 struct symt_ht* type;
154 struct hash_table_iter hti;
155
156 assert(typename);
157 assert(module);
158
159 hash_table_iter_init(&module->ht_types, &hti, typename);
160 while ((ptr = hash_table_iter_up(&hti)))
161 {
162 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
163
164 if ((sym_tag == SymTagNull || type->symt.tag == sym_tag) &&
165 type->hash_elt.name && !strcmp(type->hash_elt.name, typename))
166 return &type->symt;
167 }
168 SetLastError(ERROR_INVALID_NAME); /* FIXME ?? */
169 return NULL;
170 }
171
172 static void symt_add_type(struct module* module, struct symt* symt)
173 {
174 struct symt** p;
175 p = vector_add(&module->vtypes, &module->pool);
176 assert(p);
177 *p = symt;
178 }
179
180 struct symt_basic* symt_new_basic(struct module* module, enum BasicType bt,
181 const char* typename, unsigned size)
182 {
183 struct symt_basic* sym;
184
185 if (typename)
186 {
187 sym = (struct symt_basic*)symt_find_type_by_name(module, SymTagBaseType,
188 typename);
189 if (sym && sym->bt == bt && sym->size == size)
190 return sym;
191 }
192 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
193 {
194 sym->symt.tag = SymTagBaseType;
195 if (typename)
196 {
197 sym->hash_elt.name = pool_strdup(&module->pool, typename);
198 hash_table_add(&module->ht_types, &sym->hash_elt);
199 } else sym->hash_elt.name = NULL;
200 sym->bt = bt;
201 sym->size = size;
202 symt_add_type(module, &sym->symt);
203 }
204 return sym;
205 }
206
207 struct symt_udt* symt_new_udt(struct module* module, const char* typename,
208 unsigned size, enum UdtKind kind)
209 {
210 struct symt_udt* sym;
211
212 TRACE_(dbghelp_symt)("Adding udt %s:%s\n",
213 debugstr_w(module->module.ModuleName), typename);
214 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
215 {
216 sym->symt.tag = SymTagUDT;
217 sym->kind = kind;
218 sym->size = size;
219 if (typename)
220 {
221 sym->hash_elt.name = pool_strdup(&module->pool, typename);
222 hash_table_add(&module->ht_types, &sym->hash_elt);
223 } else sym->hash_elt.name = NULL;
224 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
225 symt_add_type(module, &sym->symt);
226 }
227 return sym;
228 }
229
230 BOOL symt_set_udt_size(struct module* module, struct symt_udt* udt, unsigned size)
231 {
232 assert(udt->symt.tag == SymTagUDT);
233 if (vector_length(&udt->vchildren) != 0)
234 {
235 if (udt->size != size)
236 FIXME_(dbghelp_symt)("Changing size for %s from %u to %u\n",
237 udt->hash_elt.name, udt->size, size);
238 return TRUE;
239 }
240 udt->size = size;
241 return TRUE;
242 }
243
244 /******************************************************************
245 * symt_add_udt_element
246 *
247 * add an element to a udt (struct, class, union)
248 * the size & offset parameters are expressed in bits (not bytes) so that
249 * we can mix in the single call bytes aligned elements (regular fields) and
250 * the others (bit fields)
251 */
252 BOOL symt_add_udt_element(struct module* module, struct symt_udt* udt_type,
253 const char* name, struct symt* elt_type,
254 unsigned offset, unsigned size)
255 {
256 struct symt_data* m;
257 struct symt** p;
258
259 assert(udt_type->symt.tag == SymTagUDT);
260
261 TRACE_(dbghelp_symt)("Adding %s to UDT %s\n", name, udt_type->hash_elt.name);
262 if (name)
263 {
264 unsigned int i;
265 for (i=0; i<vector_length(&udt_type->vchildren); i++)
266 {
267 m = *(struct symt_data**)vector_at(&udt_type->vchildren, i);
268 assert(m);
269 assert(m->symt.tag == SymTagData);
270 if (strcmp(m->hash_elt.name, name) == 0)
271 return TRUE;
272 }
273 }
274
275 if ((m = pool_alloc(&module->pool, sizeof(*m))) == NULL) return FALSE;
276 memset(m, 0, sizeof(*m));
277 m->symt.tag = SymTagData;
278 m->hash_elt.name = name ? pool_strdup(&module->pool, name) : "";
279 m->hash_elt.next = NULL;
280
281 m->kind = DataIsMember;
282 m->container = &udt_type->symt;
283 m->type = elt_type;
284 m->u.member.offset = offset;
285 m->u.member.length = ((offset & 7) || (size & 7)) ? size : 0;
286 p = vector_add(&udt_type->vchildren, &module->pool);
287 *p = &m->symt;
288
289 return TRUE;
290 }
291
292 struct symt_enum* symt_new_enum(struct module* module, const char* typename,
293 struct symt* basetype)
294 {
295 struct symt_enum* sym;
296
297 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
298 {
299 sym->symt.tag = SymTagEnum;
300 sym->name = (typename) ? pool_strdup(&module->pool, typename) : NULL;
301 sym->base_type = basetype;
302 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
303 }
304 return sym;
305 }
306
307 BOOL symt_add_enum_element(struct module* module, struct symt_enum* enum_type,
308 const char* name, int value)
309 {
310 struct symt_data* e;
311 struct symt** p;
312
313 assert(enum_type->symt.tag == SymTagEnum);
314 e = pool_alloc(&module->pool, sizeof(*e));
315 if (e == NULL) return FALSE;
316
317 e->symt.tag = SymTagData;
318 e->hash_elt.name = pool_strdup(&module->pool, name);
319 e->hash_elt.next = NULL;
320 e->kind = DataIsConstant;
321 e->container = &enum_type->symt;
322 e->type = enum_type->base_type;
323 e->u.value.n1.n2.vt = VT_I4;
324 e->u.value.n1.n2.n3.lVal = value;
325
326 p = vector_add(&enum_type->vchildren, &module->pool);
327 if (!p) return FALSE; /* FIXME we leak e */
328 *p = &e->symt;
329
330 return TRUE;
331 }
332
333 struct symt_array* symt_new_array(struct module* module, int min, int max,
334 struct symt* base, struct symt* index)
335 {
336 struct symt_array* sym;
337
338 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
339 {
340 sym->symt.tag = SymTagArrayType;
341 sym->start = min;
342 sym->end = max;
343 sym->base_type = base;
344 sym->index_type = index;
345 symt_add_type(module, &sym->symt);
346 }
347 return sym;
348 }
349
350 static inline DWORD symt_array_count(struct module* module, const struct symt_array* array)
351 {
352 if (array->end < 0)
353 {
354 DWORD64 elem_size;
355 /* One could want to also set the array->end field in array, but we won't do it
356 * as long as all the get_type() helpers use const objects
357 */
358 if (symt_get_info(module, array->base_type, TI_GET_LENGTH, &elem_size) && elem_size)
359 return -array->end / (DWORD)elem_size;
360 return 0;
361 }
362 return array->end - array->start + 1;
363 }
364
365 struct symt_function_signature* symt_new_function_signature(struct module* module,
366 struct symt* ret_type,
367 enum CV_call_e call_conv)
368 {
369 struct symt_function_signature* sym;
370
371 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
372 {
373 sym->symt.tag = SymTagFunctionType;
374 sym->rettype = ret_type;
375 vector_init(&sym->vchildren, sizeof(struct symt*), 4);
376 sym->call_conv = call_conv;
377 symt_add_type(module, &sym->symt);
378 }
379 return sym;
380 }
381
382 BOOL symt_add_function_signature_parameter(struct module* module,
383 struct symt_function_signature* sig_type,
384 struct symt* param)
385 {
386 struct symt** p;
387 struct symt_function_arg_type* arg;
388
389 assert(sig_type->symt.tag == SymTagFunctionType);
390 arg = pool_alloc(&module->pool, sizeof(*arg));
391 if (!arg) return FALSE;
392 arg->symt.tag = SymTagFunctionArgType;
393 arg->arg_type = param;
394 arg->container = &sig_type->symt;
395 p = vector_add(&sig_type->vchildren, &module->pool);
396 if (!p) return FALSE; /* FIXME we leak arg */
397 *p = &arg->symt;
398
399 return TRUE;
400 }
401
402 struct symt_pointer* symt_new_pointer(struct module* module, struct symt* ref_type, unsigned long size)
403 {
404 struct symt_pointer* sym;
405
406 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
407 {
408 sym->symt.tag = SymTagPointerType;
409 sym->pointsto = ref_type;
410 sym->size = size;
411 symt_add_type(module, &sym->symt);
412 }
413 return sym;
414 }
415
416 struct symt_typedef* symt_new_typedef(struct module* module, struct symt* ref,
417 const char* name)
418 {
419 struct symt_typedef* sym;
420
421 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
422 {
423 sym->symt.tag = SymTagTypedef;
424 sym->type = ref;
425 sym->hash_elt.name = pool_strdup(&module->pool, name);
426 hash_table_add(&module->ht_types, &sym->hash_elt);
427 symt_add_type(module, &sym->symt);
428 }
429 return sym;
430 }
431
432 /******************************************************************
433 * SymEnumTypes (DBGHELP.@)
434 *
435 */
436 BOOL WINAPI SymEnumTypes(HANDLE hProcess, ULONG64 BaseOfDll,
437 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
438 PVOID UserContext)
439 {
440 struct module_pair pair;
441 char buffer[sizeof(SYMBOL_INFO) + 256];
442 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
443 const char* tmp;
444 struct symt* type;
445 DWORD64 size;
446 unsigned int i;
447
448 TRACE("(%p %s %p %p)\n",
449 hProcess, wine_dbgstr_longlong(BaseOfDll), EnumSymbolsCallback,
450 UserContext);
451
452 if (!(pair.pcs = process_find_by_handle(hProcess))) return FALSE;
453 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
454 if (!module_get_debug(&pair)) return FALSE;
455
456 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
457 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
458
459 for (i=0; i<vector_length(&pair.effective->vtypes); i++)
460 {
461 type = *(struct symt**)vector_at(&pair.effective->vtypes, i);
462 sym_info->TypeIndex = symt_ptr2index(pair.effective, type);
463 sym_info->info = 0; /* FIXME */
464 symt_get_info(pair.effective, type, TI_GET_LENGTH, &size);
465 sym_info->Size = size;
466 sym_info->ModBase = pair.requested->module.BaseOfImage;
467 sym_info->Flags = 0; /* FIXME */
468 sym_info->Value = 0; /* FIXME */
469 sym_info->Address = 0; /* FIXME */
470 sym_info->Register = 0; /* FIXME */
471 sym_info->Scope = 0; /* FIXME */
472 sym_info->Tag = type->tag;
473 tmp = symt_get_name(type);
474 if (tmp)
475 {
476 sym_info->NameLen = min(strlen(tmp),sym_info->MaxNameLen-1);
477 memcpy(sym_info->Name, tmp, sym_info->NameLen);
478 sym_info->Name[sym_info->NameLen] = '\0';
479 }
480 else
481 sym_info->Name[sym_info->NameLen = 0] = '\0';
482 if (!EnumSymbolsCallback(sym_info, sym_info->Size, UserContext)) break;
483 }
484 return TRUE;
485 }
486
487 struct enum_types_AtoW
488 {
489 char buffer[sizeof(SYMBOL_INFOW) + 256 * sizeof(WCHAR)];
490 void* user;
491 PSYM_ENUMERATESYMBOLS_CALLBACKW callback;
492 };
493
494 static BOOL CALLBACK enum_types_AtoW(PSYMBOL_INFO si, ULONG addr, PVOID _et)
495 {
496 struct enum_types_AtoW* et = _et;
497 SYMBOL_INFOW* siW = (SYMBOL_INFOW*)et->buffer;
498
499 copy_symbolW(siW, si);
500 return et->callback(siW, addr, et->user);
501 }
502
503 /******************************************************************
504 * SymEnumTypesW (DBGHELP.@)
505 *
506 */
507 BOOL WINAPI SymEnumTypesW(HANDLE hProcess, ULONG64 BaseOfDll,
508 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
509 PVOID UserContext)
510 {
511 struct enum_types_AtoW et;
512
513 et.callback = EnumSymbolsCallback;
514 et.user = UserContext;
515
516 return SymEnumTypes(hProcess, BaseOfDll, enum_types_AtoW, &et);
517 }
518
519 /******************************************************************
520 * symt_get_info
521 *
522 * Retrieves information about a symt (either symbol or type)
523 */
524 BOOL symt_get_info(struct module* module, const struct symt* type,
525 IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo)
526 {
527 unsigned len;
528
529 if (!type) return FALSE;
530
531 /* helper to typecast pInfo to its expected type (_t) */
532 #define X(_t) (*((_t*)pInfo))
533
534 switch (req)
535 {
536 case TI_FINDCHILDREN:
537 {
538 const struct vector* v;
539 struct symt** pt;
540 unsigned i;
541 TI_FINDCHILDREN_PARAMS* tifp = pInfo;
542
543 switch (type->tag)
544 {
545 case SymTagUDT: v = &((const struct symt_udt*)type)->vchildren; break;
546 case SymTagEnum: v = &((const struct symt_enum*)type)->vchildren; break;
547 case SymTagFunctionType: v = &((const struct symt_function_signature*)type)->vchildren; break;
548 case SymTagFunction: v = &((const struct symt_function*)type)->vchildren; break;
549 default:
550 FIXME("Unsupported sym-tag %s for find-children\n",
551 symt_get_tag_str(type->tag));
552 return FALSE;
553 }
554 for (i = 0; i < tifp->Count; i++)
555 {
556 if (!(pt = vector_at(v, tifp->Start + i))) return FALSE;
557 tifp->ChildId[i] = symt_ptr2index(module, *pt);
558 }
559 }
560 break;
561
562 case TI_GET_ADDRESS:
563 return symt_get_address(type, (ULONG64*)pInfo);
564
565 case TI_GET_BASETYPE:
566 switch (type->tag)
567 {
568 case SymTagBaseType:
569 X(DWORD) = ((const struct symt_basic*)type)->bt;
570 break;
571 case SymTagEnum:
572 X(DWORD) = btInt;
573 break;
574 default:
575 return FALSE;
576 }
577 break;
578
579 case TI_GET_BITPOSITION:
580 if (type->tag == SymTagData &&
581 ((const struct symt_data*)type)->kind == DataIsMember &&
582 ((const struct symt_data*)type)->u.member.length != 0)
583 X(DWORD) = ((const struct symt_data*)type)->u.member.offset & 7;
584 else return FALSE;
585 break;
586
587 case TI_GET_CHILDRENCOUNT:
588 switch (type->tag)
589 {
590 case SymTagUDT:
591 X(DWORD) = vector_length(&((const struct symt_udt*)type)->vchildren);
592 break;
593 case SymTagEnum:
594 X(DWORD) = vector_length(&((const struct symt_enum*)type)->vchildren);
595 break;
596 case SymTagFunctionType:
597 X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
598 break;
599 case SymTagFunction:
600 X(DWORD) = vector_length(&((const struct symt_function*)type)->vchildren);
601 break;
602 case SymTagPointerType: /* MS does it that way */
603 case SymTagArrayType: /* MS does it that way */
604 case SymTagThunk: /* MS does it that way */
605 X(DWORD) = 0;
606 break;
607 default:
608 FIXME("Unsupported sym-tag %s for get-children-count\n",
609 symt_get_tag_str(type->tag));
610 /* fall through */
611 case SymTagData:
612 case SymTagPublicSymbol:
613 case SymTagBaseType:
614 return FALSE;
615 }
616 break;
617
618 case TI_GET_COUNT:
619 switch (type->tag)
620 {
621 case SymTagArrayType:
622 X(DWORD) = symt_array_count(module, (const struct symt_array*)type);
623 break;
624 case SymTagFunctionType:
625 /* this seems to be wrong for (future) C++ methods, where 'this' parameter
626 * should be included in this value (and not in GET_CHILDREN_COUNT)
627 */
628 X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
629 break;
630 default: return FALSE;
631 }
632 break;
633
634 case TI_GET_DATAKIND:
635 if (type->tag != SymTagData) return FALSE;
636 X(DWORD) = ((const struct symt_data*)type)->kind;
637 break;
638
639 case TI_GET_LENGTH:
640 switch (type->tag)
641 {
642 case SymTagBaseType:
643 X(DWORD64) = ((const struct symt_basic*)type)->size;
644 break;
645 case SymTagFunction:
646 X(DWORD64) = ((const struct symt_function*)type)->size;
647 break;
648 case SymTagPointerType:
649 X(DWORD64) = ((const struct symt_pointer*)type)->size;
650 break;
651 case SymTagUDT:
652 X(DWORD64) = ((const struct symt_udt*)type)->size;
653 break;
654 case SymTagEnum:
655 X(DWORD64) = sizeof(int); /* FIXME: should be size of base-type of enum !!! */
656 break;
657 case SymTagData:
658 if (((const struct symt_data*)type)->kind != DataIsMember ||
659 !((const struct symt_data*)type)->u.member.length)
660 return FALSE;
661 X(DWORD64) = ((const struct symt_data*)type)->u.member.length;
662 break;
663 case SymTagArrayType:
664 if (!symt_get_info(module, ((const struct symt_array*)type)->base_type,
665 TI_GET_LENGTH, pInfo))
666 return FALSE;
667 X(DWORD64) *= symt_array_count(module, (const struct symt_array*)type);
668 break;
669 case SymTagPublicSymbol:
670 X(DWORD64) = ((const struct symt_public*)type)->size;
671 break;
672 case SymTagTypedef:
673 return symt_get_info(module, ((const struct symt_typedef*)type)->type, TI_GET_LENGTH, pInfo);
674 case SymTagThunk:
675 X(DWORD64) = ((const struct symt_thunk*)type)->size;
676 break;
677 case SymTagLabel:
678 X(DWORD64) = 0;
679 break;
680 default:
681 FIXME("Unsupported sym-tag %s for get-length\n",
682 symt_get_tag_str(type->tag));
683 /* fall through */
684 case SymTagFunctionType:
685 return 0;
686 }
687 break;
688
689 case TI_GET_LEXICALPARENT:
690 switch (type->tag)
691 {
692 case SymTagBlock:
693 X(DWORD) = symt_ptr2index(module, ((const struct symt_block*)type)->container);
694 break;
695 case SymTagData:
696 X(DWORD) = symt_ptr2index(module, ((const struct symt_data*)type)->container);
697 break;
698 case SymTagFunction:
699 X(DWORD) = symt_ptr2index(module, ((const struct symt_function*)type)->container);
700 break;
701 case SymTagThunk:
702 X(DWORD) = symt_ptr2index(module, ((const struct symt_thunk*)type)->container);
703 break;
704 case SymTagFunctionArgType:
705 X(DWORD) = symt_ptr2index(module, ((const struct symt_function_arg_type*)type)->container);
706 break;
707 default:
708 FIXME("Unsupported sym-tag %s for get-lexical-parent\n",
709 symt_get_tag_str(type->tag));
710 return FALSE;
711 }
712 break;
713
714 case TI_GET_NESTED:
715 switch (type->tag)
716 {
717 case SymTagUDT:
718 case SymTagEnum:
719 X(DWORD) = 0;
720 break;
721 default:
722 return FALSE;
723 }
724 break;
725
726 case TI_GET_OFFSET:
727 switch (type->tag)
728 {
729 case SymTagData:
730 switch (((const struct symt_data*)type)->kind)
731 {
732 case DataIsParam:
733 case DataIsLocal:
734 X(ULONG) = ((const struct symt_data*)type)->u.var.offset;
735 break;
736 case DataIsMember:
737 X(ULONG) = ((const struct symt_data*)type)->u.member.offset >> 3;
738 break;
739 default:
740 FIXME("Unknown kind (%u) for get-offset\n",
741 ((const struct symt_data*)type)->kind);
742 return FALSE;
743 }
744 break;
745 default:
746 FIXME("Unsupported sym-tag %s for get-offset\n",
747 symt_get_tag_str(type->tag));
748 return FALSE;
749 }
750 break;
751
752 case TI_GET_SYMNAME:
753 {
754 const char* name = symt_get_name(type);
755 if (!name) return FALSE;
756 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
757 X(WCHAR*) = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
758 if (!X(WCHAR*)) return FALSE;
759 MultiByteToWideChar(CP_ACP, 0, name, -1, X(WCHAR*), len);
760 }
761 break;
762
763 case TI_GET_SYMTAG:
764 X(DWORD) = type->tag;
765 break;
766
767 case TI_GET_TYPE:
768 case TI_GET_TYPEID:
769 switch (type->tag)
770 {
771 /* hierarchical => hierarchical */
772 case SymTagArrayType:
773 X(DWORD) = symt_ptr2index(module, ((const struct symt_array*)type)->base_type);
774 break;
775 case SymTagPointerType:
776 X(DWORD) = symt_ptr2index(module, ((const struct symt_pointer*)type)->pointsto);
777 break;
778 case SymTagFunctionType:
779 X(DWORD) = symt_ptr2index(module, ((const struct symt_function_signature*)type)->rettype);
780 break;
781 case SymTagTypedef:
782 X(DWORD) = symt_ptr2index(module, ((const struct symt_typedef*)type)->type);
783 break;
784 /* lexical => hierarchical */
785 case SymTagData:
786 X(DWORD) = symt_ptr2index(module, ((const struct symt_data*)type)->type);
787 break;
788 case SymTagFunction:
789 X(DWORD) = symt_ptr2index(module, ((const struct symt_function*)type)->type);
790 break;
791 case SymTagEnum:
792 X(DWORD) = symt_ptr2index(module, ((const struct symt_enum*)type)->base_type);
793 break;
794 case SymTagFunctionArgType:
795 X(DWORD) = symt_ptr2index(module, ((const struct symt_function_arg_type*)type)->arg_type);
796 break;
797 default:
798 FIXME("Unsupported sym-tag %s for get-type\n",
799 symt_get_tag_str(type->tag));
800 /* fall through */
801 case SymTagPublicSymbol:
802 case SymTagThunk:
803 case SymTagLabel:
804 return FALSE;
805 }
806 break;
807
808 case TI_GET_UDTKIND:
809 if (type->tag != SymTagUDT) return FALSE;
810 X(DWORD) = ((const struct symt_udt*)type)->kind;
811 break;
812
813 case TI_GET_VALUE:
814 if (type->tag != SymTagData) return FALSE;
815 switch (((const struct symt_data*)type)->kind)
816 {
817 case DataIsConstant: X(VARIANT) = ((const struct symt_data*)type)->u.value; break;
818 case DataIsLocal:
819 case DataIsParam:
820 {
821 struct location loc = ((const struct symt_data*)type)->u.var;
822 unsigned i;
823 struct module_format* modfmt;
824
825 if (loc.kind < loc_user) return FALSE;
826 for (i = 0; i < DFI_LAST; i++)
827 {
828 modfmt = module->format_info[i];
829 if (modfmt && modfmt->loc_compute)
830 {
831 modfmt->loc_compute(module->process, modfmt,
832 (const struct symt_function*)((const struct symt_data*)type)->container, &loc);
833 break;
834 }
835 }
836 if (loc.kind != loc_absolute) return FALSE;
837 X(VARIANT).n1.n2.vt = VT_UI4; /* FIXME */
838 X(VARIANT).n1.n2.n3.uiVal = loc.offset;
839 }
840 break;
841 default: return FALSE;
842 }
843 break;
844
845 case TI_GET_CALLING_CONVENTION:
846 if (type->tag != SymTagFunctionType) return FALSE;
847 if (((const struct symt_function_signature*)type)->call_conv == -1)
848 {
849 FIXME("No support for calling convention for this signature\n");
850 X(DWORD) = CV_CALL_FAR_C; /* FIXME */
851 }
852 else X(DWORD) = ((const struct symt_function_signature*)type)->call_conv;
853 break;
854 case TI_GET_ARRAYINDEXTYPEID:
855 if (type->tag != SymTagArrayType) return FALSE;
856 X(DWORD) = symt_ptr2index(module, ((const struct symt_array*)type)->index_type);
857 break;
858
859 case TI_GET_CLASSPARENTID:
860 /* FIXME: we don't support properly C++ for now, pretend this symbol doesn't
861 * belong to a parent class
862 */
863 return FALSE;
864
865 #undef X
866
867 case TI_GET_ADDRESSOFFSET:
868 case TI_GET_SYMINDEX:
869 case TI_GET_THISADJUST:
870 case TI_GET_VIRTUALBASECLASS:
871 case TI_GET_VIRTUALBASEPOINTEROFFSET:
872 case TI_GET_VIRTUALTABLESHAPEID:
873 case TI_IS_EQUIV_TO:
874 FIXME("Unsupported GetInfo request (%u)\n", req);
875 return FALSE;
876 default:
877 FIXME("Unknown GetInfo request (%u)\n", req);
878 return FALSE;
879 }
880
881 return TRUE;
882 }
883
884 /******************************************************************
885 * SymGetTypeInfo (DBGHELP.@)
886 *
887 */
888 BOOL WINAPI SymGetTypeInfo(HANDLE hProcess, DWORD64 ModBase,
889 ULONG TypeId, IMAGEHLP_SYMBOL_TYPE_INFO GetType,
890 PVOID pInfo)
891 {
892 struct module_pair pair;
893
894 pair.pcs = process_find_by_handle(hProcess);
895 if (!pair.pcs) return FALSE;
896
897 pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
898 if (!module_get_debug(&pair))
899 {
900 FIXME("Someone didn't properly set ModBase (%s)\n", wine_dbgstr_longlong(ModBase));
901 return FALSE;
902 }
903
904 return symt_get_info(pair.effective, symt_index2ptr(pair.effective, TypeId), GetType, pInfo);
905 }
906
907 /******************************************************************
908 * SymGetTypeFromName (DBGHELP.@)
909 *
910 */
911 BOOL WINAPI SymGetTypeFromName(HANDLE hProcess, ULONG64 BaseOfDll,
912 PCSTR Name, PSYMBOL_INFO Symbol)
913 {
914 struct process* pcs = process_find_by_handle(hProcess);
915 struct module_pair pair;
916 struct symt* type;
917
918 if (!pcs) return FALSE;
919 pair.requested = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
920 if (!module_get_debug(&pair)) return FALSE;
921 type = symt_find_type_by_name(pair.effective, SymTagNull, Name);
922 if (!type) return FALSE;
923 Symbol->TypeIndex = symt_ptr2index(pair.effective, type);
924
925 return TRUE;
926 }