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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include <sys/types.h>
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
41 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt
);
43 inline static int cmp_addr(ULONG64 a1
, ULONG64 a2
)
45 if (a1
> a2
) return 1;
46 if (a1
< a2
) return -1;
50 inline static int cmp_sorttab_addr(const struct module
* module
, int idx
, ULONG64 addr
)
54 symt_get_info(&module
->addr_sorttab
[idx
]->symt
, TI_GET_ADDRESS
, &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_info(sym1
, TI_GET_ADDRESS
, &a1
);
65 symt_get_info(sym2
, TI_GET_ADDRESS
, &a2
);
66 return cmp_addr(a1
, a2
);
69 static inline void re_append(char** mask
, unsigned* len
, char ch
)
71 *mask
= HeapReAlloc(GetProcessHeap(), 0, *mask
, ++(*len
));
72 (*mask
)[*len
- 2] = ch
;
75 /* transforms a dbghelp's regular expression into a POSIX one
76 * Here are the valid dbghelp reg ex characters:
77 * * 0 or more characters
78 * ? a single character
80 * # 0 or more of preceding char
81 * + 1 or more of preceding char
82 * escapes \ on #, ?, [, ], *, +. don't work on -
84 static void compile_regex(const char* str
, int numchar
, regex_t
* re
)
86 char* mask
= HeapAlloc(GetProcessHeap(), 0, 1);
88 BOOL in_escape
= FALSE
;
90 re_append(&mask
, &len
, '^');
92 while (*str
&& numchar
--)
94 /* FIXME: this shouldn't be valid on '-' */
97 re_append(&mask
, &len
, '\\');
98 re_append(&mask
, &len
, *str
);
103 case '\\': in_escape
= TRUE
; break;
104 case '*': re_append(&mask
, &len
, '.'); re_append(&mask
, &len
, '*'); break;
105 case '?': re_append(&mask
, &len
, '.'); break;
106 case '#': re_append(&mask
, &len
, '*'); break;
107 /* escape some valid characters in dbghelp reg exp:s */
108 case '$': re_append(&mask
, &len
, '\\'); re_append(&mask
, &len
, '$'); break;
109 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
110 default: re_append(&mask
, &len
, *str
); break;
116 re_append(&mask
, &len
, '\\');
117 re_append(&mask
, &len
, '\\');
119 re_append(&mask
, &len
, '$');
120 mask
[len
- 1] = '\0';
121 if (regcomp(re
, mask
, REG_NOSUB
)) FIXME("Couldn't compile %s\n", mask
);
122 HeapFree(GetProcessHeap(), 0, mask
);
125 struct symt_compiland
* symt_new_compiland(struct module
* module
, const char* name
)
127 struct symt_compiland
* sym
;
129 TRACE_(dbghelp_symt
)("Adding compiland symbol %s:%s\n",
130 module
->module
.ModuleName
, name
);
131 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
133 sym
->symt
.tag
= SymTagCompiland
;
134 sym
->source
= source_new(module
, name
);
135 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 32);
140 struct symt_public
* symt_new_public(struct module
* module
,
141 struct symt_compiland
* compiland
,
143 unsigned long address
, unsigned size
,
144 BOOL in_code
, BOOL is_func
)
146 struct symt_public
* sym
;
149 TRACE_(dbghelp_symt
)("Adding public symbol %s:%s @%lx\n",
150 module
->module
.ModuleName
, name
, address
);
151 if ((dbghelp_options
& SYMOPT_AUTO_PUBLICS
) &&
152 symt_find_nearest(module
, address
) != -1)
154 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
156 sym
->symt
.tag
= SymTagPublicSymbol
;
157 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
158 hash_table_add(&module
->ht_symbols
, &sym
->hash_elt
);
159 module
->sortlist_valid
= FALSE
;
160 sym
->container
= compiland
? &compiland
->symt
: NULL
;
161 sym
->address
= address
;
163 sym
->in_code
= in_code
;
164 sym
->is_function
= is_func
;
167 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
174 struct symt_data
* symt_new_global_variable(struct module
* module
,
175 struct symt_compiland
* compiland
,
176 const char* name
, unsigned is_static
,
177 unsigned long addr
, unsigned long size
,
180 struct symt_data
* sym
;
184 TRACE_(dbghelp_symt
)("Adding global symbol %s:%s @%lx %p\n",
185 module
->module
.ModuleName
, name
, addr
, type
);
186 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
188 sym
->symt
.tag
= SymTagData
;
189 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
190 hash_table_add(&module
->ht_symbols
, &sym
->hash_elt
);
191 module
->sortlist_valid
= FALSE
;
192 sym
->kind
= is_static
? DataIsFileStatic
: DataIsGlobal
;
193 sym
->container
= compiland
? &compiland
->symt
: NULL
;
195 sym
->u
.address
= addr
;
196 if (type
&& size
&& symt_get_info(type
, TI_GET_LENGTH
, &tsz
))
199 FIXME("Size mismatch for %s.%s between type (%lu) and src (%lu)\n",
200 module
->module
.ModuleName
, name
, tsz
, size
);
204 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
211 struct symt_function
* symt_new_function(struct module
* module
,
212 struct symt_compiland
* compiland
,
214 unsigned long addr
, unsigned long size
,
215 struct symt
* sig_type
)
217 struct symt_function
* sym
;
220 TRACE_(dbghelp_symt
)("Adding global function %s:%s @%lx-%lx\n",
221 module
->module
.ModuleName
, name
, addr
, addr
+ size
- 1);
223 assert(!sig_type
|| sig_type
->tag
== SymTagFunctionType
);
224 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
226 sym
->symt
.tag
= SymTagFunction
;
227 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
228 hash_table_add(&module
->ht_symbols
, &sym
->hash_elt
);
229 module
->sortlist_valid
= FALSE
;
230 sym
->container
= &compiland
->symt
;
232 sym
->type
= sig_type
;
234 vector_init(&sym
->vlines
, sizeof(struct line_info
), 64);
235 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
238 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
245 void symt_add_func_line(struct module
* module
, struct symt_function
* func
,
246 unsigned source_idx
, int line_num
, unsigned long offset
)
248 struct line_info
* dli
;
249 BOOL last_matches
= FALSE
;
251 if (func
== NULL
|| !(dbghelp_options
& SYMOPT_LOAD_LINES
)) return;
253 TRACE_(dbghelp_symt
)("(%p)%s:%lx %s:%u\n",
254 func
, func
->hash_elt
.name
, offset
,
255 source_get(module
, source_idx
), line_num
);
257 assert(func
->symt
.tag
== SymTagFunction
);
260 while ((dli
= vector_iter_down(&func
->vlines
, dli
)))
262 if (dli
->is_source_file
)
264 last_matches
= (source_idx
== dli
->u
.source_file
);
271 /* we shouldn't have line changes on first line of function */
272 dli
= vector_add(&func
->vlines
, &module
->pool
);
273 dli
->is_source_file
= 1;
274 dli
->is_first
= dli
->is_last
= 0;
275 dli
->line_number
= 0;
276 dli
->u
.source_file
= source_idx
;
278 dli
= vector_add(&func
->vlines
, &module
->pool
);
279 dli
->is_source_file
= 0;
280 dli
->is_first
= dli
->is_last
= 0;
281 dli
->line_number
= line_num
;
282 dli
->u
.pc_offset
= func
->address
+ offset
;
285 struct symt_data
* symt_add_func_local(struct module
* module
,
286 struct symt_function
* func
,
287 int regno
, int offset
,
288 struct symt_block
* block
,
289 struct symt
* type
, const char* name
)
291 struct symt_data
* locsym
;
295 assert(func
->symt
.tag
== SymTagFunction
);
297 TRACE_(dbghelp_symt
)("Adding local symbol (%s:%s): %s %p\n",
298 module
->module
.ModuleName
, func
->hash_elt
.name
,
300 locsym
= pool_alloc(&module
->pool
, sizeof(*locsym
));
301 locsym
->symt
.tag
= SymTagData
;
302 locsym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
303 locsym
->hash_elt
.next
= NULL
;
304 locsym
->kind
= (offset
< 0) ? DataIsParam
: DataIsLocal
;
305 locsym
->container
= &block
->symt
;
309 locsym
->u
.s
.reg_id
= regno
;
310 locsym
->u
.s
.offset
= 0;
311 locsym
->u
.s
.length
= 0;
315 locsym
->u
.s
.reg_id
= 0;
316 locsym
->u
.s
.offset
= offset
* 8;
317 locsym
->u
.s
.length
= 0;
320 p
= vector_add(&block
->vchildren
, &module
->pool
);
322 p
= vector_add(&func
->vchildren
, &module
->pool
);
327 struct symt_block
* symt_open_func_block(struct module
* module
,
328 struct symt_function
* func
,
329 struct symt_block
* parent_block
,
330 unsigned pc
, unsigned len
)
332 struct symt_block
* block
;
336 assert(func
->symt
.tag
== SymTagFunction
);
338 assert(!parent_block
|| parent_block
->symt
.tag
== SymTagBlock
);
339 block
= pool_alloc(&module
->pool
, sizeof(*block
));
340 block
->symt
.tag
= SymTagBlock
;
341 block
->address
= func
->address
+ pc
;
343 block
->container
= parent_block
? &parent_block
->symt
: &func
->symt
;
344 vector_init(&block
->vchildren
, sizeof(struct symt
*), 4);
346 p
= vector_add(&parent_block
->vchildren
, &module
->pool
);
348 p
= vector_add(&func
->vchildren
, &module
->pool
);
354 struct symt_block
* symt_close_func_block(struct module
* module
,
355 struct symt_function
* func
,
356 struct symt_block
* block
, unsigned pc
)
358 assert(func
->symt
.tag
== SymTagFunction
);
360 if (pc
) block
->size
= func
->address
+ pc
- block
->address
;
361 return (block
->container
->tag
== SymTagBlock
) ?
362 GET_ENTRY(block
->container
, struct symt_block
, symt
) : NULL
;
365 struct symt_function_point
* symt_add_function_point(struct module
* module
,
366 struct symt_function
* func
,
367 enum SymTagEnum point
,
368 unsigned offset
, const char* name
)
370 struct symt_function_point
* sym
;
373 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
375 sym
->symt
.tag
= point
;
377 sym
->offset
= offset
;
378 sym
->name
= name
? pool_strdup(&module
->pool
, name
) : NULL
;
379 p
= vector_add(&func
->vchildren
, &module
->pool
);
385 BOOL
symt_normalize_function(struct module
* module
, struct symt_function
* func
)
388 struct line_info
* dli
;
391 /* We aren't adding any more locals or line numbers to this function.
392 * Free any spare memory that we might have allocated.
394 assert(func
->symt
.tag
== SymTagFunction
);
396 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
397 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
399 len
= vector_length(&func
->vlines
);
402 dli
= vector_at(&func
->vlines
, 0); dli
->is_first
= 1;
403 dli
= vector_at(&func
->vlines
, len
); dli
->is_last
= 1;
408 struct symt_thunk
* symt_new_thunk(struct module
* module
,
409 struct symt_compiland
* compiland
,
410 const char* name
, THUNK_ORDINAL ord
,
411 unsigned long addr
, unsigned long size
)
413 struct symt_thunk
* sym
;
415 TRACE_(dbghelp_symt
)("Adding global thunk %s:%s @%lx-%lx\n",
416 module
->module
.ModuleName
, name
, addr
, addr
+ size
- 1);
418 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
420 sym
->symt
.tag
= SymTagThunk
;
421 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
422 hash_table_add(&module
->ht_symbols
, &sym
->hash_elt
);
423 module
->sortlist_valid
= FALSE
;
424 sym
->container
= &compiland
->symt
;
431 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
438 /* expect sym_info->MaxNameLen to be set before being called */
439 static void symt_fill_sym_info(const struct module
* module
,
440 const struct symt
* sym
, SYMBOL_INFO
* sym_info
)
444 sym_info
->TypeIndex
= (DWORD
)sym
;
445 sym_info
->info
= 0; /* TBD */
446 symt_get_info(sym
, TI_GET_LENGTH
, &sym_info
->Size
);
447 sym_info
->ModBase
= module
->module
.BaseOfImage
;
453 const struct symt_data
* data
= (const struct symt_data
*)sym
;
458 if (data
->u
.s
.reg_id
)
460 sym_info
->Flags
|= SYMFLAG_LOCAL
| SYMFLAG_REGISTER
;
461 sym_info
->Register
= data
->u
.s
.reg_id
;
462 sym_info
->Address
= 0;
466 if (data
->u
.s
.offset
< 0)
467 sym_info
->Flags
|= SYMFLAG_LOCAL
| SYMFLAG_FRAMEREL
;
469 sym_info
->Flags
|= SYMFLAG_PARAMETER
| SYMFLAG_FRAMEREL
;
470 /* FIXME: needed ? moreover, it's i386 dependent !!! */
471 sym_info
->Register
= CV_REG_EBP
;
472 sym_info
->Address
= data
->u
.s
.offset
;
476 case DataIsFileStatic
:
477 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
478 sym_info
->Register
= 0;
481 sym_info
->Flags
|= SYMFLAG_VALUEPRESENT
;
482 switch (data
->u
.value
.n1
.n2
.vt
)
484 case VT_I4
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.lVal
; break;
485 case VT_I2
: sym_info
->Value
= (ULONG
)(long)data
->u
.value
.n1
.n2
.n3
.iVal
; break;
486 case VT_I1
: sym_info
->Value
= (ULONG
)(long)data
->u
.value
.n1
.n2
.n3
.cVal
; break;
487 case VT_UI4
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.ulVal
; break;
488 case VT_UI2
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.uiVal
; break;
489 case VT_UI1
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.bVal
; break;
491 FIXME("Unsupported variant type (%u)\n", data
->u
.value
.n1
.n2
.vt
);
495 FIXME("Unhandled kind (%u) in sym data\n", data
->kind
);
499 case SymTagPublicSymbol
:
500 sym_info
->Flags
|= SYMFLAG_EXPORT
;
501 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
504 sym_info
->Flags
|= SYMFLAG_FUNCTION
;
505 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
508 sym_info
->Flags
|= SYMFLAG_THUNK
;
509 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
512 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
513 sym_info
->Register
= 0;
516 sym_info
->Scope
= 0; /* FIXME */
517 sym_info
->Tag
= sym
->tag
;
518 name
= symt_get_name(sym
);
519 if (sym_info
->MaxNameLen
)
521 if (sym
->tag
!= SymTagPublicSymbol
|| !(dbghelp_options
& SYMOPT_UNDNAME
) ||
522 (sym_info
->NameLen
= UnDecorateSymbolName(name
, sym_info
->Name
,
523 sym_info
->MaxNameLen
, UNDNAME_COMPLETE
) == 0))
525 sym_info
->NameLen
= min(strlen(name
), sym_info
->MaxNameLen
- 1);
526 memcpy(sym_info
->Name
, name
, sym_info
->NameLen
);
527 sym_info
->Name
[sym_info
->NameLen
] = '\0';
530 TRACE_(dbghelp_symt
)("%p => %s %lu %s\n",
531 sym
, sym_info
->Name
, sym_info
->Size
,
532 wine_dbgstr_longlong(sym_info
->Address
));
535 static BOOL
symt_enum_module(struct module
* module
, regex_t
* regex
,
536 PSYM_ENUMERATESYMBOLS_CALLBACK cb
, PVOID user
)
538 char buffer
[sizeof(SYMBOL_INFO
) + 256];
539 SYMBOL_INFO
* sym_info
= (SYMBOL_INFO
*)buffer
;
541 struct symt_ht
* sym
= NULL
;
542 struct hash_table_iter hti
;
544 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
545 while ((ptr
= hash_table_iter_up(&hti
)))
547 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
548 if (sym
->hash_elt
.name
&&
549 regexec(regex
, sym
->hash_elt
.name
, 0, NULL
, 0) == 0)
551 sym_info
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
552 sym_info
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
553 symt_fill_sym_info(module
, &sym
->symt
, sym_info
);
554 if (!cb(sym_info
, sym_info
->Size
, user
)) return TRUE
;
560 /***********************************************************************
563 * Rebuild sorted list of symbols for a module.
565 static BOOL
resort_symbols(struct module
* module
)
570 struct hash_table_iter hti
;
572 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
573 while ((ptr
= hash_table_iter_up(&hti
)))
576 if (!(module
->module
.NumSyms
= nsym
)) return FALSE
;
578 if (module
->addr_sorttab
)
579 module
->addr_sorttab
= HeapReAlloc(GetProcessHeap(), 0,
580 module
->addr_sorttab
,
581 nsym
* sizeof(struct symt_ht
*));
583 module
->addr_sorttab
= HeapAlloc(GetProcessHeap(), 0,
584 nsym
* sizeof(struct symt_ht
*));
585 if (!module
->addr_sorttab
) return FALSE
;
588 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
589 while ((ptr
= hash_table_iter_up(&hti
)))
591 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
593 module
->addr_sorttab
[nsym
++] = sym
;
596 qsort(module
->addr_sorttab
, nsym
, sizeof(struct symt_ht
*), symt_cmp_addr
);
597 return module
->sortlist_valid
= TRUE
;
600 /* assume addr is in module */
601 int symt_find_nearest(struct module
* module
, DWORD addr
)
607 if (!module
->sortlist_valid
|| !module
->addr_sorttab
)
609 if (!resort_symbols(module
)) return -1;
613 * Binary search to find closest symbol.
616 high
= module
->module
.NumSyms
;
618 symt_get_info(&module
->addr_sorttab
[0]->symt
, TI_GET_ADDRESS
, &ref_addr
);
619 if (addr
< ref_addr
) return -1;
622 symt_get_info(&module
->addr_sorttab
[high
- 1]->symt
, TI_GET_ADDRESS
, &ref_addr
);
623 if (!symt_get_info(&module
->addr_sorttab
[high
- 1]->symt
, TI_GET_LENGTH
, &ref_size
) || !ref_size
)
624 ref_size
= 0x1000; /* arbitrary value */
625 if (addr
>= ref_addr
+ ref_size
) return -1;
628 while (high
> low
+ 1)
630 mid
= (high
+ low
) / 2;
631 if (cmp_sorttab_addr(module
, mid
, addr
) < 0)
636 if (low
!= high
&& high
!= module
->module
.NumSyms
&&
637 cmp_sorttab_addr(module
, high
, addr
) <= 0)
640 /* If found symbol is a public symbol, check if there are any other entries that
641 * might also have the same address, but would get better information
643 if (module
->addr_sorttab
[low
]->symt
.tag
== SymTagPublicSymbol
)
645 symt_get_info(&module
->addr_sorttab
[low
]->symt
, TI_GET_ADDRESS
, &ref_addr
);
647 module
->addr_sorttab
[low
- 1]->symt
.tag
!= SymTagPublicSymbol
&&
648 !cmp_sorttab_addr(module
, low
- 1, ref_addr
))
650 else if (low
< module
->module
.NumSyms
- 1 &&
651 module
->addr_sorttab
[low
+ 1]->symt
.tag
!= SymTagPublicSymbol
&&
652 !cmp_sorttab_addr(module
, low
+ 1, ref_addr
))
655 /* finally check that we fit into the found symbol */
656 symt_get_info(&module
->addr_sorttab
[low
]->symt
, TI_GET_ADDRESS
, &ref_addr
);
657 if (addr
< ref_addr
) return -1;
658 if (!symt_get_info(&module
->addr_sorttab
[high
- 1]->symt
, TI_GET_LENGTH
, &ref_size
) || !ref_size
)
659 ref_size
= 0x1000; /* arbitrary value */
660 if (addr
>= ref_addr
+ ref_size
) return -1;
665 static BOOL
symt_enum_locals_helper(struct process
* pcs
, struct module
* module
,
666 regex_t
* preg
, PSYM_ENUMERATESYMBOLS_CALLBACK cb
,
667 PVOID user
, SYMBOL_INFO
* sym_info
,
670 struct symt
** plsym
= NULL
;
671 struct symt
* lsym
= NULL
;
672 DWORD pc
= pcs
->ctx_frame
.InstructionOffset
;
674 while ((plsym
= vector_iter_up(v
, plsym
)))
681 struct symt_block
* block
= (struct symt_block
*)lsym
;
682 if (pc
< block
->address
|| block
->address
+ block
->size
<= pc
)
684 if (!symt_enum_locals_helper(pcs
, module
, preg
, cb
, user
,
685 sym_info
, &block
->vchildren
))
690 if (regexec(preg
, symt_get_name(lsym
), 0, NULL
, 0) == 0)
692 symt_fill_sym_info(module
, lsym
, sym_info
);
693 if (!cb(sym_info
, sym_info
->Size
, user
))
698 case SymTagFuncDebugStart
:
699 case SymTagFuncDebugEnd
:
702 FIXME("Unknown type: %u (%x)\n", lsym
->tag
, lsym
->tag
);
709 static BOOL
symt_enum_locals(struct process
* pcs
, const char* mask
,
710 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
713 struct module
* module
;
715 char buffer
[sizeof(SYMBOL_INFO
) + 256];
716 SYMBOL_INFO
* sym_info
= (SYMBOL_INFO
*)buffer
;
717 DWORD pc
= pcs
->ctx_frame
.InstructionOffset
;
720 sym_info
->SizeOfStruct
= sizeof(*sym_info
);
721 sym_info
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
723 module
= module_find_by_addr(pcs
, pc
, DMT_UNKNOWN
);
724 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
725 if ((idx
= symt_find_nearest(module
, pc
)) == -1) return FALSE
;
727 sym
= module
->addr_sorttab
[idx
];
728 if (sym
->symt
.tag
== SymTagFunction
)
733 compile_regex(mask
? mask
: "*", -1, &preg
);
734 ret
= symt_enum_locals_helper(pcs
, module
, &preg
, EnumSymbolsCallback
,
735 UserContext
, sym_info
,
736 &((struct symt_function
*)sym
)->vchildren
);
741 symt_fill_sym_info(module
, &sym
->symt
, sym_info
);
742 return EnumSymbolsCallback(sym_info
, sym_info
->Size
, UserContext
);
745 /******************************************************************
746 * SymEnumSymbols (DBGHELP.@)
748 * cases BaseOfDll = 0
749 * !foo fails always (despite what MSDN states)
750 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
751 * no ! in Mask, lookup in local Context
752 * cases BaseOfDll != 0
753 * !foo fails always (despite what MSDN states)
754 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
756 BOOL WINAPI
SymEnumSymbols(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR Mask
,
757 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
760 struct process
* pcs
= process_find_by_handle(hProcess
);
761 struct module
* module
;
762 struct module
* dbg_module
;
764 regex_t mod_regex
, sym_regex
;
766 TRACE("(%p %s %s %p %p)\n",
767 hProcess
, wine_dbgstr_longlong(BaseOfDll
), debugstr_a(Mask
),
768 EnumSymbolsCallback
, UserContext
);
770 if (!pcs
) return FALSE
;
774 /* do local variables ? */
775 if (!Mask
|| !(bang
= strchr(Mask
, '!')))
776 return symt_enum_locals(pcs
, Mask
, EnumSymbolsCallback
, UserContext
);
778 if (bang
== Mask
) return FALSE
;
780 compile_regex(Mask
, bang
- Mask
, &mod_regex
);
781 compile_regex(bang
+ 1, -1, &sym_regex
);
783 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
785 if (module
->type
== DMT_PE
&& (dbg_module
= module_get_debug(pcs
, module
)))
787 if (regexec(&mod_regex
, module
->module
.ModuleName
, 0, NULL
, 0) == 0 &&
788 symt_enum_module(dbg_module
, &sym_regex
,
789 EnumSymbolsCallback
, UserContext
))
793 /* not found in PE modules, retry on the ELF ones
795 if (!module
&& (dbghelp_options
& SYMOPT_WINE_WITH_ELF_MODULES
))
797 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
799 if (module
->type
== DMT_ELF
&&
800 !module_get_containee(pcs
, module
) &&
801 (dbg_module
= module_get_debug(pcs
, module
)))
803 if (regexec(&mod_regex
, module
->module
.ModuleName
, 0, NULL
, 0) == 0 &&
804 symt_enum_module(dbg_module
, &sym_regex
, EnumSymbolsCallback
, UserContext
))
813 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
814 if (!(module
= module_get_debug(pcs
, module
)))
817 /* we always ignore module name from Mask when BaseOfDll is defined */
818 if (Mask
&& (bang
= strchr(Mask
, '!')))
820 if (bang
== Mask
) return FALSE
;
824 compile_regex(Mask
? Mask
: "*", -1, &sym_regex
);
825 symt_enum_module(module
, &sym_regex
, EnumSymbolsCallback
, UserContext
);
834 PSYM_ENUMSYMBOLS_CALLBACK cb
;
837 static BOOL CALLBACK
sym_enumerate_cb(PSYMBOL_INFO syminfo
, ULONG size
, void* ctx
)
839 struct sym_enumerate
* se
= (struct sym_enumerate
*)ctx
;
840 return (se
->cb
)(syminfo
->Name
, syminfo
->Address
, syminfo
->Size
, se
->ctx
);
843 /***********************************************************************
844 * SymEnumerateSymbols (DBGHELP.@)
846 BOOL WINAPI
SymEnumerateSymbols(HANDLE hProcess
, DWORD BaseOfDll
,
847 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback
,
850 struct sym_enumerate se
;
852 se
.ctx
= UserContext
;
853 se
.cb
= EnumSymbolsCallback
;
855 return SymEnumSymbols(hProcess
, BaseOfDll
, NULL
, sym_enumerate_cb
, &se
);
858 /******************************************************************
859 * SymFromAddr (DBGHELP.@)
862 BOOL WINAPI
SymFromAddr(HANDLE hProcess
, DWORD64 Address
,
863 DWORD64
* Displacement
, PSYMBOL_INFO Symbol
)
865 struct process
* pcs
= process_find_by_handle(hProcess
);
866 struct module
* module
;
870 if (!pcs
) return FALSE
;
871 module
= module_find_by_addr(pcs
, Address
, DMT_UNKNOWN
);
872 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
873 if ((idx
= symt_find_nearest(module
, Address
)) == -1) return FALSE
;
875 sym
= module
->addr_sorttab
[idx
];
877 symt_fill_sym_info(module
, &sym
->symt
, Symbol
);
878 *Displacement
= Address
- Symbol
->Address
;
882 /******************************************************************
883 * SymGetSymFromAddr (DBGHELP.@)
886 BOOL WINAPI
SymGetSymFromAddr(HANDLE hProcess
, DWORD Address
,
887 PDWORD Displacement
, PIMAGEHLP_SYMBOL Symbol
)
889 char buffer
[sizeof(SYMBOL_INFO
) + 256];
890 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
892 DWORD64 Displacement64
;
894 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
895 si
->SizeOfStruct
= sizeof(*si
);
896 si
->MaxNameLen
= 256;
897 if (!SymFromAddr(hProcess
, Address
, &Displacement64
, si
))
901 *Displacement
= Displacement64
;
902 Symbol
->Address
= si
->Address
;
903 Symbol
->Size
= si
->Size
;
904 Symbol
->Flags
= si
->Flags
;
905 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
906 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
910 /******************************************************************
911 * SymFromName (DBGHELP.@)
914 BOOL WINAPI
SymFromName(HANDLE hProcess
, LPSTR Name
, PSYMBOL_INFO Symbol
)
916 struct process
* pcs
= process_find_by_handle(hProcess
);
917 struct module
* module
;
918 struct hash_table_iter hti
;
920 struct symt_ht
* sym
= NULL
;
923 TRACE("(%p, %s, %p)\n", hProcess
, Name
, Symbol
);
924 if (!pcs
) return FALSE
;
925 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
926 name
= strchr(Name
, '!');
930 assert(name
- Name
< sizeof(tmp
));
931 memcpy(tmp
, Name
, name
- Name
);
932 tmp
[name
- Name
] = '\0';
933 module
= module_find_by_name(pcs
, tmp
, DMT_UNKNOWN
);
934 if (!module
) return FALSE
;
935 Name
= (char*)(name
+ 1);
937 else module
= pcs
->lmodules
;
939 /* FIXME: Name could be made out of a regular expression */
940 for (; module
; module
= (name
) ? NULL
: module
->next
)
942 if (module
->module
.SymType
== SymNone
) continue;
943 if (module
->module
.SymType
== SymDeferred
)
945 struct module
* xmodule
= module_get_debug(pcs
, module
);
946 if (!xmodule
|| xmodule
!= module
) continue;
948 hash_table_iter_init(&module
->ht_symbols
, &hti
, Name
);
949 while ((ptr
= hash_table_iter_up(&hti
)))
951 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
953 if (!strcmp(sym
->hash_elt
.name
, Name
))
955 symt_fill_sym_info(module
, &sym
->symt
, Symbol
);
963 /***********************************************************************
964 * SymGetSymFromName (DBGHELP.@)
966 BOOL WINAPI
SymGetSymFromName(HANDLE hProcess
, LPSTR Name
, PIMAGEHLP_SYMBOL Symbol
)
968 char buffer
[sizeof(SYMBOL_INFO
) + 256];
969 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
972 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
973 si
->SizeOfStruct
= sizeof(*si
);
974 si
->MaxNameLen
= 256;
975 if (!SymFromName(hProcess
, Name
, si
)) return FALSE
;
977 Symbol
->Address
= si
->Address
;
978 Symbol
->Size
= si
->Size
;
979 Symbol
->Flags
= si
->Flags
;
980 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
981 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
985 /******************************************************************
986 * sym_fill_func_line_info
988 * fills information about a file
990 BOOL
symt_fill_func_line_info(struct module
* module
, struct symt_function
* func
,
991 DWORD addr
, IMAGEHLP_LINE
* line
)
993 struct line_info
* dli
= NULL
;
996 assert(func
->symt
.tag
== SymTagFunction
);
998 while ((dli
= vector_iter_down(&func
->vlines
, dli
)))
1000 if (!dli
->is_source_file
)
1002 if (found
|| dli
->u
.pc_offset
> addr
) continue;
1003 line
->LineNumber
= dli
->line_number
;
1004 line
->Address
= dli
->u
.pc_offset
;
1011 line
->FileName
= (char*)source_get(module
, dli
->u
.source_file
);
1018 /***********************************************************************
1019 * SymGetSymNext (DBGHELP.@)
1021 BOOL WINAPI
SymGetSymNext(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1024 * get module from Symbol.Address
1025 * get index in module.addr_sorttab of Symbol.Address
1027 * if out of module bounds, move to next module in process address space
1029 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1030 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1034 /***********************************************************************
1035 * SymGetSymPrev (DBGHELP.@)
1038 BOOL WINAPI
SymGetSymPrev(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1040 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1041 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1045 /******************************************************************
1046 * SymGetLineFromAddr (DBGHELP.@)
1049 BOOL WINAPI
SymGetLineFromAddr(HANDLE hProcess
, DWORD dwAddr
,
1050 PDWORD pdwDisplacement
, PIMAGEHLP_LINE Line
)
1052 struct process
* pcs
= process_find_by_handle(hProcess
);
1053 struct module
* module
;
1056 TRACE("%p %08lx %p %p\n", hProcess
, dwAddr
, pdwDisplacement
, Line
);
1058 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1060 if (!pcs
) return FALSE
;
1061 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1062 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
1063 if ((idx
= symt_find_nearest(module
, dwAddr
)) == -1) return FALSE
;
1065 if (module
->addr_sorttab
[idx
]->symt
.tag
!= SymTagFunction
) return FALSE
;
1066 if (!symt_fill_func_line_info(module
,
1067 (struct symt_function
*)module
->addr_sorttab
[idx
],
1068 dwAddr
, Line
)) return FALSE
;
1069 *pdwDisplacement
= dwAddr
- Line
->Address
;
1073 /******************************************************************
1074 * SymGetLinePrev (DBGHELP.@)
1077 BOOL WINAPI
SymGetLinePrev(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
1079 struct process
* pcs
= process_find_by_handle(hProcess
);
1080 struct module
* module
;
1081 struct line_info
* li
;
1082 BOOL in_search
= FALSE
;
1084 TRACE("(%p %p)\n", hProcess
, Line
);
1086 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1088 if (!pcs
) return FALSE
;
1089 module
= module_find_by_addr(pcs
, Line
->Address
, DMT_UNKNOWN
);
1090 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
1092 if (Line
->Key
== 0) return FALSE
;
1093 li
= (struct line_info
*)Line
->Key
;
1094 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1095 * element we have to go back until we find the prev one to get the real
1096 * source file name for the DLIT_OFFSET element just before
1097 * the first DLIT_SOURCEFILE
1099 while (!li
->is_first
)
1102 if (!li
->is_source_file
)
1104 Line
->LineNumber
= li
->line_number
;
1105 Line
->Address
= li
->u
.pc_offset
;
1107 if (!in_search
) return TRUE
;
1113 Line
->FileName
= (char*)source_get(module
, li
->u
.source_file
);
1119 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
1123 BOOL
symt_get_func_line_next(struct module
* module
, PIMAGEHLP_LINE line
)
1125 struct line_info
* li
;
1127 if (line
->Key
== 0) return FALSE
;
1128 li
= (struct line_info
*)line
->Key
;
1129 while (!li
->is_last
)
1132 if (!li
->is_source_file
)
1134 line
->LineNumber
= li
->line_number
;
1135 line
->Address
= li
->u
.pc_offset
;
1139 line
->FileName
= (char*)source_get(module
, li
->u
.source_file
);
1144 /******************************************************************
1145 * SymGetLineNext (DBGHELP.@)
1148 BOOL WINAPI
SymGetLineNext(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
1150 struct process
* pcs
= process_find_by_handle(hProcess
);
1151 struct module
* module
;
1153 TRACE("(%p %p)\n", hProcess
, Line
);
1155 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1156 if (!pcs
) return FALSE
;
1157 module
= module_find_by_addr(pcs
, Line
->Address
, DMT_UNKNOWN
);
1158 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
1160 if (symt_get_func_line_next(module
, Line
)) return TRUE
;
1161 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
1165 /***********************************************************************
1166 * SymFunctionTableAccess (DBGHELP.@)
1168 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1170 FIXME("(%p, 0x%08lx): stub\n", hProcess
, AddrBase
);
1171 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1175 /***********************************************************************
1176 * SymUnDName (DBGHELP.@)
1178 BOOL WINAPI
SymUnDName(PIMAGEHLP_SYMBOL sym
, LPSTR UnDecName
, DWORD UnDecNameLength
)
1180 TRACE("(%p %s %lu): stub\n", sym
, UnDecName
, UnDecNameLength
);
1181 return UnDecorateSymbolName(sym
->Name
, UnDecName
, UnDecNameLength
,
1182 UNDNAME_COMPLETE
) != 0;
1185 static void* und_alloc(size_t len
) { return HeapAlloc(GetProcessHeap(), 0, len
); }
1186 static void und_free (void* ptr
) { HeapFree(GetProcessHeap(), 0, ptr
); }
1188 /***********************************************************************
1189 * UnDecorateSymbolName (DBGHELP.@)
1191 DWORD WINAPI
UnDecorateSymbolName(LPCSTR DecoratedName
, LPSTR UnDecoratedName
,
1192 DWORD UndecoratedLength
, DWORD Flags
)
1194 /* undocumented from msvcrt */
1195 static char* (*p_undname
)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1196 static WCHAR szMsvcrt
[] = {'m','s','v','c','r','t','.','d','l','l',0};
1198 TRACE("(%s, %p, %ld, 0x%08lx): stub\n",
1199 debugstr_a(DecoratedName
), UnDecoratedName
, UndecoratedLength
, Flags
);
1203 if (!hMsvcrt
) hMsvcrt
= LoadLibraryW(szMsvcrt
);
1204 if (hMsvcrt
) p_undname
= (void*)GetProcAddress(hMsvcrt
, "__unDName");
1205 if (!p_undname
) return 0;
1208 if (!UnDecoratedName
) return 0;
1209 if (!p_undname(UnDecoratedName
, DecoratedName
, UndecoratedLength
,
1210 und_alloc
, und_free
, Flags
))
1212 return strlen(UnDecoratedName
);