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
23 #define NONAMELESSSTRUCT
31 #include <sys/types.h>
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt
);
44 static inline int cmp_addr(ULONG64 a1
, ULONG64 a2
)
46 if (a1
> a2
) return 1;
47 if (a1
< a2
) return -1;
51 static inline int cmp_sorttab_addr(struct module
* module
, int idx
, ULONG64 addr
)
54 symt_get_address(&module
->addr_sorttab
[idx
]->symt
, &ref
);
55 return cmp_addr(ref
, addr
);
58 int symt_cmp_addr(const void* p1
, const void* p2
)
60 const struct symt
* sym1
= *(const struct symt
* const *)p1
;
61 const struct symt
* sym2
= *(const struct symt
* const *)p2
;
64 symt_get_address(sym1
, &a1
);
65 symt_get_address(sym2
, &a2
);
66 return cmp_addr(a1
, a2
);
69 DWORD
symt_ptr2index(struct module
* module
, const struct symt
* sym
)
72 const struct symt
** c
;
73 int len
= vector_length(&module
->vsymt
), i
;
75 /* FIXME: this is inefficient */
76 for (i
= 0; i
< len
; i
++)
78 if (*(struct symt
**)vector_at(&module
->vsymt
, i
) == sym
)
82 c
= vector_add(&module
->vsymt
, &module
->pool
);
90 struct symt
* symt_index2ptr(struct module
* module
, DWORD id
)
93 if (!id
-- || id
>= vector_length(&module
->vsymt
)) return NULL
;
94 return *(struct symt
**)vector_at(&module
->vsymt
, id
);
96 return (struct symt
*)id
;
100 static BOOL
symt_grow_sorttab(struct module
* module
, unsigned sz
)
102 struct symt_ht
** new;
105 if (sz
<= module
->sorttab_size
) return TRUE
;
106 if (module
->addr_sorttab
)
108 size
= module
->sorttab_size
* 2;
109 new = HeapReAlloc(GetProcessHeap(), 0, module
->addr_sorttab
,
110 size
* sizeof(struct symt_ht
*));
115 new = HeapAlloc(GetProcessHeap(), 0, size
* sizeof(struct symt_ht
*));
117 if (!new) return FALSE
;
118 module
->sorttab_size
= size
;
119 module
->addr_sorttab
= new;
123 static void symt_add_module_ht(struct module
* module
, struct symt_ht
* ht
)
127 hash_table_add(&module
->ht_symbols
, &ht
->hash_elt
);
128 /* Don't store in sorttab a symbol without address, they are of
129 * no use here (e.g. constant values)
131 if (symt_get_address(&ht
->symt
, &addr
) &&
132 symt_grow_sorttab(module
, module
->num_symbols
+ 1))
134 module
->addr_sorttab
[module
->num_symbols
++] = ht
;
135 module
->sortlist_valid
= FALSE
;
141 /* transforms a dbghelp's regular expression into a POSIX one
142 * Here are the valid dbghelp reg ex characters:
143 * * 0 or more characters
144 * ? a single character
146 * # 0 or more of preceding char
147 * + 1 or more of preceding char
148 * escapes \ on #, ?, [, ], *, +. don't work on -
150 static void compile_regex(const char* str
, int numchar
, regex_t
* re
, BOOL _case
)
153 BOOL in_escape
= FALSE
;
154 unsigned flags
= REG_NOSUB
;
156 if (numchar
== -1) numchar
= strlen( str
);
158 p
= mask
= HeapAlloc( GetProcessHeap(), 0, 2 * numchar
+ 3 );
161 while (*str
&& numchar
--)
163 /* FIXME: this shouldn't be valid on '-' */
172 case '\\': in_escape
= TRUE
; break;
173 case '*': *p
++ = '.'; *p
++ = '*'; break;
174 case '?': *p
++ = '.'; break;
175 case '#': *p
++ = '*'; break;
176 /* escape some valid characters in dbghelp reg exp:s */
177 case '$': *p
++ = '\\'; *p
++ = '$'; break;
178 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
179 default: *p
++ = *str
; break;
190 if (_case
) flags
|= REG_ICASE
;
191 if (regcomp(re
, mask
, flags
)) FIXME("Couldn't compile %s\n", mask
);
192 HeapFree(GetProcessHeap(), 0, mask
);
195 static BOOL
compile_file_regex(regex_t
* re
, const char* srcfile
)
200 if (!srcfile
|| !*srcfile
) return regcomp(re
, ".*", REG_NOSUB
);
202 p
= mask
= HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile
) + 4);
232 ret
= !regcomp(re
, mask
, REG_NOSUB
);
233 HeapFree(GetProcessHeap(), 0, mask
);
236 FIXME("Couldn't compile %s\n", mask
);
237 SetLastError(ERROR_INVALID_PARAMETER
);
242 static int match_regexp( const regex_t
*re
, const char *str
)
244 return !regexec( re
, str
, 0, NULL
, 0 );
247 #else /* HAVE_REGEX_H */
249 /* if we don't have regexp support, fall back to a simple string comparison */
257 static void compile_regex(const char* str
, int numchar
, regex_t
* re
, BOOL _case
)
259 if (numchar
== -1) numchar
= strlen( str
);
261 re
->str
= HeapAlloc( GetProcessHeap(), 0, numchar
+ 1 );
262 memcpy( re
->str
, str
, numchar
);
263 re
->str
[numchar
] = 0;
267 static BOOL
compile_file_regex(regex_t
* re
, const char* srcfile
)
269 if (!srcfile
|| !*srcfile
) re
->str
= NULL
;
270 else compile_regex( srcfile
, -1, re
, FALSE
);
274 static int match_regexp( const regex_t
*re
, const char *str
)
276 if (!re
->str
) return 1;
277 if (re
->icase
) return !lstrcmpiA( re
->str
, str
);
278 return !strcmp( re
->str
, str
);
281 static void regfree( regex_t
*re
)
283 HeapFree( GetProcessHeap(), 0, re
->str
);
286 #endif /* HAVE_REGEX_H */
288 struct symt_compiland
* symt_new_compiland(struct module
* module
,
289 unsigned long address
, unsigned src_idx
)
291 struct symt_compiland
* sym
;
293 TRACE_(dbghelp_symt
)("Adding compiland symbol %s:%s\n",
294 debugstr_w(module
->module
.ModuleName
), source_get(module
, src_idx
));
295 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
297 sym
->symt
.tag
= SymTagCompiland
;
298 sym
->address
= address
;
299 sym
->source
= src_idx
;
300 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 32);
305 struct symt_public
* symt_new_public(struct module
* module
,
306 struct symt_compiland
* compiland
,
308 unsigned long address
, unsigned size
)
310 struct symt_public
* sym
;
313 TRACE_(dbghelp_symt
)("Adding public symbol %s:%s @%lx\n",
314 debugstr_w(module
->module
.ModuleName
), name
, address
);
315 if ((dbghelp_options
& SYMOPT_AUTO_PUBLICS
) &&
316 symt_find_nearest(module
, address
) != NULL
)
318 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
320 sym
->symt
.tag
= SymTagPublicSymbol
;
321 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
322 sym
->container
= compiland
? &compiland
->symt
: NULL
;
323 sym
->address
= address
;
325 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
328 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
335 struct symt_data
* symt_new_global_variable(struct module
* module
,
336 struct symt_compiland
* compiland
,
337 const char* name
, unsigned is_static
,
338 struct location loc
, unsigned long size
,
341 struct symt_data
* sym
;
345 TRACE_(dbghelp_symt
)("Adding global symbol %s:%s %d@%lx %p\n",
346 debugstr_w(module
->module
.ModuleName
), name
, loc
.kind
, loc
.offset
, type
);
347 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
349 sym
->symt
.tag
= SymTagData
;
350 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
351 sym
->kind
= is_static
? DataIsFileStatic
: DataIsGlobal
;
352 sym
->container
= compiland
? &compiland
->symt
: NULL
;
355 if (type
&& size
&& symt_get_info(module
, type
, TI_GET_LENGTH
, &tsz
))
358 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
359 debugstr_w(module
->module
.ModuleName
), name
,
360 wine_dbgstr_longlong(tsz
), size
);
362 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
365 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
372 struct symt_function
* symt_new_function(struct module
* module
,
373 struct symt_compiland
* compiland
,
375 unsigned long addr
, unsigned long size
,
376 struct symt
* sig_type
)
378 struct symt_function
* sym
;
381 TRACE_(dbghelp_symt
)("Adding global function %s:%s @%lx-%lx\n",
382 debugstr_w(module
->module
.ModuleName
), name
, addr
, addr
+ size
- 1);
384 assert(!sig_type
|| sig_type
->tag
== SymTagFunctionType
);
385 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
387 sym
->symt
.tag
= SymTagFunction
;
388 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
389 sym
->container
= &compiland
->symt
;
391 sym
->type
= sig_type
;
393 vector_init(&sym
->vlines
, sizeof(struct line_info
), 64);
394 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
395 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
398 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
405 void symt_add_func_line(struct module
* module
, struct symt_function
* func
,
406 unsigned source_idx
, int line_num
, unsigned long offset
)
408 struct line_info
* dli
;
409 BOOL last_matches
= FALSE
;
412 if (func
== NULL
|| !(dbghelp_options
& SYMOPT_LOAD_LINES
)) return;
414 TRACE_(dbghelp_symt
)("(%p)%s:%lx %s:%u\n",
415 func
, func
->hash_elt
.name
, offset
,
416 source_get(module
, source_idx
), line_num
);
418 assert(func
->symt
.tag
== SymTagFunction
);
420 for (i
=vector_length(&func
->vlines
)-1; i
>=0; i
--)
422 dli
= vector_at(&func
->vlines
, i
);
423 if (dli
->is_source_file
)
425 last_matches
= (source_idx
== dli
->u
.source_file
);
432 /* we shouldn't have line changes on first line of function */
433 dli
= vector_add(&func
->vlines
, &module
->pool
);
434 dli
->is_source_file
= 1;
435 dli
->is_first
= dli
->is_last
= 0;
436 dli
->line_number
= 0;
437 dli
->u
.source_file
= source_idx
;
439 dli
= vector_add(&func
->vlines
, &module
->pool
);
440 dli
->is_source_file
= 0;
441 dli
->is_first
= dli
->is_last
= 0;
442 dli
->line_number
= line_num
;
443 dli
->u
.pc_offset
= func
->address
+ offset
;
446 /******************************************************************
447 * symt_add_func_local
449 * Adds a new local/parameter to a given function:
450 * In any cases, dt tells whether it's a local variable or a parameter
451 * If regno it's not 0:
452 * - then variable is stored in a register
453 * - otherwise, value is referenced by register + offset
454 * Otherwise, the variable is stored on the stack:
455 * - offset is then the offset from the frame register
457 struct symt_data
* symt_add_func_local(struct module
* module
,
458 struct symt_function
* func
,
460 const struct location
* loc
,
461 struct symt_block
* block
,
462 struct symt
* type
, const char* name
)
464 struct symt_data
* locsym
;
467 TRACE_(dbghelp_symt
)("Adding local symbol (%s:%s): %s %p\n",
468 debugstr_w(module
->module
.ModuleName
), func
->hash_elt
.name
,
472 assert(func
->symt
.tag
== SymTagFunction
);
473 assert(dt
== DataIsParam
|| dt
== DataIsLocal
);
475 locsym
= pool_alloc(&module
->pool
, sizeof(*locsym
));
476 locsym
->symt
.tag
= SymTagData
;
477 locsym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
478 locsym
->hash_elt
.next
= NULL
;
480 locsym
->container
= block
? &block
->symt
: &func
->symt
;
482 locsym
->u
.var
= *loc
;
484 p
= vector_add(&block
->vchildren
, &module
->pool
);
486 p
= vector_add(&func
->vchildren
, &module
->pool
);
492 struct symt_block
* symt_open_func_block(struct module
* module
,
493 struct symt_function
* func
,
494 struct symt_block
* parent_block
,
495 unsigned pc
, unsigned len
)
497 struct symt_block
* block
;
501 assert(func
->symt
.tag
== SymTagFunction
);
503 assert(!parent_block
|| parent_block
->symt
.tag
== SymTagBlock
);
504 block
= pool_alloc(&module
->pool
, sizeof(*block
));
505 block
->symt
.tag
= SymTagBlock
;
506 block
->address
= func
->address
+ pc
;
508 block
->container
= parent_block
? &parent_block
->symt
: &func
->symt
;
509 vector_init(&block
->vchildren
, sizeof(struct symt
*), 4);
511 p
= vector_add(&parent_block
->vchildren
, &module
->pool
);
513 p
= vector_add(&func
->vchildren
, &module
->pool
);
519 struct symt_block
* symt_close_func_block(struct module
* module
,
520 const struct symt_function
* func
,
521 struct symt_block
* block
, unsigned pc
)
524 assert(func
->symt
.tag
== SymTagFunction
);
526 if (pc
) block
->size
= func
->address
+ pc
- block
->address
;
527 return (block
->container
->tag
== SymTagBlock
) ?
528 GET_ENTRY(block
->container
, struct symt_block
, symt
) : NULL
;
531 struct symt_hierarchy_point
* symt_add_function_point(struct module
* module
,
532 struct symt_function
* func
,
533 enum SymTagEnum point
,
534 const struct location
* loc
,
537 struct symt_hierarchy_point
*sym
;
540 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
542 sym
->symt
.tag
= point
;
543 sym
->parent
= &func
->symt
;
545 sym
->hash_elt
.name
= name
? pool_strdup(&module
->pool
, name
) : NULL
;
546 p
= vector_add(&func
->vchildren
, &module
->pool
);
552 BOOL
symt_normalize_function(struct module
* module
, const struct symt_function
* func
)
555 struct line_info
* dli
;
558 /* We aren't adding any more locals or line numbers to this function.
559 * Free any spare memory that we might have allocated.
561 assert(func
->symt
.tag
== SymTagFunction
);
563 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
564 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
566 len
= vector_length(&func
->vlines
);
569 dli
= vector_at(&func
->vlines
, 0); dli
->is_first
= 1;
570 dli
= vector_at(&func
->vlines
, len
); dli
->is_last
= 1;
575 struct symt_thunk
* symt_new_thunk(struct module
* module
,
576 struct symt_compiland
* compiland
,
577 const char* name
, THUNK_ORDINAL ord
,
578 unsigned long addr
, unsigned long size
)
580 struct symt_thunk
* sym
;
582 TRACE_(dbghelp_symt
)("Adding global thunk %s:%s @%lx-%lx\n",
583 debugstr_w(module
->module
.ModuleName
), name
, addr
, addr
+ size
- 1);
585 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
587 sym
->symt
.tag
= SymTagThunk
;
588 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
589 sym
->container
= &compiland
->symt
;
593 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
597 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
604 struct symt_data
* symt_new_constant(struct module
* module
,
605 struct symt_compiland
* compiland
,
606 const char* name
, struct symt
* type
,
609 struct symt_data
* sym
;
611 TRACE_(dbghelp_symt
)("Adding constant value %s:%s\n",
612 debugstr_w(module
->module
.ModuleName
), name
);
614 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
616 sym
->symt
.tag
= SymTagData
;
617 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
618 sym
->kind
= DataIsConstant
;
619 sym
->container
= compiland
? &compiland
->symt
: NULL
;
622 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
626 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
633 struct symt_hierarchy_point
* symt_new_label(struct module
* module
,
634 struct symt_compiland
* compiland
,
635 const char* name
, unsigned long address
)
637 struct symt_hierarchy_point
* sym
;
639 TRACE_(dbghelp_symt
)("Adding global label value %s:%s\n",
640 debugstr_w(module
->module
.ModuleName
), name
);
642 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
644 sym
->symt
.tag
= SymTagLabel
;
645 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
646 sym
->loc
.kind
= loc_absolute
;
647 sym
->loc
.offset
= address
;
648 sym
->parent
= compiland
? &compiland
->symt
: NULL
;
649 symt_add_module_ht(module
, (struct symt_ht
*)sym
);
653 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
660 /* expect sym_info->MaxNameLen to be set before being called */
661 static void symt_fill_sym_info(struct module_pair
* pair
,
662 const struct symt_function
* func
,
663 const struct symt
* sym
, SYMBOL_INFO
* sym_info
)
668 if (!symt_get_info(pair
->effective
, sym
, TI_GET_TYPE
, &sym_info
->TypeIndex
))
669 sym_info
->TypeIndex
= 0;
670 sym_info
->info
= symt_ptr2index(pair
->effective
, sym
);
671 sym_info
->Reserved
[0] = sym_info
->Reserved
[1] = 0;
672 if (!symt_get_info(pair
->effective
, sym
, TI_GET_LENGTH
, &size
) &&
673 (!sym_info
->TypeIndex
||
674 !symt_get_info(pair
->effective
, symt_index2ptr(pair
->effective
, sym_info
->TypeIndex
),
675 TI_GET_LENGTH
, &size
)))
677 sym_info
->Size
= (DWORD
)size
;
678 sym_info
->ModBase
= pair
->requested
->module
.BaseOfImage
;
686 const struct symt_data
* data
= (const struct symt_data
*)sym
;
690 sym_info
->Flags
|= SYMFLAG_PARAMETER
;
694 struct location loc
= data
->u
.var
;
696 if (loc
.kind
>= loc_user
)
699 struct module_format
* modfmt
;
701 for (i
= 0; i
< DFI_LAST
; i
++)
703 modfmt
= pair
->effective
->format_info
[i
];
704 if (modfmt
&& modfmt
->loc_compute
)
706 modfmt
->loc_compute(pair
->pcs
, modfmt
, func
, &loc
);
714 /* for now we report error cases as a negative register number */
715 sym_info
->Flags
|= SYMFLAG_LOCAL
;
718 sym_info
->Flags
|= SYMFLAG_REGISTER
;
719 sym_info
->Register
= loc
.reg
;
720 sym_info
->Address
= 0;
723 sym_info
->Flags
|= SYMFLAG_LOCAL
| SYMFLAG_REGREL
;
724 /* FIXME: it's i386 dependent !!! */
725 sym_info
->Register
= loc
.reg
? loc
.reg
: CV_REG_EBP
;
726 sym_info
->Address
= loc
.offset
;
729 sym_info
->Flags
|= SYMFLAG_VALUEPRESENT
;
730 sym_info
->Value
= loc
.offset
;
733 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc
.kind
);
739 case DataIsFileStatic
:
740 switch (data
->u
.var
.kind
)
743 sym_info
->Flags
|= SYMFLAG_TLSREL
;
746 symt_get_address(sym
, &sym_info
->Address
);
747 sym_info
->Register
= 0;
750 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data
->u
.var
.kind
);
755 sym_info
->Flags
|= SYMFLAG_VALUEPRESENT
;
756 switch (data
->u
.value
.n1
.n2
.vt
)
758 case VT_I4
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.lVal
; break;
759 case VT_I2
: sym_info
->Value
= (ULONG
)(long)data
->u
.value
.n1
.n2
.n3
.iVal
; break;
760 case VT_I1
: sym_info
->Value
= (ULONG
)(long)data
->u
.value
.n1
.n2
.n3
.cVal
; break;
761 case VT_UI4
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.ulVal
; break;
762 case VT_UI2
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.uiVal
; break;
763 case VT_UI1
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.bVal
; break;
764 case VT_I1
| VT_BYREF
: sym_info
->Value
= (ULONG64
)(DWORD_PTR
)data
->u
.value
.n1
.n2
.n3
.byref
; break;
765 case VT_EMPTY
: sym_info
->Value
= 0; break;
767 FIXME("Unsupported variant type (%u)\n", data
->u
.value
.n1
.n2
.vt
);
773 FIXME("Unhandled kind (%u) in sym data\n", data
->kind
);
777 case SymTagPublicSymbol
:
778 sym_info
->Flags
|= SYMFLAG_EXPORT
;
779 symt_get_address(sym
, &sym_info
->Address
);
782 sym_info
->Flags
|= SYMFLAG_FUNCTION
;
783 symt_get_address(sym
, &sym_info
->Address
);
786 sym_info
->Flags
|= SYMFLAG_THUNK
;
787 symt_get_address(sym
, &sym_info
->Address
);
790 symt_get_address(sym
, &sym_info
->Address
);
791 sym_info
->Register
= 0;
794 sym_info
->Scope
= 0; /* FIXME */
795 sym_info
->Tag
= sym
->tag
;
796 name
= symt_get_name(sym
);
797 if (sym_info
->MaxNameLen
)
799 if (sym
->tag
!= SymTagPublicSymbol
|| !(dbghelp_options
& SYMOPT_UNDNAME
) ||
800 (sym_info
->NameLen
= UnDecorateSymbolName(name
, sym_info
->Name
,
801 sym_info
->MaxNameLen
, UNDNAME_NAME_ONLY
) == 0))
803 sym_info
->NameLen
= min(strlen(name
), sym_info
->MaxNameLen
- 1);
804 memcpy(sym_info
->Name
, name
, sym_info
->NameLen
);
805 sym_info
->Name
[sym_info
->NameLen
] = '\0';
808 TRACE_(dbghelp_symt
)("%p => %s %u %s\n",
809 sym
, sym_info
->Name
, sym_info
->Size
,
810 wine_dbgstr_longlong(sym_info
->Address
));
815 PSYM_ENUMERATESYMBOLS_CALLBACK cb
;
817 SYMBOL_INFO
* sym_info
;
821 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
824 static BOOL
send_symbol(const struct sym_enum
* se
, struct module_pair
* pair
,
825 const struct symt_function
* func
, const struct symt
* sym
)
827 symt_fill_sym_info(pair
, func
, sym
, se
->sym_info
);
828 if (se
->index
&& se
->sym_info
->info
!= se
->index
) return FALSE
;
829 if (se
->tag
&& se
->sym_info
->Tag
!= se
->tag
) return FALSE
;
830 if (se
->addr
&& !(se
->addr
>= se
->sym_info
->Address
&& se
->addr
< se
->sym_info
->Address
+ se
->sym_info
->Size
)) return FALSE
;
831 return !se
->cb(se
->sym_info
, se
->sym_info
->Size
, se
->user
);
834 static BOOL
symt_enum_module(struct module_pair
* pair
, const regex_t
* regex
,
835 const struct sym_enum
* se
)
838 struct symt_ht
* sym
= NULL
;
839 struct hash_table_iter hti
;
841 hash_table_iter_init(&pair
->effective
->ht_symbols
, &hti
, NULL
);
842 while ((ptr
= hash_table_iter_up(&hti
)))
844 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
845 if (sym
->hash_elt
.name
&& match_regexp(regex
, sym
->hash_elt
.name
))
847 se
->sym_info
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
848 se
->sym_info
->MaxNameLen
= sizeof(se
->buffer
) - sizeof(SYMBOL_INFO
);
849 if (send_symbol(se
, pair
, NULL
, &sym
->symt
)) return TRUE
;
855 static inline unsigned where_to_insert(struct module
* module
, unsigned high
, const struct symt_ht
* elt
)
857 unsigned low
= 0, mid
= high
/ 2;
861 symt_get_address(&elt
->symt
, &addr
);
864 switch (cmp_sorttab_addr(module
, mid
, addr
))
867 case -1: low
= mid
+ 1; break;
868 case 1: high
= mid
; break;
870 mid
= low
+ (high
- low
) / 2;
871 } while (low
< high
);
875 /***********************************************************************
878 * Rebuild sorted list of symbols for a module.
880 static BOOL
resort_symbols(struct module
* module
)
884 if (!(module
->module
.NumSyms
= module
->num_symbols
))
887 /* we know that set from 0 up to num_sorttab is already sorted
888 * so sort the remaining (new) symbols, and merge the two sets
889 * (unless the first set is empty)
891 delta
= module
->num_symbols
- module
->num_sorttab
;
892 qsort(&module
->addr_sorttab
[module
->num_sorttab
], delta
, sizeof(struct symt_ht
*), symt_cmp_addr
);
893 if (module
->num_sorttab
)
895 int i
, ins_idx
= module
->num_sorttab
, prev_ins_idx
;
896 static struct symt_ht
** tmp
;
897 static unsigned num_tmp
;
901 static struct symt_ht
** new;
903 new = HeapReAlloc(GetProcessHeap(), 0, tmp
, delta
* sizeof(struct symt_ht
*));
905 new = HeapAlloc(GetProcessHeap(), 0, delta
* sizeof(struct symt_ht
*));
908 module
->num_sorttab
= 0;
909 return resort_symbols(module
);
914 memcpy(tmp
, &module
->addr_sorttab
[module
->num_sorttab
], delta
* sizeof(struct symt_ht
*));
915 qsort(tmp
, delta
, sizeof(struct symt_ht
*), symt_cmp_addr
);
917 for (i
= delta
- 1; i
>= 0; i
--)
919 prev_ins_idx
= ins_idx
;
920 ins_idx
= where_to_insert(module
, ins_idx
, tmp
[i
]);
921 memmove(&module
->addr_sorttab
[ins_idx
+ i
+ 1],
922 &module
->addr_sorttab
[ins_idx
],
923 (prev_ins_idx
- ins_idx
) * sizeof(struct symt_ht
*));
924 module
->addr_sorttab
[ins_idx
+ i
] = tmp
[i
];
927 module
->num_sorttab
= module
->num_symbols
;
928 return module
->sortlist_valid
= TRUE
;
931 static void symt_get_length(struct module
* module
, const struct symt
* symt
, ULONG64
* size
)
935 if (symt_get_info(module
, symt
, TI_GET_LENGTH
, size
) && *size
)
938 if (symt_get_info(module
, symt
, TI_GET_TYPE
, &type_index
) &&
939 symt_get_info(module
, symt_index2ptr(module
, type_index
), TI_GET_LENGTH
, size
)) return;
940 *size
= 0x1000; /* arbitrary value */
943 /* assume addr is in module */
944 struct symt_ht
* symt_find_nearest(struct module
* module
, DWORD_PTR addr
)
947 ULONG64 ref_addr
, ref_size
;
949 if (!module
->sortlist_valid
|| !module
->addr_sorttab
)
951 if (!resort_symbols(module
)) return NULL
;
955 * Binary search to find closest symbol.
958 high
= module
->num_sorttab
;
960 symt_get_address(&module
->addr_sorttab
[0]->symt
, &ref_addr
);
961 if (addr
< ref_addr
) return NULL
;
964 symt_get_address(&module
->addr_sorttab
[high
- 1]->symt
, &ref_addr
);
965 symt_get_length(module
, &module
->addr_sorttab
[high
- 1]->symt
, &ref_size
);
966 if (addr
>= ref_addr
+ ref_size
) return NULL
;
969 while (high
> low
+ 1)
971 mid
= (high
+ low
) / 2;
972 if (cmp_sorttab_addr(module
, mid
, addr
) < 0)
977 if (low
!= high
&& high
!= module
->num_sorttab
&&
978 cmp_sorttab_addr(module
, high
, addr
) <= 0)
981 /* If found symbol is a public symbol, check if there are any other entries that
982 * might also have the same address, but would get better information
984 if (module
->addr_sorttab
[low
]->symt
.tag
== SymTagPublicSymbol
)
986 symt_get_address(&module
->addr_sorttab
[low
]->symt
, &ref_addr
);
988 module
->addr_sorttab
[low
- 1]->symt
.tag
!= SymTagPublicSymbol
&&
989 !cmp_sorttab_addr(module
, low
- 1, ref_addr
))
991 else if (low
< module
->num_sorttab
- 1 &&
992 module
->addr_sorttab
[low
+ 1]->symt
.tag
!= SymTagPublicSymbol
&&
993 !cmp_sorttab_addr(module
, low
+ 1, ref_addr
))
996 /* finally check that we fit into the found symbol */
997 symt_get_address(&module
->addr_sorttab
[low
]->symt
, &ref_addr
);
998 if (addr
< ref_addr
) return NULL
;
999 symt_get_length(module
, &module
->addr_sorttab
[low
]->symt
, &ref_size
);
1000 if (addr
>= ref_addr
+ ref_size
) return NULL
;
1002 return module
->addr_sorttab
[low
];
1005 static BOOL
symt_enum_locals_helper(struct module_pair
* pair
,
1006 regex_t
* preg
, const struct sym_enum
* se
,
1007 struct symt_function
* func
, const struct vector
* v
)
1009 struct symt
* lsym
= NULL
;
1010 DWORD pc
= pair
->pcs
->ctx_frame
.InstructionOffset
;
1013 for (i
=0; i
<vector_length(v
); i
++)
1015 lsym
= *(struct symt
**)vector_at(v
, i
);
1020 struct symt_block
* block
= (struct symt_block
*)lsym
;
1021 if (pc
< block
->address
|| block
->address
+ block
->size
<= pc
)
1023 if (!symt_enum_locals_helper(pair
, preg
, se
, func
, &block
->vchildren
))
1028 if (match_regexp(preg
, symt_get_name(lsym
)))
1030 if (send_symbol(se
, pair
, func
, lsym
)) return FALSE
;
1034 case SymTagFuncDebugStart
:
1035 case SymTagFuncDebugEnd
:
1039 FIXME("Unknown type: %u (%x)\n", lsym
->tag
, lsym
->tag
);
1046 static BOOL
symt_enum_locals(struct process
* pcs
, const char* mask
,
1047 const struct sym_enum
* se
)
1049 struct module_pair pair
;
1050 struct symt_ht
* sym
;
1051 DWORD_PTR pc
= pcs
->ctx_frame
.InstructionOffset
;
1053 se
->sym_info
->SizeOfStruct
= sizeof(*se
->sym_info
);
1054 se
->sym_info
->MaxNameLen
= sizeof(se
->buffer
) - sizeof(SYMBOL_INFO
);
1057 pair
.requested
= module_find_by_addr(pair
.pcs
, pc
, DMT_UNKNOWN
);
1058 if (!module_get_debug(&pair
)) return FALSE
;
1059 if ((sym
= symt_find_nearest(pair
.effective
, pc
)) == NULL
) return FALSE
;
1061 if (sym
->symt
.tag
== SymTagFunction
)
1066 compile_regex(mask
? mask
: "*", -1, &preg
,
1067 dbghelp_options
& SYMOPT_CASE_INSENSITIVE
);
1068 ret
= symt_enum_locals_helper(&pair
, &preg
, se
, (struct symt_function
*)sym
,
1069 &((struct symt_function
*)sym
)->vchildren
);
1076 /******************************************************************
1079 * Helper for transforming an ANSI symbol info into a UNICODE one.
1080 * Assume that MaxNameLen is the same for both version (A & W).
1082 void copy_symbolW(SYMBOL_INFOW
* siw
, const SYMBOL_INFO
* si
)
1084 siw
->SizeOfStruct
= si
->SizeOfStruct
;
1085 siw
->TypeIndex
= si
->TypeIndex
;
1086 siw
->Reserved
[0] = si
->Reserved
[0];
1087 siw
->Reserved
[1] = si
->Reserved
[1];
1088 siw
->Index
= si
->info
; /* FIXME: see dbghelp.h */
1089 siw
->Size
= si
->Size
;
1090 siw
->ModBase
= si
->ModBase
;
1091 siw
->Flags
= si
->Flags
;
1092 siw
->Value
= si
->Value
;
1093 siw
->Address
= si
->Address
;
1094 siw
->Register
= si
->Register
;
1095 siw
->Scope
= si
->Scope
;
1097 siw
->NameLen
= si
->NameLen
;
1098 siw
->MaxNameLen
= si
->MaxNameLen
;
1099 MultiByteToWideChar(CP_ACP
, 0, si
->Name
, -1, siw
->Name
, siw
->MaxNameLen
);
1102 /******************************************************************
1105 * Core routine for most of the enumeration of symbols
1107 static BOOL
sym_enum(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR Mask
,
1108 const struct sym_enum
* se
)
1110 struct module_pair pair
;
1112 regex_t mod_regex
, sym_regex
;
1114 pair
.pcs
= process_find_by_handle(hProcess
);
1115 if (!pair
.pcs
) return FALSE
;
1118 /* do local variables ? */
1119 if (!Mask
|| !(bang
= strchr(Mask
, '!')))
1120 return symt_enum_locals(pair
.pcs
, Mask
, se
);
1122 if (bang
== Mask
) return FALSE
;
1124 compile_regex(Mask
, bang
- Mask
, &mod_regex
, TRUE
);
1125 compile_regex(bang
+ 1, -1, &sym_regex
,
1126 dbghelp_options
& SYMOPT_CASE_INSENSITIVE
);
1128 for (pair
.requested
= pair
.pcs
->lmodules
; pair
.requested
; pair
.requested
= pair
.requested
->next
)
1130 if (pair
.requested
->type
== DMT_PE
&& module_get_debug(&pair
))
1132 if (match_regexp(&mod_regex
, pair
.requested
->module_name
) &&
1133 symt_enum_module(&pair
, &sym_regex
, se
))
1137 /* not found in PE modules, retry on the ELF ones
1139 if (!pair
.requested
&& (dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
))
1141 for (pair
.requested
= pair
.pcs
->lmodules
; pair
.requested
; pair
.requested
= pair
.requested
->next
)
1143 if ((pair
.requested
->type
== DMT_ELF
|| pair
.requested
->type
== DMT_MACHO
) &&
1144 !module_get_containee(pair
.pcs
, pair
.requested
) &&
1145 module_get_debug(&pair
))
1147 if (match_regexp(&mod_regex
, pair
.requested
->module_name
) &&
1148 symt_enum_module(&pair
, &sym_regex
, se
))
1153 regfree(&mod_regex
);
1154 regfree(&sym_regex
);
1157 pair
.requested
= module_find_by_addr(pair
.pcs
, BaseOfDll
, DMT_UNKNOWN
);
1158 if (!module_get_debug(&pair
))
1161 /* we always ignore module name from Mask when BaseOfDll is defined */
1162 if (Mask
&& (bang
= strchr(Mask
, '!')))
1164 if (bang
== Mask
) return FALSE
;
1168 compile_regex(Mask
? Mask
: "*", -1, &sym_regex
,
1169 dbghelp_options
& SYMOPT_CASE_INSENSITIVE
);
1170 symt_enum_module(&pair
, &sym_regex
, se
);
1171 regfree(&sym_regex
);
1176 /******************************************************************
1177 * SymEnumSymbols (DBGHELP.@)
1179 * cases BaseOfDll = 0
1180 * !foo fails always (despite what MSDN states)
1181 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1182 * no ! in Mask, lookup in local Context
1183 * cases BaseOfDll != 0
1184 * !foo fails always (despite what MSDN states)
1185 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1187 BOOL WINAPI
SymEnumSymbols(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR Mask
,
1188 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
1193 TRACE("(%p %s %s %p %p)\n",
1194 hProcess
, wine_dbgstr_longlong(BaseOfDll
), debugstr_a(Mask
),
1195 EnumSymbolsCallback
, UserContext
);
1197 se
.cb
= EnumSymbolsCallback
;
1198 se
.user
= UserContext
;
1202 se
.sym_info
= (PSYMBOL_INFO
)se
.buffer
;
1204 return sym_enum(hProcess
, BaseOfDll
, Mask
, &se
);
1209 PSYM_ENUMERATESYMBOLS_CALLBACKW cb
;
1211 PSYMBOL_INFOW sym_info
;
1212 char buffer
[sizeof(SYMBOL_INFOW
) + MAX_SYM_NAME
];
1216 static BOOL CALLBACK
sym_enumW(PSYMBOL_INFO si
, ULONG size
, PVOID ctx
)
1218 struct sym_enumW
* sew
= ctx
;
1220 copy_symbolW(sew
->sym_info
, si
);
1222 return (sew
->cb
)(sew
->sym_info
, size
, sew
->ctx
);
1225 /******************************************************************
1226 * SymEnumSymbolsW (DBGHELP.@)
1229 BOOL WINAPI
SymEnumSymbolsW(HANDLE hProcess
, ULONG64 BaseOfDll
, PCWSTR Mask
,
1230 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback
,
1233 struct sym_enumW sew
;
1237 sew
.ctx
= UserContext
;
1238 sew
.cb
= EnumSymbolsCallback
;
1239 sew
.sym_info
= (PSYMBOL_INFOW
)sew
.buffer
;
1243 unsigned len
= WideCharToMultiByte(CP_ACP
, 0, Mask
, -1, NULL
, 0, NULL
, NULL
);
1244 maskA
= HeapAlloc(GetProcessHeap(), 0, len
);
1245 if (!maskA
) return FALSE
;
1246 WideCharToMultiByte(CP_ACP
, 0, Mask
, -1, maskA
, len
, NULL
, NULL
);
1248 ret
= SymEnumSymbols(hProcess
, BaseOfDll
, maskA
, sym_enumW
, &sew
);
1249 HeapFree(GetProcessHeap(), 0, maskA
);
1254 struct sym_enumerate
1257 PSYM_ENUMSYMBOLS_CALLBACK cb
;
1260 static BOOL CALLBACK
sym_enumerate_cb(PSYMBOL_INFO syminfo
, ULONG size
, void* ctx
)
1262 struct sym_enumerate
* se
= ctx
;
1263 return (se
->cb
)(syminfo
->Name
, syminfo
->Address
, syminfo
->Size
, se
->ctx
);
1266 /***********************************************************************
1267 * SymEnumerateSymbols (DBGHELP.@)
1269 BOOL WINAPI
SymEnumerateSymbols(HANDLE hProcess
, DWORD BaseOfDll
,
1270 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback
,
1273 struct sym_enumerate se
;
1275 se
.ctx
= UserContext
;
1276 se
.cb
= EnumSymbolsCallback
;
1278 return SymEnumSymbols(hProcess
, BaseOfDll
, NULL
, sym_enumerate_cb
, &se
);
1281 struct sym_enumerate64
1284 PSYM_ENUMSYMBOLS_CALLBACK64 cb
;
1287 static BOOL CALLBACK
sym_enumerate_cb64(PSYMBOL_INFO syminfo
, ULONG size
, void* ctx
)
1289 struct sym_enumerate64
* se
= ctx
;
1290 return (se
->cb
)(syminfo
->Name
, syminfo
->Address
, syminfo
->Size
, se
->ctx
);
1293 /***********************************************************************
1294 * SymEnumerateSymbols64 (DBGHELP.@)
1296 BOOL WINAPI
SymEnumerateSymbols64(HANDLE hProcess
, DWORD64 BaseOfDll
,
1297 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback
,
1300 struct sym_enumerate64 se
;
1302 se
.ctx
= UserContext
;
1303 se
.cb
= EnumSymbolsCallback
;
1305 return SymEnumSymbols(hProcess
, BaseOfDll
, NULL
, sym_enumerate_cb64
, &se
);
1308 /******************************************************************
1309 * SymFromAddr (DBGHELP.@)
1312 BOOL WINAPI
SymFromAddr(HANDLE hProcess
, DWORD64 Address
,
1313 DWORD64
* Displacement
, PSYMBOL_INFO Symbol
)
1315 struct module_pair pair
;
1316 struct symt_ht
* sym
;
1318 pair
.pcs
= process_find_by_handle(hProcess
);
1319 if (!pair
.pcs
) return FALSE
;
1320 pair
.requested
= module_find_by_addr(pair
.pcs
, Address
, DMT_UNKNOWN
);
1321 if (!module_get_debug(&pair
)) return FALSE
;
1322 if ((sym
= symt_find_nearest(pair
.effective
, Address
)) == NULL
) return FALSE
;
1324 symt_fill_sym_info(&pair
, NULL
, &sym
->symt
, Symbol
);
1325 *Displacement
= Address
- Symbol
->Address
;
1329 /******************************************************************
1330 * SymFromAddrW (DBGHELP.@)
1333 BOOL WINAPI
SymFromAddrW(HANDLE hProcess
, DWORD64 Address
,
1334 DWORD64
* Displacement
, PSYMBOL_INFOW Symbol
)
1340 len
= sizeof(*si
) + Symbol
->MaxNameLen
* sizeof(WCHAR
);
1341 si
= HeapAlloc(GetProcessHeap(), 0, len
);
1342 if (!si
) return FALSE
;
1344 si
->SizeOfStruct
= sizeof(*si
);
1345 si
->MaxNameLen
= Symbol
->MaxNameLen
;
1346 if ((ret
= SymFromAddr(hProcess
, Address
, Displacement
, si
)))
1348 copy_symbolW(Symbol
, si
);
1350 HeapFree(GetProcessHeap(), 0, si
);
1354 /******************************************************************
1355 * SymGetSymFromAddr (DBGHELP.@)
1358 BOOL WINAPI
SymGetSymFromAddr(HANDLE hProcess
, DWORD Address
,
1359 PDWORD Displacement
, PIMAGEHLP_SYMBOL Symbol
)
1361 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1362 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1364 DWORD64 Displacement64
;
1366 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1367 si
->SizeOfStruct
= sizeof(*si
);
1368 si
->MaxNameLen
= MAX_SYM_NAME
;
1369 if (!SymFromAddr(hProcess
, Address
, &Displacement64
, si
))
1373 *Displacement
= Displacement64
;
1374 Symbol
->Address
= si
->Address
;
1375 Symbol
->Size
= si
->Size
;
1376 Symbol
->Flags
= si
->Flags
;
1377 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1378 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1382 /******************************************************************
1383 * SymGetSymFromAddr64 (DBGHELP.@)
1386 BOOL WINAPI
SymGetSymFromAddr64(HANDLE hProcess
, DWORD64 Address
,
1387 PDWORD64 Displacement
, PIMAGEHLP_SYMBOL64 Symbol
)
1389 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1390 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1392 DWORD64 Displacement64
;
1394 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1395 si
->SizeOfStruct
= sizeof(*si
);
1396 si
->MaxNameLen
= MAX_SYM_NAME
;
1397 if (!SymFromAddr(hProcess
, Address
, &Displacement64
, si
))
1401 *Displacement
= Displacement64
;
1402 Symbol
->Address
= si
->Address
;
1403 Symbol
->Size
= si
->Size
;
1404 Symbol
->Flags
= si
->Flags
;
1405 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1406 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1410 static BOOL
find_name(struct process
* pcs
, struct module
* module
, const char* name
,
1411 SYMBOL_INFO
* symbol
)
1413 struct hash_table_iter hti
;
1415 struct symt_ht
* sym
= NULL
;
1416 struct module_pair pair
;
1419 if (!(pair
.requested
= module
)) return FALSE
;
1420 if (!module_get_debug(&pair
)) return FALSE
;
1422 hash_table_iter_init(&pair
.effective
->ht_symbols
, &hti
, name
);
1423 while ((ptr
= hash_table_iter_up(&hti
)))
1425 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
1427 if (!strcmp(sym
->hash_elt
.name
, name
))
1429 symt_fill_sym_info(&pair
, NULL
, &sym
->symt
, symbol
);
1436 /******************************************************************
1437 * SymFromName (DBGHELP.@)
1440 BOOL WINAPI
SymFromName(HANDLE hProcess
, PCSTR Name
, PSYMBOL_INFO Symbol
)
1442 struct process
* pcs
= process_find_by_handle(hProcess
);
1443 struct module
* module
;
1446 TRACE("(%p, %s, %p)\n", hProcess
, Name
, Symbol
);
1447 if (!pcs
) return FALSE
;
1448 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1449 name
= strchr(Name
, '!');
1453 assert(name
- Name
< sizeof(tmp
));
1454 memcpy(tmp
, Name
, name
- Name
);
1455 tmp
[name
- Name
] = '\0';
1456 module
= module_find_by_nameA(pcs
, tmp
);
1457 return find_name(pcs
, module
, name
+ 1, Symbol
);
1459 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1461 if (module
->type
== DMT_PE
&& find_name(pcs
, module
, Name
, Symbol
))
1464 /* not found in PE modules, retry on the ELF ones
1466 if (dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
)
1468 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1470 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
1471 !module_get_containee(pcs
, module
) &&
1472 find_name(pcs
, module
, Name
, Symbol
))
1479 /***********************************************************************
1480 * SymGetSymFromName64 (DBGHELP.@)
1482 BOOL WINAPI
SymGetSymFromName64(HANDLE hProcess
, PCSTR Name
, PIMAGEHLP_SYMBOL64 Symbol
)
1484 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1485 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1488 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1489 si
->SizeOfStruct
= sizeof(*si
);
1490 si
->MaxNameLen
= MAX_SYM_NAME
;
1491 if (!SymFromName(hProcess
, Name
, si
)) return FALSE
;
1493 Symbol
->Address
= si
->Address
;
1494 Symbol
->Size
= si
->Size
;
1495 Symbol
->Flags
= si
->Flags
;
1496 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1497 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1501 /***********************************************************************
1502 * SymGetSymFromName (DBGHELP.@)
1504 BOOL WINAPI
SymGetSymFromName(HANDLE hProcess
, PCSTR Name
, PIMAGEHLP_SYMBOL Symbol
)
1506 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1507 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1510 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
1511 si
->SizeOfStruct
= sizeof(*si
);
1512 si
->MaxNameLen
= MAX_SYM_NAME
;
1513 if (!SymFromName(hProcess
, Name
, si
)) return FALSE
;
1515 Symbol
->Address
= si
->Address
;
1516 Symbol
->Size
= si
->Size
;
1517 Symbol
->Flags
= si
->Flags
;
1518 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
1519 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
1523 /******************************************************************
1524 * sym_fill_func_line_info
1526 * fills information about a file
1528 BOOL
symt_fill_func_line_info(const struct module
* module
, const struct symt_function
* func
,
1529 DWORD64 addr
, IMAGEHLP_LINE64
* line
)
1531 struct line_info
* dli
= NULL
;
1535 assert(func
->symt
.tag
== SymTagFunction
);
1537 for (i
=vector_length(&func
->vlines
)-1; i
>=0; i
--)
1539 dli
= vector_at(&func
->vlines
, i
);
1540 if (!dli
->is_source_file
)
1542 if (found
|| dli
->u
.pc_offset
> addr
) continue;
1543 line
->LineNumber
= dli
->line_number
;
1544 line
->Address
= dli
->u
.pc_offset
;
1551 line
->FileName
= (char*)source_get(module
, dli
->u
.source_file
);
1558 /***********************************************************************
1559 * SymGetSymNext64 (DBGHELP.@)
1561 BOOL WINAPI
SymGetSymNext64(HANDLE hProcess
, PIMAGEHLP_SYMBOL64 Symbol
)
1564 * get module from Symbol.Address
1565 * get index in module.addr_sorttab of Symbol.Address
1567 * if out of module bounds, move to next module in process address space
1569 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1570 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1574 /***********************************************************************
1575 * SymGetSymNext (DBGHELP.@)
1577 BOOL WINAPI
SymGetSymNext(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1579 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1580 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1584 /***********************************************************************
1585 * SymGetSymPrev64 (DBGHELP.@)
1587 BOOL WINAPI
SymGetSymPrev64(HANDLE hProcess
, PIMAGEHLP_SYMBOL64 Symbol
)
1589 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1590 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1594 /***********************************************************************
1595 * SymGetSymPrev (DBGHELP.@)
1597 BOOL WINAPI
SymGetSymPrev(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1599 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1600 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1604 /******************************************************************
1605 * copy_line_64_from_32 (internal)
1608 static void copy_line_64_from_32(IMAGEHLP_LINE64
* l64
, const IMAGEHLP_LINE
* l32
)
1611 l64
->Key
= l32
->Key
;
1612 l64
->LineNumber
= l32
->LineNumber
;
1613 l64
->FileName
= l32
->FileName
;
1614 l64
->Address
= l32
->Address
;
1617 /******************************************************************
1618 * copy_line_W64_from_32 (internal)
1621 static void copy_line_W64_from_64(struct process
* pcs
, IMAGEHLP_LINEW64
* l64w
, const IMAGEHLP_LINE64
* l64
)
1625 l64w
->Key
= l64
->Key
;
1626 l64w
->LineNumber
= l64
->LineNumber
;
1627 len
= MultiByteToWideChar(CP_ACP
, 0, l64
->FileName
, -1, NULL
, 0);
1628 if ((l64w
->FileName
= fetch_buffer(pcs
, len
* sizeof(WCHAR
))))
1629 MultiByteToWideChar(CP_ACP
, 0, l64
->FileName
, -1, l64w
->FileName
, len
);
1630 l64w
->Address
= l64
->Address
;
1633 /******************************************************************
1634 * copy_line_32_from_64 (internal)
1637 static void copy_line_32_from_64(IMAGEHLP_LINE
* l32
, const IMAGEHLP_LINE64
* l64
)
1640 l32
->Key
= l64
->Key
;
1641 l32
->LineNumber
= l64
->LineNumber
;
1642 l32
->FileName
= l64
->FileName
;
1643 l32
->Address
= l64
->Address
;
1646 /******************************************************************
1647 * SymGetLineFromAddr (DBGHELP.@)
1650 BOOL WINAPI
SymGetLineFromAddr(HANDLE hProcess
, DWORD dwAddr
,
1651 PDWORD pdwDisplacement
, PIMAGEHLP_LINE Line
)
1653 IMAGEHLP_LINE64 il64
;
1655 il64
.SizeOfStruct
= sizeof(il64
);
1656 if (!SymGetLineFromAddr64(hProcess
, dwAddr
, pdwDisplacement
, &il64
))
1658 copy_line_32_from_64(Line
, &il64
);
1662 /******************************************************************
1663 * SymGetLineFromAddr64 (DBGHELP.@)
1666 BOOL WINAPI
SymGetLineFromAddr64(HANDLE hProcess
, DWORD64 dwAddr
,
1667 PDWORD pdwDisplacement
, PIMAGEHLP_LINE64 Line
)
1669 struct module_pair pair
;
1670 struct symt_ht
* symt
;
1672 TRACE("%p %s %p %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), pdwDisplacement
, Line
);
1674 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1676 pair
.pcs
= process_find_by_handle(hProcess
);
1677 if (!pair
.pcs
) return FALSE
;
1678 pair
.requested
= module_find_by_addr(pair
.pcs
, dwAddr
, DMT_UNKNOWN
);
1679 if (!module_get_debug(&pair
)) return FALSE
;
1680 if ((symt
= symt_find_nearest(pair
.effective
, dwAddr
)) == NULL
) return FALSE
;
1682 if (symt
->symt
.tag
!= SymTagFunction
) return FALSE
;
1683 if (!symt_fill_func_line_info(pair
.effective
, (struct symt_function
*)symt
,
1684 dwAddr
, Line
)) return FALSE
;
1685 *pdwDisplacement
= dwAddr
- Line
->Address
;
1689 /******************************************************************
1690 * SymGetLineFromAddrW64 (DBGHELP.@)
1693 BOOL WINAPI
SymGetLineFromAddrW64(HANDLE hProcess
, DWORD64 dwAddr
,
1694 PDWORD pdwDisplacement
, PIMAGEHLP_LINEW64 Line
)
1696 IMAGEHLP_LINE64 il64
;
1698 il64
.SizeOfStruct
= sizeof(il64
);
1699 if (!SymGetLineFromAddr64(hProcess
, dwAddr
, pdwDisplacement
, &il64
))
1701 copy_line_W64_from_64(process_find_by_handle(hProcess
), Line
, &il64
);
1705 /******************************************************************
1706 * SymGetLinePrev64 (DBGHELP.@)
1709 BOOL WINAPI
SymGetLinePrev64(HANDLE hProcess
, PIMAGEHLP_LINE64 Line
)
1711 struct module_pair pair
;
1712 struct line_info
* li
;
1713 BOOL in_search
= FALSE
;
1715 TRACE("(%p %p)\n", hProcess
, Line
);
1717 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1719 pair
.pcs
= process_find_by_handle(hProcess
);
1720 if (!pair
.pcs
) return FALSE
;
1721 pair
.requested
= module_find_by_addr(pair
.pcs
, Line
->Address
, DMT_UNKNOWN
);
1722 if (!module_get_debug(&pair
)) return FALSE
;
1724 if (Line
->Key
== 0) return FALSE
;
1726 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1727 * element we have to go back until we find the prev one to get the real
1728 * source file name for the DLIT_OFFSET element just before
1729 * the first DLIT_SOURCEFILE
1731 while (!li
->is_first
)
1734 if (!li
->is_source_file
)
1736 Line
->LineNumber
= li
->line_number
;
1737 Line
->Address
= li
->u
.pc_offset
;
1739 if (!in_search
) return TRUE
;
1745 Line
->FileName
= (char*)source_get(pair
.effective
, li
->u
.source_file
);
1751 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
1755 /******************************************************************
1756 * SymGetLinePrev (DBGHELP.@)
1759 BOOL WINAPI
SymGetLinePrev(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
1761 IMAGEHLP_LINE64 line64
;
1763 line64
.SizeOfStruct
= sizeof(line64
);
1764 copy_line_64_from_32(&line64
, Line
);
1765 if (!SymGetLinePrev64(hProcess
, &line64
)) return FALSE
;
1766 copy_line_32_from_64(Line
, &line64
);
1770 BOOL
symt_get_func_line_next(const struct module
* module
, PIMAGEHLP_LINE64 line
)
1772 struct line_info
* li
;
1774 if (line
->Key
== 0) return FALSE
;
1776 while (!li
->is_last
)
1779 if (!li
->is_source_file
)
1781 line
->LineNumber
= li
->line_number
;
1782 line
->Address
= li
->u
.pc_offset
;
1786 line
->FileName
= (char*)source_get(module
, li
->u
.source_file
);
1791 /******************************************************************
1792 * SymGetLineNext64 (DBGHELP.@)
1795 BOOL WINAPI
SymGetLineNext64(HANDLE hProcess
, PIMAGEHLP_LINE64 Line
)
1797 struct module_pair pair
;
1799 TRACE("(%p %p)\n", hProcess
, Line
);
1801 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1802 pair
.pcs
= process_find_by_handle(hProcess
);
1803 if (!pair
.pcs
) return FALSE
;
1804 pair
.requested
= module_find_by_addr(pair
.pcs
, Line
->Address
, DMT_UNKNOWN
);
1805 if (!module_get_debug(&pair
)) return FALSE
;
1807 if (symt_get_func_line_next(pair
.effective
, Line
)) return TRUE
;
1808 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
1812 /******************************************************************
1813 * SymGetLineNext (DBGHELP.@)
1816 BOOL WINAPI
SymGetLineNext(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
1818 IMAGEHLP_LINE64 line64
;
1820 line64
.SizeOfStruct
= sizeof(line64
);
1821 copy_line_64_from_32(&line64
, Line
);
1822 if (!SymGetLineNext64(hProcess
, &line64
)) return FALSE
;
1823 copy_line_32_from_64(Line
, &line64
);
1827 /***********************************************************************
1828 * SymUnDName (DBGHELP.@)
1830 BOOL WINAPI
SymUnDName(PIMAGEHLP_SYMBOL sym
, PSTR UnDecName
, DWORD UnDecNameLength
)
1832 return UnDecorateSymbolName(sym
->Name
, UnDecName
, UnDecNameLength
,
1833 UNDNAME_COMPLETE
) != 0;
1836 /***********************************************************************
1837 * SymUnDName64 (DBGHELP.@)
1839 BOOL WINAPI
SymUnDName64(PIMAGEHLP_SYMBOL64 sym
, PSTR UnDecName
, DWORD UnDecNameLength
)
1841 return UnDecorateSymbolName(sym
->Name
, UnDecName
, UnDecNameLength
,
1842 UNDNAME_COMPLETE
) != 0;
1845 static void* und_alloc(size_t len
) { return HeapAlloc(GetProcessHeap(), 0, len
); }
1846 static void und_free (void* ptr
) { HeapFree(GetProcessHeap(), 0, ptr
); }
1848 /***********************************************************************
1849 * UnDecorateSymbolName (DBGHELP.@)
1851 DWORD WINAPI
UnDecorateSymbolName(PCSTR DecoratedName
, PSTR UnDecoratedName
,
1852 DWORD UndecoratedLength
, DWORD Flags
)
1854 /* undocumented from msvcrt */
1855 static char* (*p_undname
)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1856 static const WCHAR szMsvcrt
[] = {'m','s','v','c','r','t','.','d','l','l',0};
1858 TRACE("(%s, %p, %d, 0x%08x)\n",
1859 debugstr_a(DecoratedName
), UnDecoratedName
, UndecoratedLength
, Flags
);
1863 if (!hMsvcrt
) hMsvcrt
= LoadLibraryW(szMsvcrt
);
1864 if (hMsvcrt
) p_undname
= (void*)GetProcAddress(hMsvcrt
, "__unDName");
1865 if (!p_undname
) return 0;
1868 if (!UnDecoratedName
) return 0;
1869 if (!p_undname(UnDecoratedName
, DecoratedName
, UndecoratedLength
,
1870 und_alloc
, und_free
, Flags
))
1872 return strlen(UnDecoratedName
);
1875 /******************************************************************
1876 * SymMatchString (DBGHELP.@)
1879 BOOL WINAPI
SymMatchString(PCSTR string
, PCSTR re
, BOOL _case
)
1884 TRACE("%s %s %c\n", string
, re
, _case
? 'Y' : 'N');
1886 compile_regex(re
, -1, &preg
, _case
);
1887 ret
= match_regexp(&preg
, string
);
1892 /******************************************************************
1893 * SymSearch (DBGHELP.@)
1895 BOOL WINAPI
SymSearch(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD Index
,
1896 DWORD SymTag
, PCSTR Mask
, DWORD64 Address
,
1897 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
1898 PVOID UserContext
, DWORD Options
)
1902 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1903 hProcess
, wine_dbgstr_longlong(BaseOfDll
), Index
, SymTag
, Mask
,
1904 wine_dbgstr_longlong(Address
), EnumSymbolsCallback
,
1905 UserContext
, Options
);
1907 if (Options
!= SYMSEARCH_GLOBALSONLY
)
1909 FIXME("Unsupported searching with options (%x)\n", Options
);
1910 SetLastError(ERROR_INVALID_PARAMETER
);
1914 se
.cb
= EnumSymbolsCallback
;
1915 se
.user
= UserContext
;
1919 se
.sym_info
= (PSYMBOL_INFO
)se
.buffer
;
1921 return sym_enum(hProcess
, BaseOfDll
, Mask
, &se
);
1924 /******************************************************************
1925 * SymSearchW (DBGHELP.@)
1927 BOOL WINAPI
SymSearchW(HANDLE hProcess
, ULONG64 BaseOfDll
, DWORD Index
,
1928 DWORD SymTag
, PCWSTR Mask
, DWORD64 Address
,
1929 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback
,
1930 PVOID UserContext
, DWORD Options
)
1932 struct sym_enumW sew
;
1936 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1937 hProcess
, wine_dbgstr_longlong(BaseOfDll
), Index
, SymTag
, debugstr_w(Mask
),
1938 wine_dbgstr_longlong(Address
), EnumSymbolsCallback
,
1939 UserContext
, Options
);
1941 sew
.ctx
= UserContext
;
1942 sew
.cb
= EnumSymbolsCallback
;
1943 sew
.sym_info
= (PSYMBOL_INFOW
)sew
.buffer
;
1947 unsigned len
= WideCharToMultiByte(CP_ACP
, 0, Mask
, -1, NULL
, 0, NULL
, NULL
);
1948 maskA
= HeapAlloc(GetProcessHeap(), 0, len
);
1949 if (!maskA
) return FALSE
;
1950 WideCharToMultiByte(CP_ACP
, 0, Mask
, -1, maskA
, len
, NULL
, NULL
);
1952 ret
= SymSearch(hProcess
, BaseOfDll
, Index
, SymTag
, maskA
, Address
,
1953 sym_enumW
, &sew
, Options
);
1954 HeapFree(GetProcessHeap(), 0, maskA
);
1959 /******************************************************************
1960 * SymAddSymbol (DBGHELP.@)
1963 BOOL WINAPI
SymAddSymbol(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR name
,
1964 DWORD64 addr
, DWORD size
, DWORD flags
)
1966 WCHAR nameW
[MAX_SYM_NAME
];
1968 MultiByteToWideChar(CP_ACP
, 0, name
, -1, nameW
, sizeof(nameW
) / sizeof(WCHAR
));
1969 return SymAddSymbolW(hProcess
, BaseOfDll
, nameW
, addr
, size
, flags
);
1972 /******************************************************************
1973 * SymAddSymbolW (DBGHELP.@)
1976 BOOL WINAPI
SymAddSymbolW(HANDLE hProcess
, ULONG64 BaseOfDll
, PCWSTR name
,
1977 DWORD64 addr
, DWORD size
, DWORD flags
)
1979 struct module_pair pair
;
1981 TRACE("(%p %s %s %u)\n", hProcess
, wine_dbgstr_w(name
), wine_dbgstr_longlong(addr
), size
);
1983 pair
.pcs
= process_find_by_handle(hProcess
);
1984 if (!pair
.pcs
) return FALSE
;
1985 pair
.requested
= module_find_by_addr(pair
.pcs
, BaseOfDll
, DMT_UNKNOWN
);
1986 if (!module_get_debug(&pair
)) return FALSE
;
1988 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1992 /******************************************************************
1993 * SymSetScopeFromAddr (DBGHELP.@)
1995 BOOL WINAPI
SymSetScopeFromAddr(HANDLE hProcess
, ULONG64 addr
)
1997 struct process
* pcs
;
1999 FIXME("(%p %s): stub\n", hProcess
, wine_dbgstr_longlong(addr
));
2001 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
2005 /******************************************************************
2006 * SymEnumLines (DBGHELP.@)
2009 BOOL WINAPI
SymEnumLines(HANDLE hProcess
, ULONG64 base
, PCSTR compiland
,
2010 PCSTR srcfile
, PSYM_ENUMLINES_CALLBACK cb
, PVOID user
)
2012 struct module_pair pair
;
2013 struct hash_table_iter hti
;
2014 struct symt_ht
* sym
;
2016 struct line_info
* dli
;
2021 if (!cb
) return FALSE
;
2022 if (!(dbghelp_options
& SYMOPT_LOAD_LINES
)) return TRUE
;
2024 pair
.pcs
= process_find_by_handle(hProcess
);
2025 if (!pair
.pcs
) return FALSE
;
2026 if (compiland
) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland
);
2027 pair
.requested
= module_find_by_addr(pair
.pcs
, base
, DMT_UNKNOWN
);
2028 if (!module_get_debug(&pair
)) return FALSE
;
2029 if (!compile_file_regex(&re
, srcfile
)) return FALSE
;
2031 sci
.SizeOfStruct
= sizeof(sci
);
2034 hash_table_iter_init(&pair
.effective
->ht_symbols
, &hti
, NULL
);
2035 while ((ptr
= hash_table_iter_up(&hti
)))
2039 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
2040 if (sym
->symt
.tag
!= SymTagFunction
) continue;
2042 sci
.FileName
[0] = '\0';
2043 for (i
=0; i
<vector_length(&((struct symt_function
*)sym
)->vlines
); i
++)
2045 dli
= vector_at(&((struct symt_function
*)sym
)->vlines
, i
);
2046 if (dli
->is_source_file
)
2048 file
= source_get(pair
.effective
, dli
->u
.source_file
);
2049 if (!match_regexp(&re
, file
)) sci
.FileName
[0] = '\0';
2050 else strcpy(sci
.FileName
, file
);
2052 else if (sci
.FileName
[0])
2055 sci
.Obj
[0] = '\0'; /* FIXME */
2056 sci
.LineNumber
= dli
->line_number
;
2057 sci
.Address
= dli
->u
.pc_offset
;
2058 if (!cb(&sci
, user
)) break;
2066 BOOL WINAPI
SymGetLineFromName(HANDLE hProcess
, PCSTR ModuleName
, PCSTR FileName
,
2067 DWORD dwLineNumber
, PLONG plDisplacement
, PIMAGEHLP_LINE Line
)
2069 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess
, ModuleName
, FileName
,
2070 dwLineNumber
, plDisplacement
, Line
);
2074 BOOL WINAPI
SymGetLineFromName64(HANDLE hProcess
, PCSTR ModuleName
, PCSTR FileName
,
2075 DWORD dwLineNumber
, PLONG lpDisplacement
, PIMAGEHLP_LINE64 Line
)
2077 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess
, ModuleName
, FileName
,
2078 dwLineNumber
, lpDisplacement
, Line
);
2082 BOOL WINAPI
SymGetLineFromNameW64(HANDLE hProcess
, PCWSTR ModuleName
, PCWSTR FileName
,
2083 DWORD dwLineNumber
, PLONG plDisplacement
, PIMAGEHLP_LINEW64 Line
)
2085 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess
, debugstr_w(ModuleName
), debugstr_w(FileName
),
2086 dwLineNumber
, plDisplacement
, Line
);