2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, 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
22 #define NONAMELESSUNION
27 #include <sys/types.h>
30 #include "wine/debug.h"
31 #include "dbghelp_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
35 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt
);
37 extern char * CDECL
__unDName(char *buffer
, const char *mangled
, int len
,
38 void * (CDECL
*pfn_alloc
)(size_t), void (CDECL
*pfn_free
)(void *), unsigned short flags
);
40 static inline int cmp_addr(ULONG64 a1
, ULONG64 a2
)
42 if (a1
> a2
) return 1;
43 if (a1
< a2
) return -1;
47 static inline int cmp_sorttab_addr(struct module
* module
, int idx
, ULONG64 addr
)
50 symt_get_address(&module
->addr_sorttab
[idx
]->symt
, &ref
);
51 return cmp_addr(ref
, addr
);
54 int __cdecl
symt_cmp_addr(const void* p1
, const void* p2
)
56 const struct symt
* sym1
= *(const struct symt
* const *)p1
;
57 const struct symt
* sym2
= *(const struct symt
* const *)p2
;
60 symt_get_address(sym1
, &a1
);
61 symt_get_address(sym2
, &a2
);
62 return cmp_addr(a1
, a2
);
65 #define BASE_CUSTOM_SYMT 0x80000000
67 /* dbghelp exposes the internal symbols/types with DWORD indexes.
68 * - custom symbols are always stored with index starting at BASE_CUSTOM_SYMT
69 * - for all the others (non custom) symbols:
70 * + on 32bit machine, index is set to the actual adress of symt
71 * + on 64bit machine, we have a dedicated array to store symt exposed to caller
72 * index is the index in this array of the symbol
74 DWORD
symt_ptr2index(struct module
* module
, const struct symt
* sym
)
76 struct vector
* vector
;
78 const struct symt
** c
;
82 if (sym
->tag
== SymTagCustom
)
84 vector
= &module
->vcustom_symt
;
85 offset
= BASE_CUSTOM_SYMT
;
90 vector
= &module
->vsymt
;
96 len
= vector_length(vector
);
97 /* FIXME: this is inefficient */
98 for (i
= 0; i
< len
; i
++)
100 if (*(struct symt
**)vector_at(vector
, i
) == sym
)
104 c
= vector_add(vector
, &module
->pool
);
109 struct symt
* symt_index2ptr(struct module
* module
, DWORD id
)
111 struct vector
* vector
;
112 if (id
>= BASE_CUSTOM_SYMT
)
114 id
-= BASE_CUSTOM_SYMT
;
115 vector
= &module
->vcustom_symt
;
120 if (!id
--) return NULL
;
121 vector
= &module
->vsymt
;
123 return (struct symt
*)id
;
126 return (id
>= vector_length(vector
)) ? NULL
: *(struct symt
**)vector_at(vector
, id
);
129 static BOOL
symt_grow_sorttab(struct module
* module
, unsigned sz
)
131 struct symt_ht
** new;
134 if (sz
<= module
->sorttab_size
) return TRUE
;
135 if (module
->addr_sorttab
)
137 size
= module
->sorttab_size
* 2;
138 new = HeapReAlloc(GetProcessHeap(), 0, module
->addr_sorttab
,
139 size
* sizeof(struct symt_ht
*));
144 new = HeapAlloc(GetProcessHeap(), 0, size
* sizeof(struct symt_ht
*));
146 if (!new) return FALSE
;
147 module
->sorttab_size
= size
;
148 module
->addr_sorttab
= new;
152 static void symt_add_module_ht(struct module
* module
, struct symt_ht
* ht
)
156 hash_table_add(&module
->ht_symbols
, &ht
->hash_elt
);
157 /* Don't store in sorttab a symbol without address, they are of
158 * no use here (e.g. constant values)
160 if (symt_get_address(&ht
->symt
, &addr
) &&
161 symt_grow_sorttab(module
, module
->num_symbols
+ 1))
163 module
->addr_sorttab
[module
->num_symbols
++] = ht
;
164 module
->sortlist_valid
= FALSE
;
168 static WCHAR
* file_regex(const char* srcfile
)
173 if (!srcfile
|| !*srcfile
)
175 if (!(p
= mask
= HeapAlloc(GetProcessHeap(), 0, 3 * sizeof(WCHAR
)))) return NULL
;
181 DWORD sz
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
184 /* FIXME: we use here the largest conversion for every char... could be optimized */
185 p
= mask
= HeapAlloc(GetProcessHeap(), 0, (5 * strlen(srcfile
) + 1 + sz
) * sizeof(WCHAR
));
186 if (!mask
) return NULL
;
187 srcfileW
= mask
+ 5 * strlen(srcfile
) + 1;
188 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, srcfileW
, sz
);
216 struct symt_module
* symt_new_module(struct module
* module
)
218 struct symt_module
* sym
;
220 TRACE_(dbghelp_symt
)("Adding toplevel exe symbol %s\n", debugstr_w(module
->modulename
));
221 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
223 sym
->symt
.tag
= SymTagExe
;
224 sym
->module
= module
;
225 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
230 struct symt_compiland
* symt_new_compiland(struct module
* module
,
231 ULONG_PTR address
, unsigned src_idx
)
233 struct symt_compiland
* sym
;
234 struct symt_compiland
** p
;
236 TRACE_(dbghelp_symt
)("Adding compiland symbol %s:%s\n",
237 debugstr_w(module
->modulename
), source_get(module
, src_idx
));
238 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
240 sym
->symt
.tag
= SymTagCompiland
;
241 sym
->container
= module
->top
;
242 sym
->address
= address
;
243 sym
->source
= src_idx
;
244 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 32);
246 p
= vector_add(&module
->top
->vchildren
, &module
->pool
);
252 struct symt_public
* symt_new_public(struct module
* module
,
253 struct symt_compiland
* compiland
,
256 ULONG_PTR address
, unsigned size
)
258 struct symt_public
* sym
;
261 TRACE_(dbghelp_symt
)("Adding public symbol %s:%s @%lx\n",
262 debugstr_w(module
->modulename
), name
, address
);
263 if ((dbghelp_options
& SYMOPT_AUTO_PUBLICS
) &&
264 symt_find_nearest(module
, address
) != NULL
)
266 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
268 sym
->symt
.tag
= SymTagPublicSymbol
;
269 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
270 sym
->container
= compiland
? &compiland
->symt
: NULL
;
271 sym
->is_function
= is_function
;
272 sym
->address
= address
;
274 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
277 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
284 struct symt_data
* symt_new_global_variable(struct module
* module
,
285 struct symt_compiland
* compiland
,
286 const char* name
, unsigned is_static
,
287 struct location loc
, ULONG_PTR size
,
290 struct symt_data
* sym
;
294 TRACE_(dbghelp_symt
)("Adding global symbol %s:%s %d@%lx %p\n",
295 debugstr_w(module
->modulename
), name
, loc
.kind
, loc
.offset
, type
);
296 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
298 sym
->symt
.tag
= SymTagData
;
299 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
300 sym
->kind
= is_static
? DataIsFileStatic
: DataIsGlobal
;
301 sym
->container
= compiland
? &compiland
->symt
: NULL
;
304 if (type
&& size
&& symt_get_info(module
, type
, TI_GET_LENGTH
, &tsz
))
307 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
308 debugstr_w(module
->modulename
), name
,
309 wine_dbgstr_longlong(tsz
), size
);
311 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
314 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
321 static void init_function_or_inlinesite(struct symt_function
* sym
,
322 struct module
* module
,
324 struct symt
* container
,
326 ULONG_PTR addr
, ULONG_PTR size
,
327 struct symt
* sig_type
)
329 assert(!sig_type
|| sig_type
->tag
== SymTagFunctionType
);
331 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
332 sym
->container
= container
;
334 sym
->type
= sig_type
;
336 vector_init(&sym
->vlines
, sizeof(struct line_info
), 64);
337 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
340 struct symt_function
* symt_new_function(struct module
* module
,
341 struct symt_compiland
* compiland
,
343 ULONG_PTR addr
, ULONG_PTR size
,
344 struct symt
* sig_type
)
346 struct symt_function
* sym
;
348 TRACE_(dbghelp_symt
)("Adding global function %s:%s @%lx-%lx\n",
349 debugstr_w(module
->modulename
), name
, addr
, addr
+ size
- 1);
350 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
353 init_function_or_inlinesite(sym
, module
, SymTagFunction
, &compiland
->symt
, name
, addr
, size
, sig_type
);
354 sym
->next_inlinesite
= NULL
; /* first of list */
355 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
358 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
365 struct symt_inlinesite
* symt_new_inlinesite(struct module
* module
,
366 struct symt_function
* func
,
367 struct symt
* container
,
370 struct symt
* sig_type
)
372 struct symt_inlinesite
* sym
;
374 TRACE_(dbghelp_symt
)("Adding inline site %s @%lx\n", name
, addr
);
375 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
379 init_function_or_inlinesite(&sym
->func
, module
, SymTagInlineSite
, container
, name
, addr
, 0, sig_type
);
380 vector_init(&sym
->vranges
, sizeof(struct addr_range
), 2); /* FIXME: number of elts => to be set on input */
381 /* chain inline sites */
382 sym
->func
.next_inlinesite
= func
->next_inlinesite
;
383 func
->next_inlinesite
= sym
;
384 if (container
->tag
== SymTagFunction
|| container
->tag
== SymTagInlineSite
)
385 p
= vector_add(&((struct symt_function
*)container
)->vchildren
, &module
->pool
);
388 assert(container
->tag
== SymTagBlock
);
389 p
= vector_add(&((struct symt_block
*)container
)->vchildren
, &module
->pool
);
391 *p
= &sym
->func
.symt
;
396 void symt_add_func_line(struct module
* module
, struct symt_function
* func
,
397 unsigned source_idx
, int line_num
, ULONG_PTR addr
)
399 struct line_info
* dli
;
401 struct line_info
* prev
;
402 BOOL last_matches
= FALSE
;
405 if (func
== NULL
|| !(dbghelp_options
& SYMOPT_LOAD_LINES
)) return;
407 TRACE_(dbghelp_symt
)("(%p)%s:%Ix %s:%u\n",
408 func
, func
->hash_elt
.name
, addr
,
409 source_get(module
, source_idx
), line_num
);
411 assert(func
->symt
.tag
== SymTagFunction
|| func
->symt
.tag
== SymTagInlineSite
);
413 for (i
=vector_length(&func
->vlines
)-1; i
>=0; i
--)
415 dli
= vector_at(&func
->vlines
, i
);
416 if (dli
->is_source_file
)
418 last_matches
= (source_idx
== dli
->u
.source_file
);
422 vlen
= vector_length(&func
->vlines
);
423 prev
= vlen
? vector_at(&func
->vlines
, vlen
- 1) : NULL
;
424 if (last_matches
&& prev
&& addr
== prev
->u
.address
)
426 WARN("Duplicate addition of line number in %s\n", func
->hash_elt
.name
);
431 /* we shouldn't have line changes on first line of function */
432 dli
= vector_add(&func
->vlines
, &module
->pool
);
433 dli
->is_source_file
= 1;
434 dli
->is_first
= (prev
== NULL
);
436 dli
->line_number
= 0;
437 dli
->u
.source_file
= source_idx
;
439 /* clear previous last */
440 if (prev
) prev
->is_last
= 0;
441 dli
= vector_add(&func
->vlines
, &module
->pool
);
442 dli
->is_source_file
= 0;
443 dli
->is_first
= 0; /* only a source file can be first */
445 dli
->line_number
= line_num
;
446 dli
->u
.address
= addr
;
449 /******************************************************************
450 * symt_add_func_local
452 * Adds a new local/parameter to a given function:
453 * In any cases, dt tells whether it's a local variable or a parameter
454 * If regno it's not 0:
455 * - then variable is stored in a register
456 * - otherwise, value is referenced by register + offset
457 * Otherwise, the variable is stored on the stack:
458 * - offset is then the offset from the frame register
460 struct symt_data
* symt_add_func_local(struct module
* module
,
461 struct symt_function
* func
,
463 const struct location
* loc
,
464 struct symt_block
* block
,
465 struct symt
* type
, const char* name
)
467 struct symt_data
* locsym
;
470 TRACE_(dbghelp_symt
)("Adding local symbol (%s:%s): %s %p\n",
471 debugstr_w(module
->modulename
), func
->hash_elt
.name
,
474 assert(symt_check_tag(&func
->symt
, SymTagFunction
) || symt_check_tag(&func
->symt
, SymTagInlineSite
));
475 assert(dt
== DataIsParam
|| dt
== DataIsLocal
);
477 locsym
= pool_alloc(&module
->pool
, sizeof(*locsym
));
478 locsym
->symt
.tag
= SymTagData
;
479 locsym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
480 locsym
->hash_elt
.next
= NULL
;
482 locsym
->container
= block
? &block
->symt
: &func
->symt
;
484 locsym
->u
.var
= *loc
;
486 p
= vector_add(&block
->vchildren
, &module
->pool
);
488 p
= vector_add(&func
->vchildren
, &module
->pool
);
493 /******************************************************************
494 * symt_add_func_local
496 * Adds a new (local) constant to a given function
498 struct symt_data
* symt_add_func_constant(struct module
* module
,
499 struct symt_function
* func
,
500 struct symt_block
* block
,
501 struct symt
* type
, const char* name
,
504 struct symt_data
* locsym
;
507 TRACE_(dbghelp_symt
)("Adding local constant (%s:%s): %s %p\n",
508 debugstr_w(module
->modulename
), func
->hash_elt
.name
,
511 assert(symt_check_tag(&func
->symt
, SymTagFunction
) || symt_check_tag(&func
->symt
, SymTagInlineSite
));
513 locsym
= pool_alloc(&module
->pool
, sizeof(*locsym
));
514 locsym
->symt
.tag
= SymTagData
;
515 locsym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
516 locsym
->hash_elt
.next
= NULL
;
517 locsym
->kind
= DataIsConstant
;
518 locsym
->container
= block
? &block
->symt
: &func
->symt
;
520 locsym
->u
.value
= *v
;
522 p
= vector_add(&block
->vchildren
, &module
->pool
);
524 p
= vector_add(&func
->vchildren
, &module
->pool
);
529 struct symt_block
* symt_open_func_block(struct module
* module
,
530 struct symt_function
* func
,
531 struct symt_block
* parent_block
,
532 unsigned pc
, unsigned len
)
534 struct symt_block
* block
;
537 assert(symt_check_tag(&func
->symt
, SymTagFunction
) || symt_check_tag(&func
->symt
, SymTagInlineSite
));
539 assert(!parent_block
|| parent_block
->symt
.tag
== SymTagBlock
);
540 block
= pool_alloc(&module
->pool
, sizeof(*block
));
541 block
->symt
.tag
= SymTagBlock
;
542 block
->address
= func
->address
+ pc
;
544 block
->container
= parent_block
? &parent_block
->symt
: &func
->symt
;
545 vector_init(&block
->vchildren
, sizeof(struct symt
*), 4);
547 p
= vector_add(&parent_block
->vchildren
, &module
->pool
);
549 p
= vector_add(&func
->vchildren
, &module
->pool
);
555 struct symt_block
* symt_close_func_block(struct module
* module
,
556 const struct symt_function
* func
,
557 struct symt_block
* block
, unsigned pc
)
559 assert(symt_check_tag(&func
->symt
, SymTagFunction
) || symt_check_tag(&func
->symt
, SymTagInlineSite
));
561 if (pc
) block
->size
= func
->address
+ pc
- block
->address
;
562 return (block
->container
->tag
== SymTagBlock
) ?
563 CONTAINING_RECORD(block
->container
, struct symt_block
, symt
) : NULL
;
566 struct symt_hierarchy_point
* symt_add_function_point(struct module
* module
,
567 struct symt_function
* func
,
568 enum SymTagEnum point
,
569 const struct location
* loc
,
572 struct symt_hierarchy_point
*sym
;
575 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
577 sym
->symt
.tag
= point
;
578 sym
->parent
= &func
->symt
;
580 sym
->hash_elt
.name
= name
? pool_strdup(&module
->pool
, name
) : NULL
;
581 p
= vector_add(&func
->vchildren
, &module
->pool
);
587 /* low and high are absolute addresses */
588 BOOL
symt_add_inlinesite_range(struct module
* module
,
589 struct symt_inlinesite
* inlined
,
590 ULONG_PTR low
, ULONG_PTR high
)
592 struct addr_range
* p
;
594 p
= vector_add(&inlined
->vranges
, &module
->pool
);
601 /* see dbghelp_private.h for the assumptions */
602 for (i
= 0; i
< inlined
->vranges
.num_elts
- 1; i
++)
604 if (!addr_range_disjoint((struct addr_range
*)vector_at(&inlined
->vranges
, i
), p
))
606 FIXME("Added addr_range isn't disjoint from siblings\n");
609 for ( ; inlined
->func
.symt
.tag
!= SymTagFunction
; inlined
= (struct symt_inlinesite
*)symt_get_upper_inlined(inlined
))
611 for (i
= 0; i
< inlined
->vranges
.num_elts
; i
++)
613 struct addr_range
* ar
= (struct addr_range
*)vector_at(&inlined
->vranges
, i
);
614 if (!addr_range_disjoint(ar
, p
) && !addr_range_inside(ar
, p
))
615 FIXME("Added addr_range not compatible with parent\n");
623 struct symt_thunk
* symt_new_thunk(struct module
* module
,
624 struct symt_compiland
* compiland
,
625 const char* name
, THUNK_ORDINAL ord
,
626 ULONG_PTR addr
, ULONG_PTR size
)
628 struct symt_thunk
* sym
;
630 TRACE_(dbghelp_symt
)("Adding global thunk %s:%s @%lx-%lx\n",
631 debugstr_w(module
->modulename
), name
, addr
, addr
+ size
- 1);
633 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
635 sym
->symt
.tag
= SymTagThunk
;
636 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
637 sym
->container
= &compiland
->symt
;
641 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
645 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
652 struct symt_data
* symt_new_constant(struct module
* module
,
653 struct symt_compiland
* compiland
,
654 const char* name
, struct symt
* type
,
657 struct symt_data
* sym
;
659 TRACE_(dbghelp_symt
)("Adding constant value %s:%s\n",
660 debugstr_w(module
->modulename
), name
);
662 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
664 sym
->symt
.tag
= SymTagData
;
665 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
666 sym
->kind
= DataIsConstant
;
667 sym
->container
= compiland
? &compiland
->symt
: NULL
;
670 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
674 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
681 struct symt_hierarchy_point
* symt_new_label(struct module
* module
,
682 struct symt_compiland
* compiland
,
683 const char* name
, ULONG_PTR address
)
685 struct symt_hierarchy_point
* sym
;
687 TRACE_(dbghelp_symt
)("Adding global label value %s:%s\n",
688 debugstr_w(module
->modulename
), name
);
690 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
692 sym
->symt
.tag
= SymTagLabel
;
693 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
694 sym
->loc
.kind
= loc_absolute
;
695 sym
->loc
.offset
= address
;
696 sym
->parent
= compiland
? &compiland
->symt
: NULL
;
697 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
701 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
708 struct symt_custom
* symt_new_custom(struct module
* module
, const char* name
,
709 DWORD64 addr
, DWORD size
)
711 struct symt_custom
* sym
;
713 TRACE_(dbghelp_symt
)("Adding custom symbol %s:%s\n",
714 debugstr_w(module
->modulename
), name
);
716 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
718 sym
->symt
.tag
= SymTagCustom
;
719 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
722 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
727 /* expect sym_info->MaxNameLen to be set before being called */
728 static void symt_fill_sym_info(struct module_pair
* pair
,
729 const struct symt_function
* func
,
730 const struct symt
* sym
, SYMBOL_INFO
* sym_info
)
736 if (!symt_get_info(pair
->effective
, sym
, TI_GET_TYPE
, &sym_info
->TypeIndex
))
737 sym_info
->TypeIndex
= 0;
738 sym_info
->Index
= symt_ptr2index(pair
->effective
, sym
);
739 sym_info
->Reserved
[0] = sym_info
->Reserved
[1] = 0;
740 if (!symt_get_info(pair
->effective
, sym
, TI_GET_LENGTH
, &size
) &&
741 (!sym_info
->TypeIndex
||
742 !symt_get_info(pair
->effective
, symt_index2ptr(pair
->effective
, sym_info
->TypeIndex
),
743 TI_GET_LENGTH
, &size
)))
745 sym_info
->Size
= (DWORD
)size
;
746 sym_info
->ModBase
= pair
->requested
->module
.BaseOfImage
;
754 const struct symt_data
* data
= (const struct symt_data
*)sym
;
758 sym_info
->Flags
|= SYMFLAG_PARAMETER
;
761 sym_info
->Flags
|= SYMFLAG_LOCAL
;
763 struct location loc
= data
->u
.var
;
765 if (loc
.kind
>= loc_user
)
768 struct module_format
* modfmt
;
770 for (i
= 0; i
< DFI_LAST
; i
++)
772 modfmt
= pair
->effective
->format_info
[i
];
773 if (modfmt
&& modfmt
->loc_compute
)
775 modfmt
->loc_compute(pair
->pcs
, modfmt
, func
, &loc
);
783 /* for now we report error cases as a negative register number */
786 sym_info
->Flags
|= SYMFLAG_REGISTER
;
787 sym_info
->Register
= loc
.reg
;
788 sym_info
->Address
= 0;
791 sym_info
->Flags
|= SYMFLAG_REGREL
;
792 sym_info
->Register
= loc
.reg
;
793 if (loc
.reg
== CV_REG_NONE
|| (int)loc
.reg
< 0 /* error */)
794 FIXME("suspicious register value %x\n", loc
.reg
);
795 sym_info
->Address
= loc
.offset
;
798 sym_info
->Flags
|= SYMFLAG_VALUEPRESENT
;
799 sym_info
->Value
= loc
.offset
;
802 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc
.kind
);
808 case DataIsFileStatic
:
809 switch (data
->u
.var
.kind
)
812 sym_info
->Flags
|= SYMFLAG_TLSREL
;
815 symt_get_address(sym
, &sym_info
->Address
);
816 sym_info
->Register
= 0;
819 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data
->u
.var
.kind
);
824 sym_info
->Flags
|= SYMFLAG_VALUEPRESENT
;
825 if (data
->container
&&
826 (data
->container
->tag
== SymTagFunction
|| data
->container
->tag
== SymTagBlock
))
827 sym_info
->Flags
|= SYMFLAG_LOCAL
;
828 switch (V_VT(&data
->u
.value
))
830 case VT_I8
: sym_info
->Value
= (LONG64
)V_I8(&data
->u
.value
); break;
831 case VT_I4
: sym_info
->Value
= (LONG64
)V_I4(&data
->u
.value
); break;
832 case VT_I2
: sym_info
->Value
= (LONG64
)V_I2(&data
->u
.value
); break;
833 case VT_I1
: sym_info
->Value
= (LONG64
)V_I1(&data
->u
.value
); break;
834 case VT_UINT
:sym_info
->Value
= V_UINT(&data
->u
.value
); break;
835 case VT_UI8
: sym_info
->Value
= V_UI8(&data
->u
.value
); break;
836 case VT_UI4
: sym_info
->Value
= V_UI4(&data
->u
.value
); break;
837 case VT_UI2
: sym_info
->Value
= V_UI2(&data
->u
.value
); break;
838 case VT_UI1
: sym_info
->Value
= V_UI1(&data
->u
.value
); break;
839 case VT_BYREF
: sym_info
->Value
= (DWORD_PTR
)V_BYREF(&data
->u
.value
); break;
840 case VT_EMPTY
: sym_info
->Value
= 0; break;
842 FIXME("Unsupported variant type (%u)\n", V_VT(&data
->u
.value
));
848 FIXME("Unhandled kind (%u) in sym data\n", data
->kind
);
852 case SymTagPublicSymbol
:
854 const struct symt_public
* pub
= (const struct symt_public
*)sym
;
855 if (pub
->is_function
)
856 sym_info
->Flags
|= SYMFLAG_PUBLIC_CODE
;
858 sym_info
->Flags
|= SYMFLAG_EXPORT
;
859 symt_get_address(sym
, &sym_info
->Address
);
863 case SymTagInlineSite
:
864 symt_get_address(sym
, &sym_info
->Address
);
867 sym_info
->Flags
|= SYMFLAG_THUNK
;
868 symt_get_address(sym
, &sym_info
->Address
);
871 symt_get_address(sym
, &sym_info
->Address
);
872 sym_info
->Flags
|= SYMFLAG_VIRTUAL
;
875 symt_get_address(sym
, &sym_info
->Address
);
876 sym_info
->Register
= 0;
879 sym_info
->Scope
= 0; /* FIXME */
880 sym_info
->Tag
= sym
->tag
;
881 name
= symt_get_name(sym
);
882 if (sym_info
->MaxNameLen
&&
883 sym
->tag
== SymTagPublicSymbol
&& (dbghelp_options
& SYMOPT_UNDNAME
) &&
884 (tmp
= __unDName(NULL
, name
, 0, malloc
, free
, UNDNAME_NAME_ONLY
)) != NULL
)
886 symbol_setname(sym_info
, tmp
);
890 symbol_setname(sym_info
, name
);
892 TRACE_(dbghelp_symt
)("%p => %s %u %s\n",
893 sym
, sym_info
->Name
, sym_info
->Size
,
894 wine_dbgstr_longlong(sym_info
->Address
));
899 PSYM_ENUMERATESYMBOLS_CALLBACK cb
;
901 SYMBOL_INFO
* sym_info
;
905 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
908 static BOOL
send_symbol(const struct sym_enum
* se
, struct module_pair
* pair
,
909 const struct symt_function
* func
, const struct symt
* sym
)
911 symt_fill_sym_info(pair
, func
, sym
, se
->sym_info
);
912 if (se
->index
&& se
->sym_info
->Index
!= se
->index
) return FALSE
;
913 if (se
->tag
&& se
->sym_info
->Tag
!= se
->tag
) return FALSE
;
914 if (se
->addr
&& !(se
->addr
>= se
->sym_info
->Address
&& se
->addr
< se
->sym_info
->Address
+ se
->sym_info
->Size
)) return FALSE
;
915 return !se
->cb(se
->sym_info
, se
->sym_info
->Size
, se
->user
);
918 static BOOL
symt_enum_module(struct module_pair
* pair
, const WCHAR
* match
,
919 const struct sym_enum
* se
)
922 struct symt_ht
* sym
= NULL
;
923 struct hash_table_iter hti
;
927 hash_table_iter_init(&pair
->effective
->ht_symbols
, &hti
, NULL
);
928 while ((ptr
= hash_table_iter_up(&hti
)))
930 sym
= CONTAINING_RECORD(ptr
, struct symt_ht
, hash_elt
);
931 nameW
= symt_get_nameW(&sym
->symt
);
932 ret
= SymMatchStringW(nameW
, match
, FALSE
);
933 HeapFree(GetProcessHeap(), 0, nameW
);
936 se
->sym_info
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
937 se
->sym_info
->MaxNameLen
= sizeof(se
->buffer
) - sizeof(SYMBOL_INFO
);
938 if (send_symbol(se
, pair
, NULL
, &sym
->symt
)) return TRUE
;
944 static inline unsigned where_to_insert(struct module
* module
, unsigned high
, const struct symt_ht
* elt
)
946 unsigned low
= 0, mid
= high
/ 2;
950 symt_get_address(&elt
->symt
, &addr
);
953 switch (cmp_sorttab_addr(module
, mid
, addr
))
956 case -1: low
= mid
+ 1; break;
957 case 1: high
= mid
; break;
959 mid
= low
+ (high
- low
) / 2;
960 } while (low
< high
);
964 /***********************************************************************
967 * Rebuild sorted list of symbols for a module.
969 static BOOL
resort_symbols(struct module
* module
)
973 if (!(module
->module
.NumSyms
= module
->num_symbols
))
976 /* we know that set from 0 up to num_sorttab is already sorted
977 * so sort the remaining (new) symbols, and merge the two sets
978 * (unless the first set is empty)
980 delta
= module
->num_symbols
- module
->num_sorttab
;
981 qsort(&module
->addr_sorttab
[module
->num_sorttab
], delta
, sizeof(struct symt_ht
*), symt_cmp_addr
);
982 if (module
->num_sorttab
)
984 int i
, ins_idx
= module
->num_sorttab
, prev_ins_idx
;
985 static struct symt_ht
** tmp
;
986 static unsigned num_tmp
;
990 static struct symt_ht
** new;
992 new = HeapReAlloc(GetProcessHeap(), 0, tmp
, delta
* sizeof(struct symt_ht
*));
994 new = HeapAlloc(GetProcessHeap(), 0, delta
* sizeof(struct symt_ht
*));
997 module
->num_sorttab
= 0;
998 return resort_symbols(module
);
1003 memcpy(tmp
, &module
->addr_sorttab
[module
->num_sorttab
], delta
* sizeof(struct symt_ht
*));
1004 qsort(tmp
, delta
, sizeof(struct symt_ht
*), symt_cmp_addr
);
1006 for (i
= delta
- 1; i
>= 0; i
--)
1008 prev_ins_idx
= ins_idx
;
1009 ins_idx
= where_to_insert(module
, ins_idx
, tmp
[i
]);
1010 memmove(&module
->addr_sorttab
[ins_idx
+ i
+ 1],
1011 &module
->addr_sorttab
[ins_idx
],
1012 (prev_ins_idx
- ins_idx
) * sizeof(struct symt_ht
*));
1013 module
->addr_sorttab
[ins_idx
+ i
] = tmp
[i
];
1016 module
->num_sorttab
= module
->num_symbols
;
1017 return module
->sortlist_valid
= TRUE
;
1020 static void symt_get_length(struct module
* module
, const struct symt
* symt
, ULONG64
* size
)
1024 if (symt_get_info(module
, symt
, TI_GET_LENGTH
, size
) && *size
)
1027 if (symt_get_info(module
, symt
, TI_GET_TYPE
, &type_index
) &&
1028 symt_get_info(module
, symt_index2ptr(module
, type_index
), TI_GET_LENGTH
, size
)) return;
1029 *size
= 0x1000; /* arbitrary value */
1032 /* needed by symt_find_nearest */
1033 static int symt_get_best_at(struct module
* module
, int idx_sorttab
)
1036 int idx_sorttab_orig
= idx_sorttab
;
1037 if (module
->addr_sorttab
[idx_sorttab
]->symt
.tag
== SymTagPublicSymbol
)
1039 symt_get_address(&module
->addr_sorttab
[idx_sorttab
]->symt
, &ref_addr
);
1040 while (idx_sorttab
> 0 &&
1041 module
->addr_sorttab
[idx_sorttab
]->symt
.tag
== SymTagPublicSymbol
&&
1042 !cmp_sorttab_addr(module
, idx_sorttab
- 1, ref_addr
))
1044 if (module
->addr_sorttab
[idx_sorttab
]->symt
.tag
== SymTagPublicSymbol
)
1046 idx_sorttab
= idx_sorttab_orig
;
1047 while (idx_sorttab
< module
->num_sorttab
- 1 &&
1048 module
->addr_sorttab
[idx_sorttab
]->symt
.tag
== SymTagPublicSymbol
&&
1049 !cmp_sorttab_addr(module
, idx_sorttab
+ 1, ref_addr
))
1052 /* if no better symbol was found restore the original */
1053 if (module
->addr_sorttab
[idx_sorttab
]->symt
.tag
== SymTagPublicSymbol
)
1054 idx_sorttab
= idx_sorttab_orig
;
1059 /* assume addr is in module */
1060 struct symt_ht
* symt_find_nearest(struct module
* module
, DWORD_PTR addr
)
1063 ULONG64 ref_addr
, ref_size
;
1065 if (!module
->sortlist_valid
|| !module
->addr_sorttab
)
1067 if (!resort_symbols(module
)) return NULL
;
1071 * Binary search to find closest symbol.
1074 high
= module
->num_sorttab
;
1076 symt_get_address(&module
->addr_sorttab
[0]->symt
, &ref_addr
);
1077 if (addr
< ref_addr
) return NULL
;
1081 symt_get_address(&module
->addr_sorttab
[high
- 1]->symt
, &ref_addr
);
1082 symt_get_length(module
, &module
->addr_sorttab
[high
- 1]->symt
, &ref_size
);
1083 if (addr
>= ref_addr
+ ref_size
) return NULL
;
1086 while (high
> low
+ 1)
1088 mid
= (high
+ low
) / 2;
1089 if (cmp_sorttab_addr(module
, mid
, addr
) < 0)
1094 if (low
!= high
&& high
!= module
->num_sorttab
&&
1095 cmp_sorttab_addr(module
, high
, addr
) <= 0)
1098 /* If found symbol is a public symbol, check if there are any other entries that
1099 * might also have the same address, but would get better information
1101 low
= symt_get_best_at(module
, low
);
1103 return module
->addr_sorttab
[low
];
1106 static BOOL
symt_enum_locals_helper(struct module_pair
* pair
,
1107 const WCHAR
* match
, const struct sym_enum
* se
,
1108 struct symt_function
* func
, const struct vector
* v
)
1110 struct symt
* lsym
= NULL
;
1111 DWORD_PTR pc
= pair
->pcs
->localscope_pc
;
1116 for (i
=0; i
<vector_length(v
); i
++)
1118 lsym
= *(struct symt
**)vector_at(v
, i
);
1123 struct symt_block
* block
= (struct symt_block
*)lsym
;
1124 if (pc
< block
->address
|| block
->address
+ block
->size
<= pc
)
1126 if (!symt_enum_locals_helper(pair
, match
, se
, func
, &block
->vchildren
))
1131 nameW
= symt_get_nameW(lsym
);
1132 ret
= SymMatchStringW(nameW
, match
,
1133 !(dbghelp_options
& SYMOPT_CASE_INSENSITIVE
));
1134 HeapFree(GetProcessHeap(), 0, nameW
);
1137 if (send_symbol(se
, pair
, func
, lsym
)) return FALSE
;
1141 case SymTagFuncDebugStart
:
1142 case SymTagFuncDebugEnd
:
1144 case SymTagInlineSite
:
1147 FIXME("Unknown type: %u (%x)\n", lsym
->tag
, lsym
->tag
);
1154 static BOOL
symt_enum_locals(struct process
* pcs
, const WCHAR
* mask
,
1155 const struct sym_enum
* se
)
1157 struct module_pair pair
;
1159 se
->sym_info
->SizeOfStruct
= sizeof(*se
->sym_info
);
1160 se
->sym_info
->MaxNameLen
= sizeof(se
->buffer
) - sizeof(SYMBOL_INFO
);
1163 pair
.requested
= module_find_by_addr(pair
.pcs
, pcs
->localscope_pc
, DMT_UNKNOWN
);
1164 if (!module_get_debug(&pair
)) return FALSE
;
1166 if (symt_check_tag(pcs
->localscope_symt
, SymTagFunction
) ||
1167 symt_check_tag(pcs
->localscope_symt
, SymTagInlineSite
))
1169 struct symt_function
* func
= (struct symt_function
*)pcs
->localscope_symt
;
1170 return symt_enum_locals_helper(&pair
, mask
? mask
: L
"*", se
, func
, &func
->vchildren
);
1175 /**********************************************************
1178 * Properly sets Name and NameLen in SYMBOL_INFO
1179 * according to MaxNameLen value
1181 void symbol_setname(SYMBOL_INFO
* sym_info
, const char* name
)
1186 sym_info
->NameLen
= strlen(name
);
1187 if (sym_info
->MaxNameLen
)
1189 len
= min(sym_info
->NameLen
, sym_info
->MaxNameLen
- 1);
1190 memcpy(sym_info
->Name
, name
, len
);
1194 sym_info
->NameLen
= 0;
1195 sym_info
->Name
[len
] = '\0';
1198 /******************************************************************
1201 * Helper for transforming an ANSI symbol info into a UNICODE one.
1202 * Assume that MaxNameLen is the same for both version (A & W).
1204 void copy_symbolW(SYMBOL_INFOW
* siw
, const SYMBOL_INFO
* si
)
1206 siw
->SizeOfStruct
= si
->SizeOfStruct
;
1207 siw
->TypeIndex
= si
->TypeIndex
;
1208 siw
->Reserved
[0] = si
->Reserved
[0];
1209 siw
->Reserved
[1] = si
->Reserved
[1];
1210 siw
->Index
= si
->Index
;
1211 siw
->Size
= si
->Size
;
1212 siw
->ModBase
= si
->ModBase
;
1213 siw
->Flags
= si
->Flags
;
1214 siw
->Value
= si
->Value
;
1215 siw
->Address
= si
->Address
;
1216 siw
->Register
= si
->Register
;
1217 siw
->Scope
= si
->Scope
;
1219 siw
->NameLen
= si
->NameLen
;
1220 siw
->MaxNameLen
= si
->MaxNameLen
;
1221 MultiByteToWideChar(CP_ACP
, 0, si
->Name
, -1, siw
->Name
, siw
->MaxNameLen
);
1224 /* return the lowest inline site inside a function */
1225 struct symt_inlinesite
* symt_find_lowest_inlined(struct symt_function
* func
, DWORD64 addr
)
1227 struct symt_inlinesite
* current
;
1230 assert(func
->symt
.tag
== SymTagFunction
);
1231 for (current
= func
->next_inlinesite
; current
; current
= current
->func
.next_inlinesite
)
1233 for (i
= 0; i
< current
->vranges
.num_elts
; ++i
)
1235 struct addr_range
* ar
= (struct addr_range
*)vector_at(¤t
->vranges
, i
);
1236 /* first matching range gives the lowest inline site; see dbghelp_private.h for details */
1237 if (ar
->low
<= addr
&& addr
< ar
->high
)
1244 /* from an inline function, get either the enclosing inlined function, or the top function when no inlined */
1245 struct symt
* symt_get_upper_inlined(struct symt_inlinesite
* inlined
)
1247 struct symt
* symt
= &inlined
->func
.symt
;
1252 if (symt
->tag
== SymTagBlock
)
1253 symt
= ((struct symt_block
*)symt
)->container
;
1255 symt
= ((struct symt_function
*)symt
)->container
;
1256 } while (symt
->tag
== SymTagBlock
);
1257 assert(symt
->tag
== SymTagFunction
|| symt
->tag
== SymTagInlineSite
);
1261 /* lookup in module for an inline site (from addr and inline_ctx) */
1262 struct symt_inlinesite
* symt_find_inlined_site(struct module
* module
, DWORD64 addr
, DWORD inline_ctx
)
1264 struct symt_ht
* symt
= symt_find_nearest(module
, addr
);
1266 if (symt_check_tag(&symt
->symt
, SymTagFunction
))
1268 struct symt_function
* func
= (struct symt_function
*)symt
;
1269 struct symt_inlinesite
* curr
= symt_find_lowest_inlined(func
, addr
);
1270 DWORD depth
= IFC_DEPTH(inline_ctx
);
1273 for ( ; &curr
->func
!= func
; curr
= (struct symt_inlinesite
*)symt_get_upper_inlined(curr
))
1274 if (depth
-- == 0) return curr
;
1279 DWORD
symt_get_inlinesite_depth(HANDLE hProcess
, DWORD64 addr
)
1281 struct module_pair pair
;
1284 if (module_init_pair(&pair
, hProcess
, addr
))
1286 struct symt_ht
* symt
= symt_find_nearest(pair
.effective
, addr
);
1287 if (symt_check_tag(&symt
->symt
, SymTagFunction
))
1289 struct symt_inlinesite
* inlined
= symt_find_lowest_inlined((struct symt_function
*)symt
, addr
);
1292 for ( ; &inlined
->func
.symt
!= &symt
->symt
; inlined
= (struct symt_inlinesite
*)symt_get_upper_inlined(inlined
))
1300 /******************************************************************
1303 * Core routine for most of the enumeration of symbols
1305 static BOOL
sym_enum(HANDLE hProcess
, ULONG64 BaseOfDll
, PCWSTR Mask
,
1306 const struct sym_enum
* se
)
1308 struct module_pair pair
;
1312 pair
.pcs
= process_find_by_handle(hProcess
);
1313 if (!pair
.pcs
) return FALSE
;
1316 /* do local variables ? */
1317 if (!Mask
|| !(bang
= wcschr(Mask
, '!')))
1318 return symt_enum_locals(pair
.pcs
, Mask
, se
);
1320 if (bang
== Mask
) return FALSE
;
1322 mod
= HeapAlloc(GetProcessHeap(), 0, (bang
- Mask
+ 1) * sizeof(WCHAR
));
1323 if (!mod
) return FALSE
;
1324 memcpy(mod
, Mask
, (bang
- Mask
) * sizeof(WCHAR
));
1325 mod
[bang
- Mask
] = 0;
1327 for (pair
.requested
= pair
.pcs
->lmodules
; pair
.requested
; pair
.requested
= pair
.requested
->next
)
1329 if (pair
.requested
->type
== DMT_PE
&& module_get_debug(&pair
))
1331 if (SymMatchStringW(pair
.requested
->modulename
, mod
, FALSE
) &&
1332 symt_enum_module(&pair
, bang
+ 1, se
))
1336 /* not found in PE modules, retry on the ELF ones
1338 if (!pair
.requested
&& dbghelp_opt_native
)
1340 for (pair
.requested
= pair
.pcs
->lmodules
; pair
.requested
; pair
.requested
= pair
.requested
->next
)
1342 if ((pair
.requested
->type
== DMT_ELF
|| pair
.requested
->type
== DMT_MACHO
) &&
1343 !module_get_containee(pair
.pcs
, pair
.requested
) &&
1344 module_get_debug(&pair
))
1346 if (SymMatchStringW(pair
.requested
->modulename
, mod
, FALSE
) &&
1347 symt_enum_module(&pair
, bang
+ 1, se
))
1352 HeapFree(GetProcessHeap(), 0, mod
);
1355 pair
.requested
= module_find_by_addr(pair
.pcs
, BaseOfDll
, DMT_UNKNOWN
);
1356 if (!module_get_debug(&pair
))
1359 /* we always ignore module name from Mask when BaseOfDll is defined */
1360 if (Mask
&& (bang
= wcschr(Mask
, '!')))
1362 if (bang
== Mask
) return FALSE
;
1366 symt_enum_module(&pair
, Mask
? Mask
: L
"*", se
);
1371 static inline BOOL
doSymEnumSymbols(HANDLE hProcess
, ULONG64 BaseOfDll
, PCWSTR Mask
,
1372 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
1377 se
.cb
= EnumSymbolsCallback
;
1378 se
.user
= UserContext
;
1382 se
.sym_info
= (PSYMBOL_INFO
)se
.buffer
;
1384 return sym_enum(hProcess
, BaseOfDll
, Mask
, &se
);
1387 /******************************************************************
1388 * SymEnumSymbols (DBGHELP.@)
1390 * cases BaseOfDll = 0
1391 * !foo fails always (despite what MSDN states)
1392 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1393 * no ! in Mask, lookup in local Context
1394 * cases BaseOfDll != 0
1395 * !foo fails always (despite what MSDN states)
1396 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1398 BOOL WINAPI
SymEnumSymbols(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR Mask
,
1399 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
1405 TRACE("(%p %s %s %p %p)\n",
1406 hProcess
, wine_dbgstr_longlong(BaseOfDll
), debugstr_a(Mask
),
1407 EnumSymbolsCallback
, UserContext
);
1411 DWORD sz
= MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, NULL
, 0);
1412 if (!(maskW
= HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
))))
1414 MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, maskW
, sz
);
1416 ret
= doSymEnumSymbols(hProcess
, BaseOfDll
, maskW
, EnumSymbolsCallback
, UserContext
);
1417 HeapFree(GetProcessHeap(), 0, maskW
);
1423 PSYM_ENUMERATESYMBOLS_CALLBACKW cb
;
1425 PSYMBOL_INFOW sym_info
;
1426 char buffer
[sizeof(SYMBOL_INFOW
) + MAX_SYM_NAME
* sizeof(WCHAR
)];
1429 static BOOL CALLBACK
sym_enumW(PSYMBOL_INFO si
, ULONG size
, PVOID ctx
)
1431 struct sym_enumW
* sew
= ctx
;
1433 copy_symbolW(sew
->sym_info
, si
);
1435 return (sew
->cb
)(sew
->sym_info
, size
, sew
->ctx
);
1438 /******************************************************************
1439 * SymEnumSymbolsW (DBGHELP.@)
1442 BOOL WINAPI
SymEnumSymbolsW(HANDLE hProcess
, ULONG64 BaseOfDll
, PCWSTR Mask
,
1443 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback
,
1446 struct sym_enumW sew
;
1448 sew
.ctx
= UserContext
;
1449 sew
.cb
= EnumSymbolsCallback
;
1450 sew
.sym_info
= (PSYMBOL_INFOW
)sew
.buffer
;
1452 return doSymEnumSymbols(hProcess
, BaseOfDll
, Mask
, sym_enumW
, &sew
);
1455 struct sym_enumerate
1458 PSYM_ENUMSYMBOLS_CALLBACK cb
;
1461 static BOOL CALLBACK
sym_enumerate_cb(PSYMBOL_INFO syminfo
, ULONG size
, void* ctx
)
1463 struct sym_enumerate
* se
= ctx
;
1464 return (se
->cb
)(syminfo
->Name
, syminfo
->Address
, syminfo
->Size
, se
->ctx
);
1467 /***********************************************************************
1468 * SymEnumerateSymbols (DBGHELP.@)
1470 BOOL WINAPI
SymEnumerateSymbols(HANDLE hProcess
, DWORD BaseOfDll
,
1471 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback
,
1474 struct sym_enumerate se
;
1476 se
.ctx
= UserContext
;
1477 se
.cb
= EnumSymbolsCallback
;
1479 return SymEnumSymbols(hProcess
, BaseOfDll
, NULL
, sym_enumerate_cb
, &se
);
1482 struct sym_enumerate64
1485 PSYM_ENUMSYMBOLS_CALLBACK64 cb
;
1488 static BOOL CALLBACK
sym_enumerate_cb64(PSYMBOL_INFO syminfo
, ULONG size
, void* ctx
)
1490 struct sym_enumerate64
* se
= ctx
;
1491 return (se
->cb
)(syminfo
->Name
, syminfo
->Address
, syminfo
->Size
, se
->ctx
);
1494 /***********************************************************************
1495 * SymEnumerateSymbols64 (DBGHELP.@)
1497 BOOL WINAPI
SymEnumerateSymbols64(HANDLE hProcess
, DWORD64 BaseOfDll
,
1498 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback
,
1501 struct sym_enumerate64 se
;
1503 se
.ctx
= UserContext
;
1504 se
.cb
= EnumSymbolsCallback
;
1506 return SymEnumSymbols(hProcess
, BaseOfDll
, NULL
, sym_enumerate_cb64
, &se
);
1509 /******************************************************************
1510 * SymFromAddr (DBGHELP.@)
1513 BOOL WINAPI
SymFromAddr(HANDLE hProcess
, DWORD64 Address
,
1514 DWORD64
* Displacement
, PSYMBOL_INFO Symbol
)
1516 struct module_pair pair
;
1517 struct symt_ht
* sym
;
1519 if (!module_init_pair(&pair
, hProcess
, Address
)) return FALSE
;
1520 if ((sym
= symt_find_nearest(pair
.effective
, Address
)) == NULL
) return FALSE
;
1522 symt_fill_sym_info(&pair
, NULL
, &sym
->symt
, Symbol
);
1524 *Displacement
= (Address
>= Symbol
->Address
) ? (Address
- Symbol
->Address
) : (DWORD64
)-1;
1528 /******************************************************************
1529 * SymFromAddrW (DBGHELP.@)
1532 BOOL WINAPI
SymFromAddrW(HANDLE hProcess
, DWORD64 Address
,
1533 DWORD64
* Displacement
, PSYMBOL_INFOW Symbol
)
1539 len
= sizeof(*si
) + Symbol
->MaxNameLen
* sizeof(WCHAR
);
1540 si
= HeapAlloc(GetProcessHeap(), 0, len
);
1541 if (!si
) return FALSE
;
1543 si
->SizeOfStruct
= sizeof(*si
);
1544 si
->MaxNameLen
= Symbol
->MaxNameLen
;
1545 if ((ret
= SymFromAddr(hProcess
, Address
, Displacement
, si
)))
1547 copy_symbolW(Symbol
, si
);
1549 HeapFree(GetProcessHeap(), 0, si
);
1553 /******************************************************************
1554 * SymGetSymFromAddr (DBGHELP.@)
1557 BOOL WINAPI
SymGetSymFromAddr(HANDLE hProcess
, DWORD Address
,
1558 PDWORD Displacement
, PIMAGEHLP_SYMBOL Symbol
)
1560 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1561 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1563 DWORD64 Displacement64
;
1565 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1566 si
->SizeOfStruct
= sizeof(*si
);
1567 si
->MaxNameLen
= MAX_SYM_NAME
;
1568 if (!SymFromAddr(hProcess
, Address
, &Displacement64
, si
))
1572 *Displacement
= Displacement64
;
1573 Symbol
->Address
= si
->Address
;
1574 Symbol
->Size
= si
->Size
;
1575 Symbol
->Flags
= si
->Flags
;
1576 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1577 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1581 /******************************************************************
1582 * SymGetSymFromAddr64 (DBGHELP.@)
1585 BOOL WINAPI
SymGetSymFromAddr64(HANDLE hProcess
, DWORD64 Address
,
1586 PDWORD64 Displacement
, PIMAGEHLP_SYMBOL64 Symbol
)
1588 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1589 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1591 DWORD64 Displacement64
;
1593 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1594 si
->SizeOfStruct
= sizeof(*si
);
1595 si
->MaxNameLen
= MAX_SYM_NAME
;
1596 if (!SymFromAddr(hProcess
, Address
, &Displacement64
, si
))
1600 *Displacement
= Displacement64
;
1601 Symbol
->Address
= si
->Address
;
1602 Symbol
->Size
= si
->Size
;
1603 Symbol
->Flags
= si
->Flags
;
1604 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1605 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1609 static BOOL
find_name(struct process
* pcs
, struct module
* module
, const char* name
,
1610 SYMBOL_INFO
* symbol
)
1612 struct hash_table_iter hti
;
1614 struct symt_ht
* sym
= NULL
;
1615 struct module_pair pair
;
1618 if (!(pair
.requested
= module
)) return FALSE
;
1619 if (!module_get_debug(&pair
)) return FALSE
;
1621 hash_table_iter_init(&pair
.effective
->ht_symbols
, &hti
, name
);
1622 while ((ptr
= hash_table_iter_up(&hti
)))
1624 sym
= CONTAINING_RECORD(ptr
, struct symt_ht
, hash_elt
);
1626 if (!strcmp(sym
->hash_elt
.name
, name
))
1628 symt_fill_sym_info(&pair
, NULL
, &sym
->symt
, symbol
);
1635 /******************************************************************
1636 * SymFromName (DBGHELP.@)
1639 BOOL WINAPI
SymFromName(HANDLE hProcess
, PCSTR Name
, PSYMBOL_INFO Symbol
)
1641 struct process
* pcs
= process_find_by_handle(hProcess
);
1642 struct module
* module
;
1645 TRACE("(%p, %s, %p)\n", hProcess
, Name
, Symbol
);
1646 if (!pcs
) return FALSE
;
1647 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1648 name
= strchr(Name
, '!');
1652 assert(name
- Name
< sizeof(tmp
));
1653 memcpy(tmp
, Name
, name
- Name
);
1654 tmp
[name
- Name
] = '\0';
1655 module
= module_find_by_nameA(pcs
, tmp
);
1656 return find_name(pcs
, module
, name
+ 1, Symbol
);
1658 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1660 if (module
->type
== DMT_PE
&& find_name(pcs
, module
, Name
, Symbol
))
1663 /* not found in PE modules, retry on the ELF ones
1665 if (dbghelp_opt_native
)
1667 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1669 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
1670 !module_get_containee(pcs
, module
) &&
1671 find_name(pcs
, module
, Name
, Symbol
))
1678 /***********************************************************************
1679 * SymFromNameW (DBGHELP.@)
1681 BOOL WINAPI
SymFromNameW(HANDLE process
, const WCHAR
*name
, SYMBOL_INFOW
*symbol
)
1688 TRACE("(%p, %s, %p)\n", process
, debugstr_w(name
), symbol
);
1690 len
= sizeof(*si
) + symbol
->MaxNameLen
;
1691 if (!(si
= HeapAlloc(GetProcessHeap(), 0, len
))) return FALSE
;
1693 len
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
1694 if (!(tmp
= HeapAlloc(GetProcessHeap(), 0, len
)))
1696 HeapFree(GetProcessHeap(), 0, si
);
1699 WideCharToMultiByte(CP_ACP
, 0, name
, -1, tmp
, len
, NULL
, NULL
);
1701 si
->SizeOfStruct
= sizeof(*si
);
1702 si
->MaxNameLen
= symbol
->MaxNameLen
;
1703 if ((ret
= SymFromName(process
, tmp
, si
)))
1704 copy_symbolW(symbol
, si
);
1706 HeapFree(GetProcessHeap(), 0, tmp
);
1707 HeapFree(GetProcessHeap(), 0, si
);
1711 /***********************************************************************
1712 * SymGetSymFromName64 (DBGHELP.@)
1714 BOOL WINAPI
SymGetSymFromName64(HANDLE hProcess
, PCSTR Name
, PIMAGEHLP_SYMBOL64 Symbol
)
1716 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1717 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1720 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1721 si
->SizeOfStruct
= sizeof(*si
);
1722 si
->MaxNameLen
= MAX_SYM_NAME
;
1723 if (!SymFromName(hProcess
, Name
, si
)) return FALSE
;
1725 Symbol
->Address
= si
->Address
;
1726 Symbol
->Size
= si
->Size
;
1727 Symbol
->Flags
= si
->Flags
;
1728 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1729 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1733 /***********************************************************************
1734 * SymGetSymFromName (DBGHELP.@)
1736 BOOL WINAPI
SymGetSymFromName(HANDLE hProcess
, PCSTR Name
, PIMAGEHLP_SYMBOL Symbol
)
1738 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1739 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1742 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1743 si
->SizeOfStruct
= sizeof(*si
);
1744 si
->MaxNameLen
= MAX_SYM_NAME
;
1745 if (!SymFromName(hProcess
, Name
, si
)) return FALSE
;
1747 Symbol
->Address
= si
->Address
;
1748 Symbol
->Size
= si
->Size
;
1749 Symbol
->Flags
= si
->Flags
;
1750 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1751 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1755 struct internal_line_t
1768 static void init_internal_line(struct internal_line_t
* intl
, BOOL unicode
)
1770 intl
->unicode
= unicode
;
1772 intl
->line_number
= 0;
1773 intl
->file_nameA
= NULL
;
1777 static BOOL
internal_line_copy_toA32(const struct internal_line_t
* intl
, IMAGEHLP_LINE
* l32
)
1779 if (intl
->unicode
) return FALSE
;
1780 l32
->Key
= intl
->key
;
1781 l32
->LineNumber
= intl
->line_number
;
1782 l32
->FileName
= intl
->file_nameA
;
1783 l32
->Address
= intl
->address
;
1787 static BOOL
internal_line_copy_toA64(const struct internal_line_t
* intl
, IMAGEHLP_LINE64
* l64
)
1789 if (intl
->unicode
) return FALSE
;
1790 l64
->Key
= intl
->key
;
1791 l64
->LineNumber
= intl
->line_number
;
1792 l64
->FileName
= intl
->file_nameA
;
1793 l64
->Address
= intl
->address
;
1797 static BOOL
internal_line_copy_toW64(const struct internal_line_t
* intl
, IMAGEHLP_LINEW64
* l64
)
1799 if (!intl
->unicode
) return FALSE
;
1800 l64
->Key
= intl
->key
;
1801 l64
->LineNumber
= intl
->line_number
;
1802 l64
->FileName
= intl
->file_nameW
;
1803 l64
->Address
= intl
->address
;
1807 static BOOL
internal_line_set_nameA(struct process
* pcs
, struct internal_line_t
* intl
, char* str
, BOOL copy
)
1813 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
1814 if (!(intl
->file_nameW
= fetch_buffer(pcs
, len
* sizeof(WCHAR
)))) return FALSE
;
1815 MultiByteToWideChar(CP_ACP
, 0, str
, -1, intl
->file_nameW
, len
);
1821 len
= strlen(str
) + 1;
1822 if (!(intl
->file_nameA
= fetch_buffer(pcs
, len
))) return FALSE
;
1823 memcpy(intl
->file_nameA
, str
, len
);
1826 intl
->file_nameA
= str
;
1831 static BOOL
internal_line_set_nameW(struct process
* pcs
, struct internal_line_t
* intl
, WCHAR
* wstr
, BOOL copy
)
1839 len
= (lstrlenW(wstr
) + 1) * sizeof(WCHAR
);
1840 if (!(intl
->file_nameW
= fetch_buffer(pcs
, len
))) return FALSE
;
1841 memcpy(intl
->file_nameW
, wstr
, len
);
1844 intl
->file_nameW
= wstr
;
1848 DWORD len
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
1849 if (!(intl
->file_nameA
= fetch_buffer(pcs
, len
))) return FALSE
;
1850 WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, intl
->file_nameA
, len
, NULL
, NULL
);
1855 static BOOL
get_line_from_function(struct module_pair
* pair
, struct symt_function
* func
, DWORD64 addr
,
1856 PDWORD pdwDisplacement
, struct internal_line_t
* intl
)
1858 struct line_info
* dli
= NULL
;
1859 struct line_info
* found_dli
= NULL
;
1862 for (i
= vector_length(&func
->vlines
) - 1; i
>= 0; i
--)
1864 dli
= vector_at(&func
->vlines
, i
);
1865 if (!dli
->is_source_file
)
1867 if (found_dli
|| dli
->u
.address
> addr
) continue;
1868 intl
->line_number
= dli
->line_number
;
1869 intl
->address
= dli
->u
.address
;
1877 if (dbghelp_opt_native
)
1879 /* Return native file paths when using winedbg */
1880 ret
= internal_line_set_nameA(pair
->pcs
, intl
, (char*)source_get(pair
->effective
, dli
->u
.source_file
), FALSE
);
1884 WCHAR
*dospath
= wine_get_dos_file_name(source_get(pair
->effective
, dli
->u
.source_file
));
1885 ret
= internal_line_set_nameW(pair
->pcs
, intl
, dospath
, TRUE
);
1886 HeapFree( GetProcessHeap(), 0, dospath
);
1888 if (ret
) *pdwDisplacement
= addr
- found_dli
->u
.address
;
1895 /******************************************************************
1896 * get_line_from_addr
1898 * fills source file information from an address
1900 static BOOL
get_line_from_addr(HANDLE hProcess
, DWORD64 addr
,
1901 PDWORD pdwDisplacement
, struct internal_line_t
* intl
)
1903 struct module_pair pair
;
1904 struct symt_ht
* symt
;
1906 if (!module_init_pair(&pair
, hProcess
, addr
)) return FALSE
;
1907 if ((symt
= symt_find_nearest(pair
.effective
, addr
)) == NULL
) return FALSE
;
1909 if (symt
->symt
.tag
!= SymTagFunction
&& symt
->symt
.tag
!= SymTagInlineSite
) return FALSE
;
1910 return get_line_from_function(&pair
, (struct symt_function
*)symt
, addr
, pdwDisplacement
, intl
);
1913 /***********************************************************************
1914 * SymGetSymNext64 (DBGHELP.@)
1916 BOOL WINAPI
SymGetSymNext64(HANDLE hProcess
, PIMAGEHLP_SYMBOL64 Symbol
)
1919 * get module from Symbol.Address
1920 * get index in module.addr_sorttab of Symbol.Address
1922 * if out of module bounds, move to next module in process address space
1924 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1925 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1929 /***********************************************************************
1930 * SymGetSymNext (DBGHELP.@)
1932 BOOL WINAPI
SymGetSymNext(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1934 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1935 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1939 /***********************************************************************
1940 * SymGetSymPrev64 (DBGHELP.@)
1942 BOOL WINAPI
SymGetSymPrev64(HANDLE hProcess
, PIMAGEHLP_SYMBOL64 Symbol
)
1944 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1945 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1949 /***********************************************************************
1950 * SymGetSymPrev (DBGHELP.@)
1952 BOOL WINAPI
SymGetSymPrev(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1954 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1955 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1959 /******************************************************************
1960 * SymGetLineFromAddr (DBGHELP.@)
1963 BOOL WINAPI
SymGetLineFromAddr(HANDLE hProcess
, DWORD dwAddr
,
1964 PDWORD pdwDisplacement
, PIMAGEHLP_LINE Line
)
1966 struct internal_line_t intl
;
1968 TRACE("(%p %p)\n", hProcess
, Line
);
1970 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1971 init_internal_line(&intl
, FALSE
);
1972 if (!get_line_from_addr(hProcess
, dwAddr
, pdwDisplacement
, &intl
)) return FALSE
;
1973 return internal_line_copy_toA32(&intl
, Line
);
1976 /******************************************************************
1977 * SymGetLineFromAddr64 (DBGHELP.@)
1980 BOOL WINAPI
SymGetLineFromAddr64(HANDLE hProcess
, DWORD64 dwAddr
,
1981 PDWORD pdwDisplacement
, PIMAGEHLP_LINE64 Line
)
1983 struct internal_line_t intl
;
1985 TRACE("(%p %p)\n", hProcess
, Line
);
1987 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1988 init_internal_line(&intl
, FALSE
);
1989 if (!get_line_from_addr(hProcess
, dwAddr
, pdwDisplacement
, &intl
)) return FALSE
;
1990 return internal_line_copy_toA64(&intl
, Line
);
1993 /******************************************************************
1994 * SymGetLineFromAddrW64 (DBGHELP.@)
1997 BOOL WINAPI
SymGetLineFromAddrW64(HANDLE hProcess
, DWORD64 dwAddr
,
1998 PDWORD pdwDisplacement
, PIMAGEHLP_LINEW64 Line
)
2000 struct internal_line_t intl
;
2002 TRACE("(%p %p)\n", hProcess
, Line
);
2004 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2005 init_internal_line(&intl
, TRUE
);
2006 if (!get_line_from_addr(hProcess
, dwAddr
, pdwDisplacement
, &intl
)) return FALSE
;
2007 return internal_line_copy_toW64(&intl
, Line
);
2010 static BOOL
symt_get_func_line_prev(HANDLE hProcess
, struct internal_line_t
* intl
, void* key
, DWORD64 addr
)
2012 struct module_pair pair
;
2013 struct line_info
* li
;
2014 struct line_info
* srcli
;
2016 if (!module_init_pair(&pair
, hProcess
, addr
)) return FALSE
;
2018 if (key
== NULL
) return FALSE
;
2022 while (!li
->is_first
)
2025 if (!li
->is_source_file
)
2027 intl
->line_number
= li
->line_number
;
2028 intl
->address
= li
->u
.address
;
2030 /* search source file */
2031 for (srcli
= li
; !srcli
->is_source_file
; srcli
--);
2033 return internal_line_set_nameA(pair
.pcs
, intl
, (char*)source_get(pair
.effective
, srcli
->u
.source_file
), FALSE
);
2036 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
2040 /******************************************************************
2041 * SymGetLinePrev64 (DBGHELP.@)
2044 BOOL WINAPI
SymGetLinePrev64(HANDLE hProcess
, PIMAGEHLP_LINE64 Line
)
2046 struct internal_line_t intl
;
2048 TRACE("(%p %p)\n", hProcess
, Line
);
2050 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2051 init_internal_line(&intl
, FALSE
);
2052 if (!symt_get_func_line_prev(hProcess
, &intl
, Line
->Key
, Line
->Address
)) return FALSE
;
2053 return internal_line_copy_toA64(&intl
, Line
);
2056 /******************************************************************
2057 * SymGetLinePrev (DBGHELP.@)
2060 BOOL WINAPI
SymGetLinePrev(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
2062 struct internal_line_t intl
;
2064 TRACE("(%p %p)\n", hProcess
, Line
);
2066 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2067 init_internal_line(&intl
, FALSE
);
2068 if (!symt_get_func_line_prev(hProcess
, &intl
, Line
->Key
, Line
->Address
)) return FALSE
;
2069 return internal_line_copy_toA32(&intl
, Line
);
2072 /******************************************************************
2073 * SymGetLinePrevW64 (DBGHELP.@)
2076 BOOL WINAPI
SymGetLinePrevW64(HANDLE hProcess
, PIMAGEHLP_LINEW64 Line
)
2078 struct internal_line_t intl
;
2080 TRACE("(%p %p)\n", hProcess
, Line
);
2082 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2083 init_internal_line(&intl
, TRUE
);
2084 if (!symt_get_func_line_prev(hProcess
, &intl
, Line
->Key
, Line
->Address
)) return FALSE
;
2085 return internal_line_copy_toW64(&intl
, Line
);
2088 static BOOL
symt_get_func_line_next(HANDLE hProcess
, struct internal_line_t
* intl
, void* key
, DWORD64 addr
)
2090 struct module_pair pair
;
2091 struct line_info
* li
;
2092 struct line_info
* srcli
;
2094 if (key
== NULL
) return FALSE
;
2095 if (!module_init_pair(&pair
, hProcess
, addr
)) return FALSE
;
2097 /* search current source file */
2098 for (srcli
= key
; !srcli
->is_source_file
; srcli
--);
2101 while (!li
->is_last
)
2104 if (!li
->is_source_file
)
2106 intl
->line_number
= li
->line_number
;
2107 intl
->address
= li
->u
.address
;
2109 return internal_line_set_nameA(pair
.pcs
, intl
, (char*)source_get(pair
.effective
, srcli
->u
.source_file
), FALSE
);
2113 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
2117 /******************************************************************
2118 * SymGetLineNext64 (DBGHELP.@)
2121 BOOL WINAPI
SymGetLineNext64(HANDLE hProcess
, PIMAGEHLP_LINE64 Line
)
2123 struct internal_line_t intl
;
2125 TRACE("(%p %p)\n", hProcess
, Line
);
2127 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2128 init_internal_line(&intl
, FALSE
);
2129 if (!symt_get_func_line_next(hProcess
, &intl
, Line
->Key
, Line
->Address
)) return FALSE
;
2130 return internal_line_copy_toA64(&intl
, Line
);
2133 /******************************************************************
2134 * SymGetLineNext (DBGHELP.@)
2137 BOOL WINAPI
SymGetLineNext(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
2139 struct internal_line_t intl
;
2141 TRACE("(%p %p)\n", hProcess
, Line
);
2143 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2144 init_internal_line(&intl
, FALSE
);
2145 if (!symt_get_func_line_next(hProcess
, &intl
, Line
->Key
, Line
->Address
)) return FALSE
;
2146 return internal_line_copy_toA32(&intl
, Line
);
2149 /******************************************************************
2150 * SymGetLineNextW64 (DBGHELP.@)
2153 BOOL WINAPI
SymGetLineNextW64(HANDLE hProcess
, PIMAGEHLP_LINEW64 Line
)
2155 struct internal_line_t intl
;
2157 TRACE("(%p %p)\n", hProcess
, Line
);
2159 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
2160 init_internal_line(&intl
, TRUE
);
2161 if (!symt_get_func_line_next(hProcess
, &intl
, Line
->Key
, Line
->Address
)) return FALSE
;
2162 return internal_line_copy_toW64(&intl
, Line
);
2165 /***********************************************************************
2166 * SymUnDName (DBGHELP.@)
2168 BOOL WINAPI
SymUnDName(PIMAGEHLP_SYMBOL sym
, PSTR UnDecName
, DWORD UnDecNameLength
)
2170 return UnDecorateSymbolName(sym
->Name
, UnDecName
, UnDecNameLength
,
2171 UNDNAME_COMPLETE
) != 0;
2174 /***********************************************************************
2175 * SymUnDName64 (DBGHELP.@)
2177 BOOL WINAPI
SymUnDName64(PIMAGEHLP_SYMBOL64 sym
, PSTR UnDecName
, DWORD UnDecNameLength
)
2179 return UnDecorateSymbolName(sym
->Name
, UnDecName
, UnDecNameLength
,
2180 UNDNAME_COMPLETE
) != 0;
2183 /***********************************************************************
2184 * UnDecorateSymbolName (DBGHELP.@)
2186 DWORD WINAPI
UnDecorateSymbolName(const char *decorated_name
, char *undecorated_name
,
2187 DWORD undecorated_length
, DWORD flags
)
2189 TRACE("(%s, %p, %d, 0x%08x)\n",
2190 debugstr_a(decorated_name
), undecorated_name
, undecorated_length
, flags
);
2192 if (!undecorated_name
|| !undecorated_length
)
2194 if (!__unDName(undecorated_name
, decorated_name
, undecorated_length
, malloc
, free
, flags
))
2196 return strlen(undecorated_name
);
2199 /***********************************************************************
2200 * UnDecorateSymbolNameW (DBGHELP.@)
2202 DWORD WINAPI
UnDecorateSymbolNameW(const WCHAR
*decorated_name
, WCHAR
*undecorated_name
,
2203 DWORD undecorated_length
, DWORD flags
)
2208 TRACE("(%s, %p, %d, 0x%08x)\n",
2209 debugstr_w(decorated_name
), undecorated_name
, undecorated_length
, flags
);
2211 if (!undecorated_name
|| !undecorated_length
)
2214 len
= WideCharToMultiByte(CP_ACP
, 0, decorated_name
, -1, NULL
, 0, NULL
, NULL
);
2215 if ((buf
= HeapAlloc(GetProcessHeap(), 0, len
)))
2217 WideCharToMultiByte(CP_ACP
, 0, decorated_name
, -1, buf
, len
, NULL
, NULL
);
2218 if ((ptr
= __unDName(NULL
, buf
, 0, malloc
, free
, flags
)))
2220 MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, undecorated_name
, undecorated_length
);
2221 undecorated_name
[undecorated_length
- 1] = 0;
2222 ret
= lstrlenW(undecorated_name
);
2225 HeapFree(GetProcessHeap(), 0, buf
);
2231 #define WILDCHAR(x) (-(x))
2233 static int re_fetch_char(const WCHAR
** re
)
2237 case '\\': (*re
)++; return *(*re
)++;
2238 case '*': case '[': case '?': case '+': case '#': case ']': return WILDCHAR(*(*re
)++);
2239 default: return *(*re
)++;
2243 static inline int re_match_char(WCHAR ch1
, WCHAR ch2
, BOOL _case
)
2245 return _case
? ch1
- ch2
: towupper(ch1
) - towupper(ch2
);
2248 static const WCHAR
* re_match_one(const WCHAR
* string
, const WCHAR
* elt
, BOOL _case
)
2253 switch (ch1
= re_fetch_char(&elt
))
2256 return (ch1
>= 0 && re_match_char(*string
, ch1
, _case
) == 0) ? ++string
: NULL
;
2257 case WILDCHAR('?'): return *string
? ++string
: NULL
;
2258 case WILDCHAR('*'): assert(0);
2259 case WILDCHAR('['): break;
2264 ch1
= re_fetch_char(&elt
);
2265 if (ch1
== WILDCHAR(']')) return NULL
;
2266 if (state
== 1 && ch1
== '-') state
= 2;
2269 if (re_match_char(*string
, ch1
, _case
) == 0) return ++string
;
2280 if (prev
>= 0 && ch1
>= 0 && re_match_char(prev
, *string
, _case
) <= 0 &&
2281 re_match_char(*string
, ch1
, _case
) <= 0)
2290 /******************************************************************
2293 * match a substring of *pstring according to *pre regular expression
2294 * pstring and pre are only updated in case of successful match
2296 static BOOL
re_match_multi(const WCHAR
** pstring
, const WCHAR
** pre
, BOOL _case
)
2298 const WCHAR
* re_end
= *pre
;
2299 const WCHAR
* string_end
= *pstring
;
2300 const WCHAR
* re_beg
;
2301 const WCHAR
* string_beg
;
2305 while (*re_end
&& *string_end
)
2307 string_beg
= string_end
;
2309 switch (ch
= re_fetch_char(&re_end
))
2311 case WILDCHAR(']'): case WILDCHAR('+'): case WILDCHAR('#'): return FALSE
;
2313 /* transform '*' into '?#' */
2319 if (!(ch
= re_fetch_char(&re_end
))) return FALSE
;
2320 } while (ch
!= WILDCHAR(']'));
2330 if (!(next
= re_match_one(string_end
, re_beg
, _case
))) return FALSE
;
2336 while ((next
= re_match_one(string_end
, re_beg
, _case
))) string_end
= next
;
2337 for ( ; string_end
>= string_beg
; string_end
--)
2339 if (re_match_multi(&string_end
, &re_end
, _case
)) goto found
;
2343 if (!(next
= re_match_one(string_end
, re_beg
, _case
))) return FALSE
;
2348 if (*re_end
|| *string_end
) return FALSE
;
2352 *pstring
= string_end
;
2356 /******************************************************************
2357 * SymMatchStringA (DBGHELP.@)
2360 BOOL WINAPI
SymMatchStringA(PCSTR string
, PCSTR re
, BOOL _case
)
2369 SetLastError(ERROR_INVALID_HANDLE
);
2372 TRACE("%s %s %c\n", string
, re
, _case
? 'Y' : 'N');
2374 sz
= MultiByteToWideChar(CP_ACP
, 0, string
, -1, NULL
, 0);
2375 if ((strW
= HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
))))
2376 MultiByteToWideChar(CP_ACP
, 0, string
, -1, strW
, sz
);
2377 sz
= MultiByteToWideChar(CP_ACP
, 0, re
, -1, NULL
, 0);
2378 if ((reW
= HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
))))
2379 MultiByteToWideChar(CP_ACP
, 0, re
, -1, reW
, sz
);
2382 ret
= SymMatchStringW(strW
, reW
, _case
);
2383 HeapFree(GetProcessHeap(), 0, strW
);
2384 HeapFree(GetProcessHeap(), 0, reW
);
2388 /******************************************************************
2389 * SymMatchStringW (DBGHELP.@)
2392 BOOL WINAPI
SymMatchStringW(PCWSTR string
, PCWSTR re
, BOOL _case
)
2394 TRACE("%s %s %c\n", debugstr_w(string
), debugstr_w(re
), _case
? 'Y' : 'N');
2398 SetLastError(ERROR_INVALID_HANDLE
);
2401 return re_match_multi(&string
, &re
, _case
);
2404 static inline BOOL
doSymSearch(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD Index
,
2405 DWORD SymTag
, PCWSTR Mask
, DWORD64 Address
,
2406 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
2407 PVOID UserContext
, DWORD Options
)
2411 if (Options
!= SYMSEARCH_GLOBALSONLY
)
2413 FIXME("Unsupported searching with options (%x)\n", Options
);
2414 SetLastError(ERROR_INVALID_PARAMETER
);
2418 se
.cb
= EnumSymbolsCallback
;
2419 se
.user
= UserContext
;
2423 se
.sym_info
= (PSYMBOL_INFO
)se
.buffer
;
2425 return sym_enum(hProcess
, BaseOfDll
, Mask
, &se
);
2428 /******************************************************************
2429 * SymSearch (DBGHELP.@)
2431 BOOL WINAPI
SymSearch(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD Index
,
2432 DWORD SymTag
, PCSTR Mask
, DWORD64 Address
,
2433 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
2434 PVOID UserContext
, DWORD Options
)
2436 LPWSTR maskW
= NULL
;
2439 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2440 hProcess
, wine_dbgstr_longlong(BaseOfDll
), Index
, SymTag
, Mask
,
2441 wine_dbgstr_longlong(Address
), EnumSymbolsCallback
,
2442 UserContext
, Options
);
2446 DWORD sz
= MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, NULL
, 0);
2448 if (!(maskW
= HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
))))
2450 MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, maskW
, sz
);
2452 ret
= doSymSearch(hProcess
, BaseOfDll
, Index
, SymTag
, maskW
, Address
,
2453 EnumSymbolsCallback
, UserContext
, Options
);
2454 HeapFree(GetProcessHeap(), 0, maskW
);
2458 /******************************************************************
2459 * SymSearchW (DBGHELP.@)
2461 BOOL WINAPI
SymSearchW(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD Index
,
2462 DWORD SymTag
, PCWSTR Mask
, DWORD64 Address
,
2463 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback
,
2464 PVOID UserContext
, DWORD Options
)
2466 struct sym_enumW sew
;
2468 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2469 hProcess
, wine_dbgstr_longlong(BaseOfDll
), Index
, SymTag
, debugstr_w(Mask
),
2470 wine_dbgstr_longlong(Address
), EnumSymbolsCallback
,
2471 UserContext
, Options
);
2473 sew
.ctx
= UserContext
;
2474 sew
.cb
= EnumSymbolsCallback
;
2475 sew
.sym_info
= (PSYMBOL_INFOW
)sew
.buffer
;
2477 return doSymSearch(hProcess
, BaseOfDll
, Index
, SymTag
, Mask
, Address
,
2478 sym_enumW
, &sew
, Options
);
2481 /******************************************************************
2482 * SymAddSymbol (DBGHELP.@)
2485 BOOL WINAPI
SymAddSymbol(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR name
,
2486 DWORD64 addr
, DWORD size
, DWORD flags
)
2488 struct module_pair pair
;
2490 TRACE("(%p %s %s %u)\n", hProcess
, wine_dbgstr_a(name
), wine_dbgstr_longlong(addr
), size
);
2492 if (!module_init_pair(&pair
, hProcess
, BaseOfDll
)) return FALSE
;
2494 return symt_new_custom(pair
.effective
, name
, addr
, size
) != NULL
;
2497 /******************************************************************
2498 * SymAddSymbolW (DBGHELP.@)
2501 BOOL WINAPI
SymAddSymbolW(HANDLE hProcess
, ULONG64 BaseOfDll
, PCWSTR nameW
,
2502 DWORD64 addr
, DWORD size
, DWORD flags
)
2504 char name
[MAX_SYM_NAME
];
2506 TRACE("(%p %s %s %u)\n", hProcess
, wine_dbgstr_w(nameW
), wine_dbgstr_longlong(addr
), size
);
2508 WideCharToMultiByte(CP_ACP
, 0, nameW
, -1, name
, ARRAY_SIZE(name
), NULL
, NULL
);
2510 return SymAddSymbol(hProcess
, BaseOfDll
, name
, addr
, size
, flags
);
2513 /******************************************************************
2514 * SymEnumLines (DBGHELP.@)
2517 BOOL WINAPI
SymEnumLines(HANDLE hProcess
, ULONG64 base
, PCSTR compiland
,
2518 PCSTR srcfile
, PSYM_ENUMLINES_CALLBACK cb
, PVOID user
)
2520 struct module_pair pair
;
2521 struct hash_table_iter hti
;
2522 struct symt_ht
* sym
;
2524 struct line_info
* dli
;
2529 if (!cb
) return FALSE
;
2530 if (!(dbghelp_options
& SYMOPT_LOAD_LINES
)) return TRUE
;
2532 if (!module_init_pair(&pair
, hProcess
, base
)) return FALSE
;
2533 if (compiland
) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland
);
2534 if (!(srcmask
= file_regex(srcfile
))) return FALSE
;
2536 sci
.SizeOfStruct
= sizeof(sci
);
2539 hash_table_iter_init(&pair
.effective
->ht_symbols
, &hti
, NULL
);
2540 while ((ptr
= hash_table_iter_up(&hti
)))
2544 sym
= CONTAINING_RECORD(ptr
, struct symt_ht
, hash_elt
);
2545 if (sym
->symt
.tag
!= SymTagFunction
) continue;
2547 sci
.FileName
[0] = '\0';
2548 for (i
=0; i
<vector_length(&((struct symt_function
*)sym
)->vlines
); i
++)
2550 dli
= vector_at(&((struct symt_function
*)sym
)->vlines
, i
);
2551 if (dli
->is_source_file
)
2553 file
= source_get(pair
.effective
, dli
->u
.source_file
);
2554 if (!file
) sci
.FileName
[0] = '\0';
2557 DWORD sz
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
2560 if ((fileW
= HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
))))
2561 MultiByteToWideChar(CP_ACP
, 0, file
, -1, fileW
, sz
);
2562 if (SymMatchStringW(fileW
, srcmask
, FALSE
))
2563 strcpy(sci
.FileName
, file
);
2565 sci
.FileName
[0] = '\0';
2566 HeapFree(GetProcessHeap(), 0, fileW
);
2569 else if (sci
.FileName
[0])
2572 sci
.Obj
[0] = '\0'; /* FIXME */
2573 sci
.LineNumber
= dli
->line_number
;
2574 sci
.Address
= dli
->u
.address
;
2575 if (!cb(&sci
, user
)) break;
2579 HeapFree(GetProcessHeap(), 0, srcmask
);
2583 BOOL WINAPI
SymGetLineFromName(HANDLE hProcess
, PCSTR ModuleName
, PCSTR FileName
,
2584 DWORD dwLineNumber
, PLONG plDisplacement
, PIMAGEHLP_LINE Line
)
2586 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess
, ModuleName
, FileName
,
2587 dwLineNumber
, plDisplacement
, Line
);
2591 BOOL WINAPI
SymGetLineFromName64(HANDLE hProcess
, PCSTR ModuleName
, PCSTR FileName
,
2592 DWORD dwLineNumber
, PLONG lpDisplacement
, PIMAGEHLP_LINE64 Line
)
2594 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess
, ModuleName
, FileName
,
2595 dwLineNumber
, lpDisplacement
, Line
);
2599 BOOL WINAPI
SymGetLineFromNameW64(HANDLE hProcess
, PCWSTR ModuleName
, PCWSTR FileName
,
2600 DWORD dwLineNumber
, PLONG plDisplacement
, PIMAGEHLP_LINEW64 Line
)
2602 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess
, debugstr_w(ModuleName
), debugstr_w(FileName
),
2603 dwLineNumber
, plDisplacement
, Line
);
2607 /******************************************************************
2608 * SymFromIndex (DBGHELP.@)
2611 BOOL WINAPI
SymFromIndex(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD index
, PSYMBOL_INFO symbol
)
2613 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2614 hProcess
, wine_dbgstr_longlong(BaseOfDll
), index
, symbol
);
2619 /******************************************************************
2620 * SymFromIndexW (DBGHELP.@)
2623 BOOL WINAPI
SymFromIndexW(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD index
, PSYMBOL_INFOW symbol
)
2625 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2626 hProcess
, wine_dbgstr_longlong(BaseOfDll
), index
, symbol
);
2631 /******************************************************************
2632 * SymSetHomeDirectory (DBGHELP.@)
2635 PCHAR WINAPI
SymSetHomeDirectory(HANDLE hProcess
, PCSTR dir
)
2637 FIXME("(%p, %s): stub\n", hProcess
, dir
);
2642 /******************************************************************
2643 * SymSetHomeDirectoryW (DBGHELP.@)
2646 PWSTR WINAPI
SymSetHomeDirectoryW(HANDLE hProcess
, PCWSTR dir
)
2648 FIXME("(%p, %s): stub\n", hProcess
, debugstr_w(dir
));
2653 /******************************************************************
2654 * SymFromInlineContext (DBGHELP.@)
2657 BOOL WINAPI
SymFromInlineContext(HANDLE hProcess
, DWORD64 addr
, ULONG inline_ctx
, PDWORD64 disp
, PSYMBOL_INFO si
)
2659 struct module_pair pair
;
2660 struct symt_inlinesite
* inlined
;
2662 TRACE("(%p, %#I64x, 0x%x, %p, %p)\n", hProcess
, addr
, inline_ctx
, disp
, si
);
2664 switch (IFC_MODE(inline_ctx
))
2666 case IFC_MODE_IGNORE
:
2667 case IFC_MODE_REGULAR
:
2668 return SymFromAddr(hProcess
, addr
, disp
, si
);
2669 case IFC_MODE_INLINE
:
2670 if (!module_init_pair(&pair
, hProcess
, addr
)) return FALSE
;
2671 inlined
= symt_find_inlined_site(pair
.effective
, addr
, inline_ctx
);
2674 symt_fill_sym_info(&pair
, NULL
, &inlined
->func
.symt
, si
);
2675 *disp
= addr
- inlined
->func
.address
;
2680 SetLastError(ERROR_INVALID_PARAMETER
);
2685 /******************************************************************
2686 * SymFromInlineContextW (DBGHELP.@)
2689 BOOL WINAPI
SymFromInlineContextW(HANDLE hProcess
, DWORD64 addr
, ULONG inline_ctx
, PDWORD64 disp
, PSYMBOL_INFOW siW
)
2695 TRACE("(%p, %#I64x, 0x%x, %p, %p)\n", hProcess
, addr
, inline_ctx
, disp
, siW
);
2697 len
= sizeof(*si
) + siW
->MaxNameLen
* sizeof(WCHAR
);
2698 si
= HeapAlloc(GetProcessHeap(), 0, len
);
2699 if (!si
) return FALSE
;
2701 si
->SizeOfStruct
= sizeof(*si
);
2702 si
->MaxNameLen
= siW
->MaxNameLen
;
2703 if ((ret
= SymFromInlineContext(hProcess
, addr
, inline_ctx
, disp
, si
)))
2705 copy_symbolW(siW
, si
);
2707 HeapFree(GetProcessHeap(), 0, si
);
2711 static BOOL
get_line_from_inline_context(HANDLE hProcess
, DWORD64 addr
, ULONG inline_ctx
, DWORD64 mod_addr
, PDWORD disp
,
2712 struct internal_line_t
* intl
)
2714 struct module_pair pair
;
2715 struct symt_inlinesite
* inlined
;
2717 if (!module_init_pair(&pair
, hProcess
, mod_addr
? mod_addr
: addr
)) return FALSE
;
2718 switch (IFC_MODE(inline_ctx
))
2720 case IFC_MODE_INLINE
:
2721 inlined
= symt_find_inlined_site(pair
.effective
, addr
, inline_ctx
);
2722 if (inlined
&& get_line_from_function(&pair
, &inlined
->func
, addr
, disp
, intl
))
2724 /* fall through: check if we can find line info at top function level */
2725 case IFC_MODE_IGNORE
:
2726 case IFC_MODE_REGULAR
:
2727 return get_line_from_addr(hProcess
, addr
, disp
, intl
);
2729 SetLastError(ERROR_INVALID_PARAMETER
);
2734 /******************************************************************
2735 * SymGetLineFromInlineContext (DBGHELP.@)
2738 BOOL WINAPI
SymGetLineFromInlineContext(HANDLE hProcess
, DWORD64 addr
, ULONG inline_ctx
, DWORD64 mod_addr
, PDWORD disp
, PIMAGEHLP_LINE64 line
)
2740 struct internal_line_t intl
;
2742 TRACE("(%p, %#I64x, 0x%x, %#I64x, %p, %p)\n",
2743 hProcess
, addr
, inline_ctx
, mod_addr
, disp
, line
);
2745 if (line
->SizeOfStruct
< sizeof(*line
)) return FALSE
;
2746 init_internal_line(&intl
, FALSE
);
2748 if (!get_line_from_inline_context(hProcess
, addr
, inline_ctx
, mod_addr
, disp
, &intl
)) return FALSE
;
2749 return internal_line_copy_toA64(&intl
, line
);
2752 /******************************************************************
2753 * SymGetLineFromInlineContextW (DBGHELP.@)
2756 BOOL WINAPI
SymGetLineFromInlineContextW(HANDLE hProcess
, DWORD64 addr
, ULONG inline_ctx
, DWORD64 mod_addr
, PDWORD disp
, PIMAGEHLP_LINEW64 line
)
2758 struct internal_line_t intl
;
2760 TRACE("(%p, %#I64x, 0x%x, %#I64x, %p, %p)\n",
2761 hProcess
, addr
, inline_ctx
, mod_addr
, disp
, line
);
2763 if (line
->SizeOfStruct
< sizeof(*line
)) return FALSE
;
2764 init_internal_line(&intl
, TRUE
);
2766 if (!get_line_from_inline_context(hProcess
, addr
, inline_ctx
, mod_addr
, disp
, &intl
)) return FALSE
;
2767 return internal_line_copy_toW64(&intl
, line
);