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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 ((struct symt_data
*)sym
)->hash_elt
.name
;
84 case SymTagFunction
: return ((struct symt_function
*)sym
)->hash_elt
.name
;
85 case SymTagPublicSymbol
: return ((struct symt_public
*)sym
)->hash_elt
.name
;
86 case SymTagBaseType
: return ((struct symt_basic
*)sym
)->hash_elt
.name
;
87 case SymTagLabel
: return ((struct symt_function_point
*)sym
)->name
;
89 case SymTagEnum
: return ((struct symt_enum
*)sym
)->name
;
90 case SymTagTypedef
: return ((struct symt_typedef
*)sym
)->hash_elt
.name
;
91 case SymTagUDT
: return ((struct symt_udt
*)sym
)->hash_elt
.name
;
93 FIXME("Unsupported sym-tag %s\n", symt_get_tag_str(sym
->tag
));
96 case SymTagPointerType
:
97 case SymTagFunctionType
:
102 static struct symt
* symt_find_type_by_name(struct module
* module
,
103 enum SymTagEnum sym_tag
,
104 const char* typename
)
107 struct symt_ht
* type
;
108 struct hash_table_iter hti
;
113 hash_table_iter_init(&module
->ht_types
, &hti
, typename
);
114 while ((ptr
= hash_table_iter_up(&hti
)))
116 type
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
118 if ((sym_tag
== SymTagNull
|| type
->symt
.tag
== sym_tag
) &&
119 type
->hash_elt
.name
&& !strcmp(type
->hash_elt
.name
, typename
))
122 SetLastError(ERROR_INVALID_NAME
); /* FIXME ?? */
126 struct symt_basic
* symt_new_basic(struct module
* module
, enum BasicType bt
,
127 const char* typename
, unsigned size
)
129 struct symt_basic
* sym
;
133 sym
= (struct symt_basic
*)symt_find_type_by_name(module
, SymTagBaseType
,
135 if (sym
&& sym
->bt
== bt
&& sym
->size
== size
)
138 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
140 sym
->symt
.tag
= SymTagBaseType
;
143 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, typename
);
144 hash_table_add(&module
->ht_types
, &sym
->hash_elt
);
145 } else sym
->hash_elt
.name
= NULL
;
152 struct symt_udt
* symt_new_udt(struct module
* module
, const char* typename
,
153 unsigned size
, enum UdtKind kind
)
155 struct symt_udt
* sym
;
157 TRACE_(dbghelp_symt
)("Adding udt %s:%s\n", module
->module
.ModuleName
, typename
);
158 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
160 sym
->symt
.tag
= SymTagUDT
;
165 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, typename
);
166 hash_table_add(&module
->ht_types
, &sym
->hash_elt
);
167 } else sym
->hash_elt
.name
= NULL
;
168 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
173 BOOL
symt_set_udt_size(struct module
* module
, struct symt_udt
* udt
, unsigned size
)
175 assert(udt
->symt
.tag
== SymTagUDT
);
176 if (vector_length(&udt
->vchildren
) != 0)
178 if (udt
->size
!= size
)
179 FIXME_(dbghelp_symt
)("Changing size for %s from %u to %u\n",
180 udt
->hash_elt
.name
, udt
->size
, size
);
187 /******************************************************************
188 * symt_add_udt_element
190 * add an element to a udt (struct, class, union)
191 * the size & offset parameters are expressed in bits (not bytes) so that
192 * we can mix in the single call bytes aligned elements (regular fields) and
193 * the others (bit fields)
195 BOOL
symt_add_udt_element(struct module
* module
, struct symt_udt
* udt_type
,
196 const char* name
, struct symt
* elt_type
,
197 unsigned offset
, unsigned size
)
202 assert(udt_type
->symt
.tag
== SymTagUDT
);
204 TRACE_(dbghelp_symt
)("Adding %s to UDT %s\n", name
, udt_type
->hash_elt
.name
);
206 while ((p
= vector_iter_up(&udt_type
->vchildren
, p
)))
208 m
= (struct symt_data
*)*p
;
210 assert(m
->symt
.tag
== SymTagData
);
211 if (m
->hash_elt
.name
[0] == name
[0] && strcmp(m
->hash_elt
.name
, name
) == 0)
215 if ((m
= pool_alloc(&module
->pool
, sizeof(*m
))) == NULL
) return FALSE
;
216 memset(m
, 0, sizeof(*m
));
217 m
->symt
.tag
= SymTagData
;
218 m
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
219 m
->hash_elt
.next
= NULL
;
221 m
->kind
= DataIsMember
;
222 m
->container
= &udt_type
->symt
;
224 m
->u
.s
.offset
= offset
;
225 m
->u
.s
.length
= ((offset
& 7) || (size
& 7)) ? size
: 0;
227 p
= vector_add(&udt_type
->vchildren
, &module
->pool
);
233 struct symt_enum
* symt_new_enum(struct module
* module
, const char* typename
)
235 struct symt_enum
* sym
;
237 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
239 sym
->symt
.tag
= SymTagEnum
;
240 sym
->name
= pool_strdup(&module
->pool
, typename
);
241 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
246 BOOL
symt_add_enum_element(struct module
* module
, struct symt_enum
* enum_type
,
247 const char* name
, int value
)
252 assert(enum_type
->symt
.tag
== SymTagEnum
);
253 e
= pool_alloc(&module
->pool
, sizeof(*e
));
254 if (e
== NULL
) return FALSE
;
256 e
->symt
.tag
= SymTagData
;
257 e
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
258 e
->hash_elt
.next
= NULL
;
259 e
->kind
= DataIsConstant
;
260 e
->container
= &enum_type
->symt
;
261 /* CV defines the underlying type for the enumeration */
262 e
->type
= &symt_new_basic(module
, btInt
, "int", 4)->symt
;
263 e
->u
.value
.n1
.n2
.vt
= VT_I4
;
264 e
->u
.value
.n1
.n2
.n3
.lVal
= value
;
266 p
= vector_add(&enum_type
->vchildren
, &module
->pool
);
267 if (!p
) return FALSE
; /* FIXME we leak e */
273 struct symt_array
* symt_new_array(struct module
* module
, int min
, int max
,
276 struct symt_array
* sym
;
278 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
280 sym
->symt
.tag
= SymTagArrayType
;
283 sym
->basetype
= base
;
288 struct symt_function_signature
* symt_new_function_signature(struct module
* module
,
289 struct symt
* ret_type
)
291 struct symt_function_signature
* sym
;
293 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
295 sym
->symt
.tag
= SymTagFunctionType
;
296 sym
->rettype
= ret_type
;
297 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 4);
302 BOOL
symt_add_function_signature_parameter(struct module
* module
,
303 struct symt_function_signature
* sig_type
,
308 assert(sig_type
->symt
.tag
== SymTagFunctionType
);
309 p
= vector_add(&sig_type
->vchildren
, &module
->pool
);
310 if (!p
) return FALSE
; /* FIXME we leak e */
316 struct symt_pointer
* symt_new_pointer(struct module
* module
, struct symt
* ref_type
)
318 struct symt_pointer
* sym
;
320 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
322 sym
->symt
.tag
= SymTagPointerType
;
323 sym
->pointsto
= ref_type
;
328 struct symt_typedef
* symt_new_typedef(struct module
* module
, struct symt
* ref
,
331 struct symt_typedef
* sym
;
333 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
335 sym
->symt
.tag
= SymTagTypedef
;
337 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
338 hash_table_add(&module
->ht_types
, &sym
->hash_elt
);
343 /******************************************************************
344 * SymEnumTypes (DBGHELP.@)
347 BOOL WINAPI
SymEnumTypes(HANDLE hProcess
, unsigned long BaseOfDll
,
348 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
352 struct module
* module
;
353 struct symt_ht
* type
;
355 char buffer
[sizeof(SYMBOL_INFO
) + 256];
356 SYMBOL_INFO
* sym_info
= (SYMBOL_INFO
*)buffer
;
357 struct hash_table_iter hti
;
360 TRACE("(%p %08lx %p %p)\n",
361 hProcess
, BaseOfDll
, EnumSymbolsCallback
, UserContext
);
363 pcs
= process_find_by_handle(hProcess
);
364 if (!pcs
) return FALSE
;
365 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
366 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
368 sym_info
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
369 sym_info
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
371 hash_table_iter_init(&module
->ht_types
, &hti
, NULL
);
372 while ((ptr
= hash_table_iter_up(&hti
)))
374 type
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
375 sym_info
->TypeIndex
= (DWORD
)type
;
376 sym_info
->info
= 0; /* FIXME */
377 symt_get_info(&type
->symt
, TI_GET_LENGTH
, &sym_info
->Size
);
378 sym_info
->ModBase
= module
->module
.BaseOfImage
;
379 sym_info
->Flags
= 0; /* FIXME */
380 sym_info
->Value
= 0; /* FIXME */
381 sym_info
->Address
= 0; /* FIXME */
382 sym_info
->Register
= 0; /* FIXME */
383 sym_info
->Scope
= 0; /* FIXME */
384 sym_info
->Tag
= type
->symt
.tag
;
385 tmp
= symt_get_name(&type
->symt
);
386 sym_info
->NameLen
= strlen(tmp
) + 1;
387 strncpy(sym_info
->Name
, tmp
, min(sym_info
->NameLen
, sym_info
->MaxNameLen
));
388 sym_info
->Name
[sym_info
->MaxNameLen
- 1] = '\0';
389 if (!EnumSymbolsCallback(sym_info
, sym_info
->Size
, UserContext
)) break;
394 /******************************************************************
397 * Retrieves inforamtion about a symt (either symbol or type)
399 BOOL
symt_get_info(const struct symt
* type
, IMAGEHLP_SYMBOL_TYPE_INFO req
,
404 if (!type
) return FALSE
;
406 /* helper to typecast pInfo to its expected type (_t) */
407 #define X(_t) (*((_t*)pInfo))
411 case TI_FINDCHILDREN
:
413 const struct vector
* v
;
416 TI_FINDCHILDREN_PARAMS
* tifp
= pInfo
;
420 case SymTagUDT
: v
= &((struct symt_udt
*)type
)->vchildren
; break;
421 case SymTagEnum
: v
= &((struct symt_enum
*)type
)->vchildren
; break;
422 case SymTagFunctionType
: v
= &((struct symt_function_signature
*)type
)->vchildren
; break;
423 case SymTagFunction
: v
= &((struct symt_function
*)type
)->vchildren
; break;
425 FIXME("Unsupported sym-tag %s for find-children\n",
426 symt_get_tag_str(type
->tag
));
429 for (i
= 0; i
< tifp
->Count
; i
++)
431 if (!(pt
= vector_at(v
, tifp
->Start
+ i
))) return FALSE
;
432 tifp
->ChildId
[i
] = (DWORD
)*pt
;
441 switch (((struct symt_data
*)type
)->kind
)
444 case DataIsFileStatic
:
445 X(DWORD
) = ((struct symt_data
*)type
)->u
.address
;
447 default: return FALSE
;
451 X(DWORD
) = ((struct symt_function
*)type
)->addr
;
453 case SymTagPublicSymbol
:
454 X(DWORD
) = ((struct symt_public
*)type
)->address
;
456 case SymTagFuncDebugStart
:
457 case SymTagFuncDebugEnd
:
459 X(DWORD
) = ((struct symt_function_point
*)type
)->parent
->addr
+
460 ((struct symt_function_point
*)type
)->offset
;
463 FIXME("Unsupported sym-tag %s for get-address\n",
464 symt_get_tag_str(type
->tag
));
469 case TI_GET_BASETYPE
:
473 X(DWORD
) = ((struct symt_basic
*)type
)->bt
;
483 case TI_GET_BITPOSITION
:
484 if (type
->tag
!= SymTagData
||
485 ((struct symt_data
*)type
)->kind
!= DataIsMember
||
486 ((struct symt_data
*)type
)->u
.s
.length
== 0)
488 X(DWORD
) = ((struct symt_data
*)type
)->u
.s
.offset
& 7;
491 case TI_GET_CHILDRENCOUNT
:
495 X(DWORD
) = vector_length(&((struct symt_udt
*)type
)->vchildren
);
498 X(DWORD
) = vector_length(&((struct symt_enum
*)type
)->vchildren
);
500 case SymTagFunctionType
:
501 X(DWORD
) = vector_length(&((struct symt_function_signature
*)type
)->vchildren
);
504 X(DWORD
) = vector_length(&((struct symt_function
*)type
)->vchildren
);
506 case SymTagPointerType
: /* MS does it that way */
507 case SymTagArrayType
: /* MS does it that way */
511 FIXME("Unsupported sym-tag %s for get-children-count\n",
512 symt_get_tag_str(type
->tag
));
515 case SymTagPublicSymbol
:
522 /* it seems that FunctionType also react to GET_COUNT (same value as
523 * GET_CHILDREN_COUNT ?, except for C++ methods, where it seems to
524 * also include 'this' (GET_CHILDREN_COUNT+1)
526 if (type
->tag
!= SymTagArrayType
) return FALSE
;
527 X(DWORD
) = ((struct symt_array
*)type
)->end
-
528 ((struct symt_array
*)type
)->start
;
531 case TI_GET_DATAKIND
:
532 if (type
->tag
!= SymTagData
) return FALSE
;
533 X(DWORD
) = ((struct symt_data
*)type
)->kind
;
540 X(DWORD
) = ((struct symt_basic
*)type
)->size
;
543 X(DWORD
) = ((struct symt_function
*)type
)->size
;
545 case SymTagPointerType
:
546 X(DWORD
) = sizeof(void*);
549 X(DWORD
) = ((struct symt_udt
*)type
)->size
;
552 X(DWORD
) = sizeof(int); /* FIXME: should be size of base-type of enum !!! */
555 if (((struct symt_data
*)type
)->kind
!= DataIsMember
||
556 !((struct symt_data
*)type
)->u
.s
.length
)
558 X(DWORD
) = ((struct symt_data
*)type
)->u
.s
.length
;
560 case SymTagArrayType
:
561 if (!symt_get_info(((struct symt_array
*)type
)->basetype
,
562 TI_GET_LENGTH
, pInfo
))
564 X(DWORD
) *= ((struct symt_array
*)type
)->end
-
565 ((struct symt_array
*)type
)->start
;
567 case SymTagPublicSymbol
:
568 X(DWORD
) = ((struct symt_public
*)type
)->size
;
571 return symt_get_info(((struct symt_typedef
*)type
)->type
, TI_GET_LENGTH
, pInfo
);
574 FIXME("Unsupported sym-tag %s for get-length\n",
575 symt_get_tag_str(type
->tag
));
580 case TI_GET_LEXICALPARENT
:
584 X(DWORD
) = (DWORD
)((struct symt_block
*)type
)->container
;
587 X(DWORD
) = (DWORD
)((struct symt_data
*)type
)->container
;
590 FIXME("Unsupported sym-tag %s for get-lexical-parent\n",
591 symt_get_tag_str(type
->tag
));
612 switch (((struct symt_data
*)type
)->kind
)
617 X(ULONG
) = ((struct symt_data
*)type
)->u
.s
.offset
>> 3;
620 FIXME("Unknown kind (%u) for get-offset\n",
621 ((struct symt_data
*)type
)->kind
);
626 FIXME("Unsupported sym-tag %s for get-offset\n",
627 symt_get_tag_str(type
->tag
));
634 const char* name
= symt_get_name(type
);
635 if (!name
) return FALSE
;
636 len
= MultiByteToWideChar(CP_ACP
, 0, name
, -1, NULL
, 0);
637 X(WCHAR
*) = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
638 if (!X(WCHAR
*)) return FALSE
;
639 MultiByteToWideChar(CP_ACP
, 0, name
, -1, X(WCHAR
*), len
);
644 X(DWORD
) = type
->tag
;
651 /* hierarchical => hierarchical */
652 case SymTagArrayType
:
653 X(DWORD
) = (DWORD
)((struct symt_array
*)type
)->basetype
;
655 case SymTagPointerType
:
656 X(DWORD
) = (DWORD
)((struct symt_pointer
*)type
)->pointsto
;
658 case SymTagFunctionType
:
659 X(DWORD
) = (DWORD
)((struct symt_function_signature
*)type
)->rettype
;
662 X(DWORD
) = (DWORD
)((struct symt_typedef
*)type
)->type
;
664 /* lexical => hierarchical */
666 X(DWORD
) = (DWORD
)((struct symt_data
*)type
)->type
;
669 X(DWORD
) = (DWORD
)((struct symt_function
*)type
)->type
;
671 /* FIXME: should also work for enums and FunctionArgType */
673 FIXME("Unsupported sym-tag %s for get-type\n",
674 symt_get_tag_str(type
->tag
));
680 if (type
->tag
!= SymTagUDT
) return FALSE
;
681 X(DWORD
) = ((struct symt_udt
*)type
)->kind
;
685 if (type
->tag
!= SymTagData
|| ((struct symt_data
*)type
)->kind
!= DataIsConstant
)
687 X(VARIANT
) = ((struct symt_data
*)type
)->u
.value
;
692 case TI_GET_ADDRESSOFFSET
:
693 case TI_GET_ARRAYINDEXTYPEID
:
694 case TI_GET_CALLING_CONVENTION
:
695 case TI_GET_CLASSPARENTID
:
696 case TI_GET_SYMINDEX
:
697 case TI_GET_THISADJUST
:
698 case TI_GET_VIRTUALBASECLASS
:
699 case TI_GET_VIRTUALBASEPOINTEROFFSET
:
700 case TI_GET_VIRTUALTABLESHAPEID
:
702 FIXME("Unsupported GetInfo request (%u)\n", req
);
709 /******************************************************************
710 * SymGetTypeInfo (DBGHELP.@)
713 BOOL WINAPI
SymGetTypeInfo(HANDLE hProcess
, unsigned long ModBase
,
714 ULONG TypeId
, IMAGEHLP_SYMBOL_TYPE_INFO GetType
,
717 struct process
* pcs
= process_find_by_handle(hProcess
);
719 if (!pcs
) return FALSE
;
721 struct module
* module
;
722 module
= module_find_by_addr(pcs
, ModBase
, DMT_UNKNOWN
);
723 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
726 return symt_get_info((struct symt
*)TypeId
, GetType
, pInfo
);
729 /******************************************************************
730 * SymGetTypeFromName (DBGHELP.@)
733 BOOL WINAPI
SymGetTypeFromName(HANDLE hProcess
, unsigned long BaseOfDll
,
734 LPSTR Name
, PSYMBOL_INFO Symbol
)
736 struct process
* pcs
= process_find_by_handle(hProcess
);
737 struct module
* module
;
740 if (!pcs
) return FALSE
;
741 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
742 if (!module
) return FALSE
;
743 type
= symt_find_type_by_name(module
, SymTagNull
, Name
);
744 if (!type
) return FALSE
;
745 Symbol
->TypeIndex
= (DWORD
)type
;