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 (%s) and src (%lu)\n",
200 module
->module
.ModuleName
, name
,
201 wine_dbgstr_longlong(tsz
), size
);
205 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
212 struct symt_function
* symt_new_function(struct module
* module
,
213 struct symt_compiland
* compiland
,
215 unsigned long addr
, unsigned long size
,
216 struct symt
* sig_type
)
218 struct symt_function
* sym
;
221 TRACE_(dbghelp_symt
)("Adding global function %s:%s @%lx-%lx\n",
222 module
->module
.ModuleName
, name
, addr
, addr
+ size
- 1);
224 assert(!sig_type
|| sig_type
->tag
== SymTagFunctionType
);
225 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
227 sym
->symt
.tag
= SymTagFunction
;
228 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
229 hash_table_add(&module
->ht_symbols
, &sym
->hash_elt
);
230 module
->sortlist_valid
= FALSE
;
231 sym
->container
= &compiland
->symt
;
233 sym
->type
= sig_type
;
235 vector_init(&sym
->vlines
, sizeof(struct line_info
), 64);
236 vector_init(&sym
->vchildren
, sizeof(struct symt
*), 8);
239 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
246 void symt_add_func_line(struct module
* module
, struct symt_function
* func
,
247 unsigned source_idx
, int line_num
, unsigned long offset
)
249 struct line_info
* dli
;
250 BOOL last_matches
= FALSE
;
252 if (func
== NULL
|| !(dbghelp_options
& SYMOPT_LOAD_LINES
)) return;
254 TRACE_(dbghelp_symt
)("(%p)%s:%lx %s:%u\n",
255 func
, func
->hash_elt
.name
, offset
,
256 source_get(module
, source_idx
), line_num
);
258 assert(func
->symt
.tag
== SymTagFunction
);
261 while ((dli
= vector_iter_down(&func
->vlines
, dli
)))
263 if (dli
->is_source_file
)
265 last_matches
= (source_idx
== dli
->u
.source_file
);
272 /* we shouldn't have line changes on first line of function */
273 dli
= vector_add(&func
->vlines
, &module
->pool
);
274 dli
->is_source_file
= 1;
275 dli
->is_first
= dli
->is_last
= 0;
276 dli
->line_number
= 0;
277 dli
->u
.source_file
= source_idx
;
279 dli
= vector_add(&func
->vlines
, &module
->pool
);
280 dli
->is_source_file
= 0;
281 dli
->is_first
= dli
->is_last
= 0;
282 dli
->line_number
= line_num
;
283 dli
->u
.pc_offset
= func
->address
+ offset
;
286 struct symt_data
* symt_add_func_local(struct module
* module
,
287 struct symt_function
* func
,
288 int regno
, int offset
,
289 struct symt_block
* block
,
290 struct symt
* type
, const char* name
)
292 struct symt_data
* locsym
;
296 assert(func
->symt
.tag
== SymTagFunction
);
298 TRACE_(dbghelp_symt
)("Adding local symbol (%s:%s): %s %p\n",
299 module
->module
.ModuleName
, func
->hash_elt
.name
,
301 locsym
= pool_alloc(&module
->pool
, sizeof(*locsym
));
302 locsym
->symt
.tag
= SymTagData
;
303 locsym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
304 locsym
->hash_elt
.next
= NULL
;
305 locsym
->kind
= (offset
< 0) ? DataIsParam
: DataIsLocal
;
306 locsym
->container
= &block
->symt
;
310 locsym
->u
.s
.reg_id
= regno
;
311 locsym
->u
.s
.offset
= 0;
312 locsym
->u
.s
.length
= 0;
316 locsym
->u
.s
.reg_id
= 0;
317 locsym
->u
.s
.offset
= offset
* 8;
318 locsym
->u
.s
.length
= 0;
321 p
= vector_add(&block
->vchildren
, &module
->pool
);
323 p
= vector_add(&func
->vchildren
, &module
->pool
);
328 struct symt_block
* symt_open_func_block(struct module
* module
,
329 struct symt_function
* func
,
330 struct symt_block
* parent_block
,
331 unsigned pc
, unsigned len
)
333 struct symt_block
* block
;
337 assert(func
->symt
.tag
== SymTagFunction
);
339 assert(!parent_block
|| parent_block
->symt
.tag
== SymTagBlock
);
340 block
= pool_alloc(&module
->pool
, sizeof(*block
));
341 block
->symt
.tag
= SymTagBlock
;
342 block
->address
= func
->address
+ pc
;
344 block
->container
= parent_block
? &parent_block
->symt
: &func
->symt
;
345 vector_init(&block
->vchildren
, sizeof(struct symt
*), 4);
347 p
= vector_add(&parent_block
->vchildren
, &module
->pool
);
349 p
= vector_add(&func
->vchildren
, &module
->pool
);
355 struct symt_block
* symt_close_func_block(struct module
* module
,
356 struct symt_function
* func
,
357 struct symt_block
* block
, unsigned pc
)
359 assert(func
->symt
.tag
== SymTagFunction
);
361 if (pc
) block
->size
= func
->address
+ pc
- block
->address
;
362 return (block
->container
->tag
== SymTagBlock
) ?
363 GET_ENTRY(block
->container
, struct symt_block
, symt
) : NULL
;
366 struct symt_function_point
* symt_add_function_point(struct module
* module
,
367 struct symt_function
* func
,
368 enum SymTagEnum point
,
369 unsigned offset
, const char* name
)
371 struct symt_function_point
* sym
;
374 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
376 sym
->symt
.tag
= point
;
378 sym
->offset
= offset
;
379 sym
->name
= name
? pool_strdup(&module
->pool
, name
) : NULL
;
380 p
= vector_add(&func
->vchildren
, &module
->pool
);
386 BOOL
symt_normalize_function(struct module
* module
, struct symt_function
* func
)
389 struct line_info
* dli
;
392 /* We aren't adding any more locals or line numbers to this function.
393 * Free any spare memory that we might have allocated.
395 assert(func
->symt
.tag
== SymTagFunction
);
397 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
398 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
400 len
= vector_length(&func
->vlines
);
403 dli
= vector_at(&func
->vlines
, 0); dli
->is_first
= 1;
404 dli
= vector_at(&func
->vlines
, len
); dli
->is_last
= 1;
409 struct symt_thunk
* symt_new_thunk(struct module
* module
,
410 struct symt_compiland
* compiland
,
411 const char* name
, THUNK_ORDINAL ord
,
412 unsigned long addr
, unsigned long size
)
414 struct symt_thunk
* sym
;
416 TRACE_(dbghelp_symt
)("Adding global thunk %s:%s @%lx-%lx\n",
417 module
->module
.ModuleName
, name
, addr
, addr
+ size
- 1);
419 if ((sym
= pool_alloc(&module
->pool
, sizeof(*sym
))))
421 sym
->symt
.tag
= SymTagThunk
;
422 sym
->hash_elt
.name
= pool_strdup(&module
->pool
, name
);
423 hash_table_add(&module
->ht_symbols
, &sym
->hash_elt
);
424 module
->sortlist_valid
= FALSE
;
425 sym
->container
= &compiland
->symt
;
432 p
= vector_add(&compiland
->vchildren
, &module
->pool
);
439 /* expect sym_info->MaxNameLen to be set before being called */
440 static void symt_fill_sym_info(const struct module
* module
,
441 const struct symt
* sym
, SYMBOL_INFO
* sym_info
)
446 if (!symt_get_info(sym
, TI_GET_TYPE
, &sym_info
->TypeIndex
))
447 sym_info
->TypeIndex
= 0;
448 sym_info
->info
= (DWORD
)sym
;
449 symt_get_info(sym
, TI_GET_LENGTH
, &size
);
450 sym_info
->Size
= (DWORD
)size
;
451 sym_info
->ModBase
= module
->module
.BaseOfImage
;
457 const struct symt_data
* data
= (const struct symt_data
*)sym
;
462 if (data
->u
.s
.reg_id
)
464 sym_info
->Flags
|= SYMFLAG_REGISTER
;
465 sym_info
->Register
= data
->u
.s
.reg_id
;
466 sym_info
->Address
= 0;
470 if (data
->u
.s
.offset
< 0)
471 sym_info
->Flags
|= SYMFLAG_LOCAL
| SYMFLAG_FRAMEREL
;
473 sym_info
->Flags
|= SYMFLAG_LOCAL
| SYMFLAG_PARAMETER
| SYMFLAG_FRAMEREL
;
474 /* FIXME: needed ? moreover, it's i386 dependent !!! */
475 sym_info
->Register
= CV_REG_EBP
;
476 sym_info
->Address
= data
->u
.s
.offset
/ 8;
480 case DataIsFileStatic
:
481 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
482 sym_info
->Register
= 0;
485 sym_info
->Flags
|= SYMFLAG_VALUEPRESENT
;
486 switch (data
->u
.value
.n1
.n2
.vt
)
488 case VT_I4
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.lVal
; break;
489 case VT_I2
: sym_info
->Value
= (ULONG
)(long)data
->u
.value
.n1
.n2
.n3
.iVal
; break;
490 case VT_I1
: sym_info
->Value
= (ULONG
)(long)data
->u
.value
.n1
.n2
.n3
.cVal
; break;
491 case VT_UI4
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.ulVal
; break;
492 case VT_UI2
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.uiVal
; break;
493 case VT_UI1
: sym_info
->Value
= (ULONG
)data
->u
.value
.n1
.n2
.n3
.bVal
; break;
495 FIXME("Unsupported variant type (%u)\n", data
->u
.value
.n1
.n2
.vt
);
499 FIXME("Unhandled kind (%u) in sym data\n", data
->kind
);
503 case SymTagPublicSymbol
:
504 sym_info
->Flags
|= SYMFLAG_EXPORT
;
505 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
508 sym_info
->Flags
|= SYMFLAG_FUNCTION
;
509 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
512 sym_info
->Flags
|= SYMFLAG_THUNK
;
513 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
516 symt_get_info(sym
, TI_GET_ADDRESS
, &sym_info
->Address
);
517 sym_info
->Register
= 0;
520 sym_info
->Scope
= 0; /* FIXME */
521 sym_info
->Tag
= sym
->tag
;
522 name
= symt_get_name(sym
);
523 if (sym_info
->MaxNameLen
)
525 if (sym
->tag
!= SymTagPublicSymbol
|| !(dbghelp_options
& SYMOPT_UNDNAME
) ||
526 (sym_info
->NameLen
= UnDecorateSymbolName(name
, sym_info
->Name
,
527 sym_info
->MaxNameLen
, UNDNAME_COMPLETE
) == 0))
529 sym_info
->NameLen
= min(strlen(name
), sym_info
->MaxNameLen
- 1);
530 memcpy(sym_info
->Name
, name
, sym_info
->NameLen
);
531 sym_info
->Name
[sym_info
->NameLen
] = '\0';
534 TRACE_(dbghelp_symt
)("%p => %s %lu %s\n",
535 sym
, sym_info
->Name
, sym_info
->Size
,
536 wine_dbgstr_longlong(sym_info
->Address
));
539 static BOOL
symt_enum_module(struct module
* module
, regex_t
* regex
,
540 PSYM_ENUMERATESYMBOLS_CALLBACK cb
, PVOID user
)
542 char buffer
[sizeof(SYMBOL_INFO
) + 256];
543 SYMBOL_INFO
* sym_info
= (SYMBOL_INFO
*)buffer
;
545 struct symt_ht
* sym
= NULL
;
546 struct hash_table_iter hti
;
548 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
549 while ((ptr
= hash_table_iter_up(&hti
)))
551 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
552 if (sym
->hash_elt
.name
&&
553 regexec(regex
, sym
->hash_elt
.name
, 0, NULL
, 0) == 0)
555 sym_info
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
556 sym_info
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
557 symt_fill_sym_info(module
, &sym
->symt
, sym_info
);
558 if (!cb(sym_info
, sym_info
->Size
, user
)) return TRUE
;
564 /***********************************************************************
567 * Rebuild sorted list of symbols for a module.
569 static BOOL
resort_symbols(struct module
* module
)
574 struct hash_table_iter hti
;
576 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
577 while ((ptr
= hash_table_iter_up(&hti
)))
580 if (!(module
->module
.NumSyms
= nsym
)) return FALSE
;
582 if (module
->addr_sorttab
)
583 module
->addr_sorttab
= HeapReAlloc(GetProcessHeap(), 0,
584 module
->addr_sorttab
,
585 nsym
* sizeof(struct symt_ht
*));
587 module
->addr_sorttab
= HeapAlloc(GetProcessHeap(), 0,
588 nsym
* sizeof(struct symt_ht
*));
589 if (!module
->addr_sorttab
) return FALSE
;
592 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
593 while ((ptr
= hash_table_iter_up(&hti
)))
595 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
597 module
->addr_sorttab
[nsym
++] = sym
;
600 qsort(module
->addr_sorttab
, nsym
, sizeof(struct symt_ht
*), symt_cmp_addr
);
601 return module
->sortlist_valid
= TRUE
;
604 /* assume addr is in module */
605 int symt_find_nearest(struct module
* module
, DWORD addr
)
608 ULONG64 ref_addr
, ref_size
;
610 if (!module
->sortlist_valid
|| !module
->addr_sorttab
)
612 if (!resort_symbols(module
)) return -1;
616 * Binary search to find closest symbol.
619 high
= module
->module
.NumSyms
;
621 symt_get_info(&module
->addr_sorttab
[0]->symt
, TI_GET_ADDRESS
, &ref_addr
);
622 if (addr
< ref_addr
) return -1;
625 symt_get_info(&module
->addr_sorttab
[high
- 1]->symt
, TI_GET_ADDRESS
, &ref_addr
);
626 if (!symt_get_info(&module
->addr_sorttab
[high
- 1]->symt
, TI_GET_LENGTH
, &ref_size
) || !ref_size
)
627 ref_size
= 0x1000; /* arbitrary value */
628 if (addr
>= ref_addr
+ ref_size
) return -1;
631 while (high
> low
+ 1)
633 mid
= (high
+ low
) / 2;
634 if (cmp_sorttab_addr(module
, mid
, addr
) < 0)
639 if (low
!= high
&& high
!= module
->module
.NumSyms
&&
640 cmp_sorttab_addr(module
, high
, addr
) <= 0)
643 /* If found symbol is a public symbol, check if there are any other entries that
644 * might also have the same address, but would get better information
646 if (module
->addr_sorttab
[low
]->symt
.tag
== SymTagPublicSymbol
)
648 symt_get_info(&module
->addr_sorttab
[low
]->symt
, TI_GET_ADDRESS
, &ref_addr
);
650 module
->addr_sorttab
[low
- 1]->symt
.tag
!= SymTagPublicSymbol
&&
651 !cmp_sorttab_addr(module
, low
- 1, ref_addr
))
653 else if (low
< module
->module
.NumSyms
- 1 &&
654 module
->addr_sorttab
[low
+ 1]->symt
.tag
!= SymTagPublicSymbol
&&
655 !cmp_sorttab_addr(module
, low
+ 1, ref_addr
))
658 /* finally check that we fit into the found symbol */
659 symt_get_info(&module
->addr_sorttab
[low
]->symt
, TI_GET_ADDRESS
, &ref_addr
);
660 if (addr
< ref_addr
) return -1;
661 if (!symt_get_info(&module
->addr_sorttab
[high
- 1]->symt
, TI_GET_LENGTH
, &ref_size
) || !ref_size
)
662 ref_size
= 0x1000; /* arbitrary value */
663 if (addr
>= ref_addr
+ ref_size
) return -1;
668 static BOOL
symt_enum_locals_helper(struct process
* pcs
, struct module
* module
,
669 regex_t
* preg
, PSYM_ENUMERATESYMBOLS_CALLBACK cb
,
670 PVOID user
, SYMBOL_INFO
* sym_info
,
673 struct symt
** plsym
= NULL
;
674 struct symt
* lsym
= NULL
;
675 DWORD pc
= pcs
->ctx_frame
.InstructionOffset
;
677 while ((plsym
= vector_iter_up(v
, plsym
)))
684 struct symt_block
* block
= (struct symt_block
*)lsym
;
685 if (pc
< block
->address
|| block
->address
+ block
->size
<= pc
)
687 if (!symt_enum_locals_helper(pcs
, module
, preg
, cb
, user
,
688 sym_info
, &block
->vchildren
))
693 if (regexec(preg
, symt_get_name(lsym
), 0, NULL
, 0) == 0)
695 symt_fill_sym_info(module
, lsym
, sym_info
);
696 if (!cb(sym_info
, sym_info
->Size
, user
))
701 case SymTagFuncDebugStart
:
702 case SymTagFuncDebugEnd
:
705 FIXME("Unknown type: %u (%x)\n", lsym
->tag
, lsym
->tag
);
712 static BOOL
symt_enum_locals(struct process
* pcs
, const char* mask
,
713 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
716 struct module
* module
;
718 char buffer
[sizeof(SYMBOL_INFO
) + 256];
719 SYMBOL_INFO
* sym_info
= (SYMBOL_INFO
*)buffer
;
720 DWORD pc
= pcs
->ctx_frame
.InstructionOffset
;
723 sym_info
->SizeOfStruct
= sizeof(*sym_info
);
724 sym_info
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
726 module
= module_find_by_addr(pcs
, pc
, DMT_UNKNOWN
);
727 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
728 if ((idx
= symt_find_nearest(module
, pc
)) == -1) return FALSE
;
730 sym
= module
->addr_sorttab
[idx
];
731 if (sym
->symt
.tag
== SymTagFunction
)
736 compile_regex(mask
? mask
: "*", -1, &preg
);
737 ret
= symt_enum_locals_helper(pcs
, module
, &preg
, EnumSymbolsCallback
,
738 UserContext
, sym_info
,
739 &((struct symt_function
*)sym
)->vchildren
);
744 symt_fill_sym_info(module
, &sym
->symt
, sym_info
);
745 return EnumSymbolsCallback(sym_info
, sym_info
->Size
, UserContext
);
748 /******************************************************************
749 * SymEnumSymbols (DBGHELP.@)
751 * cases BaseOfDll = 0
752 * !foo fails always (despite what MSDN states)
753 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
754 * no ! in Mask, lookup in local Context
755 * cases BaseOfDll != 0
756 * !foo fails always (despite what MSDN states)
757 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
759 BOOL WINAPI
SymEnumSymbols(HANDLE hProcess
, ULONG64 BaseOfDll
, PCSTR Mask
,
760 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback
,
763 struct process
* pcs
= process_find_by_handle(hProcess
);
764 struct module
* module
;
765 struct module
* dbg_module
;
767 regex_t mod_regex
, sym_regex
;
769 TRACE("(%p %s %s %p %p)\n",
770 hProcess
, wine_dbgstr_longlong(BaseOfDll
), debugstr_a(Mask
),
771 EnumSymbolsCallback
, UserContext
);
773 if (!pcs
) return FALSE
;
777 /* do local variables ? */
778 if (!Mask
|| !(bang
= strchr(Mask
, '!')))
779 return symt_enum_locals(pcs
, Mask
, EnumSymbolsCallback
, UserContext
);
781 if (bang
== Mask
) return FALSE
;
783 compile_regex(Mask
, bang
- Mask
, &mod_regex
);
784 compile_regex(bang
+ 1, -1, &sym_regex
);
786 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
788 if (module
->type
== DMT_PE
&& (dbg_module
= module_get_debug(pcs
, module
)))
790 if (regexec(&mod_regex
, module
->module
.ModuleName
, 0, NULL
, 0) == 0 &&
791 symt_enum_module(dbg_module
, &sym_regex
,
792 EnumSymbolsCallback
, UserContext
))
796 /* not found in PE modules, retry on the ELF ones
798 if (!module
&& (dbghelp_options
& SYMOPT_WINE_WITH_ELF_MODULES
))
800 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
802 if (module
->type
== DMT_ELF
&&
803 !module_get_containee(pcs
, module
) &&
804 (dbg_module
= module_get_debug(pcs
, module
)))
806 if (regexec(&mod_regex
, module
->module
.ModuleName
, 0, NULL
, 0) == 0 &&
807 symt_enum_module(dbg_module
, &sym_regex
, EnumSymbolsCallback
, UserContext
))
816 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
817 if (!(module
= module_get_debug(pcs
, module
)))
820 /* we always ignore module name from Mask when BaseOfDll is defined */
821 if (Mask
&& (bang
= strchr(Mask
, '!')))
823 if (bang
== Mask
) return FALSE
;
827 compile_regex(Mask
? Mask
: "*", -1, &sym_regex
);
828 symt_enum_module(module
, &sym_regex
, EnumSymbolsCallback
, UserContext
);
837 PSYM_ENUMSYMBOLS_CALLBACK cb
;
840 static BOOL CALLBACK
sym_enumerate_cb(PSYMBOL_INFO syminfo
, ULONG size
, void* ctx
)
842 struct sym_enumerate
* se
= (struct sym_enumerate
*)ctx
;
843 return (se
->cb
)(syminfo
->Name
, syminfo
->Address
, syminfo
->Size
, se
->ctx
);
846 /***********************************************************************
847 * SymEnumerateSymbols (DBGHELP.@)
849 BOOL WINAPI
SymEnumerateSymbols(HANDLE hProcess
, DWORD BaseOfDll
,
850 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback
,
853 struct sym_enumerate se
;
855 se
.ctx
= UserContext
;
856 se
.cb
= EnumSymbolsCallback
;
858 return SymEnumSymbols(hProcess
, BaseOfDll
, NULL
, sym_enumerate_cb
, &se
);
861 /******************************************************************
862 * SymFromAddr (DBGHELP.@)
865 BOOL WINAPI
SymFromAddr(HANDLE hProcess
, DWORD64 Address
,
866 DWORD64
* Displacement
, PSYMBOL_INFO Symbol
)
868 struct process
* pcs
= process_find_by_handle(hProcess
);
869 struct module
* module
;
873 if (!pcs
) return FALSE
;
874 module
= module_find_by_addr(pcs
, Address
, DMT_UNKNOWN
);
875 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
876 if ((idx
= symt_find_nearest(module
, Address
)) == -1) return FALSE
;
878 sym
= module
->addr_sorttab
[idx
];
880 symt_fill_sym_info(module
, &sym
->symt
, Symbol
);
881 *Displacement
= Address
- Symbol
->Address
;
885 /******************************************************************
886 * SymGetSymFromAddr (DBGHELP.@)
889 BOOL WINAPI
SymGetSymFromAddr(HANDLE hProcess
, DWORD Address
,
890 PDWORD Displacement
, PIMAGEHLP_SYMBOL Symbol
)
892 char buffer
[sizeof(SYMBOL_INFO
) + 256];
893 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
895 DWORD64 Displacement64
;
897 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
898 si
->SizeOfStruct
= sizeof(*si
);
899 si
->MaxNameLen
= 256;
900 if (!SymFromAddr(hProcess
, Address
, &Displacement64
, si
))
904 *Displacement
= Displacement64
;
905 Symbol
->Address
= si
->Address
;
906 Symbol
->Size
= si
->Size
;
907 Symbol
->Flags
= si
->Flags
;
908 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
909 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
913 /******************************************************************
914 * SymFromName (DBGHELP.@)
917 BOOL WINAPI
SymFromName(HANDLE hProcess
, LPSTR Name
, PSYMBOL_INFO Symbol
)
919 struct process
* pcs
= process_find_by_handle(hProcess
);
920 struct module
* module
;
921 struct hash_table_iter hti
;
923 struct symt_ht
* sym
= NULL
;
926 TRACE("(%p, %s, %p)\n", hProcess
, Name
, Symbol
);
927 if (!pcs
) return FALSE
;
928 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
929 name
= strchr(Name
, '!');
933 assert(name
- Name
< sizeof(tmp
));
934 memcpy(tmp
, Name
, name
- Name
);
935 tmp
[name
- Name
] = '\0';
936 module
= module_find_by_name(pcs
, tmp
, DMT_UNKNOWN
);
937 if (!module
) return FALSE
;
938 Name
= (char*)(name
+ 1);
940 else module
= pcs
->lmodules
;
942 /* FIXME: Name could be made out of a regular expression */
943 for (; module
; module
= (name
) ? NULL
: module
->next
)
945 if (module
->module
.SymType
== SymNone
) continue;
946 if (module
->module
.SymType
== SymDeferred
)
948 struct module
* xmodule
= module_get_debug(pcs
, module
);
949 if (!xmodule
|| xmodule
!= module
) continue;
951 hash_table_iter_init(&module
->ht_symbols
, &hti
, Name
);
952 while ((ptr
= hash_table_iter_up(&hti
)))
954 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
956 if (!strcmp(sym
->hash_elt
.name
, Name
))
958 symt_fill_sym_info(module
, &sym
->symt
, Symbol
);
966 /***********************************************************************
967 * SymGetSymFromName (DBGHELP.@)
969 BOOL WINAPI
SymGetSymFromName(HANDLE hProcess
, LPSTR Name
, PIMAGEHLP_SYMBOL Symbol
)
971 char buffer
[sizeof(SYMBOL_INFO
) + 256];
972 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
975 if (Symbol
->SizeOfStruct
< sizeof(*Symbol
)) return FALSE
;
976 si
->SizeOfStruct
= sizeof(*si
);
977 si
->MaxNameLen
= 256;
978 if (!SymFromName(hProcess
, Name
, si
)) return FALSE
;
980 Symbol
->Address
= si
->Address
;
981 Symbol
->Size
= si
->Size
;
982 Symbol
->Flags
= si
->Flags
;
983 len
= min(Symbol
->MaxNameLength
, si
->MaxNameLen
);
984 lstrcpynA(Symbol
->Name
, si
->Name
, len
);
988 /******************************************************************
989 * sym_fill_func_line_info
991 * fills information about a file
993 BOOL
symt_fill_func_line_info(struct module
* module
, struct symt_function
* func
,
994 DWORD addr
, IMAGEHLP_LINE
* line
)
996 struct line_info
* dli
= NULL
;
999 assert(func
->symt
.tag
== SymTagFunction
);
1001 while ((dli
= vector_iter_down(&func
->vlines
, dli
)))
1003 if (!dli
->is_source_file
)
1005 if (found
|| dli
->u
.pc_offset
> addr
) continue;
1006 line
->LineNumber
= dli
->line_number
;
1007 line
->Address
= dli
->u
.pc_offset
;
1014 line
->FileName
= (char*)source_get(module
, dli
->u
.source_file
);
1021 /***********************************************************************
1022 * SymGetSymNext (DBGHELP.@)
1024 BOOL WINAPI
SymGetSymNext(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1027 * get module from Symbol.Address
1028 * get index in module.addr_sorttab of Symbol.Address
1030 * if out of module bounds, move to next module in process address space
1032 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1033 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1037 /***********************************************************************
1038 * SymGetSymPrev (DBGHELP.@)
1041 BOOL WINAPI
SymGetSymPrev(HANDLE hProcess
, PIMAGEHLP_SYMBOL Symbol
)
1043 FIXME("(%p, %p): stub\n", hProcess
, Symbol
);
1044 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1048 /******************************************************************
1049 * SymGetLineFromAddr (DBGHELP.@)
1052 BOOL WINAPI
SymGetLineFromAddr(HANDLE hProcess
, DWORD dwAddr
,
1053 PDWORD pdwDisplacement
, PIMAGEHLP_LINE Line
)
1055 struct process
* pcs
= process_find_by_handle(hProcess
);
1056 struct module
* module
;
1059 TRACE("%p %08lx %p %p\n", hProcess
, dwAddr
, pdwDisplacement
, Line
);
1061 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1063 if (!pcs
) return FALSE
;
1064 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1065 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
1066 if ((idx
= symt_find_nearest(module
, dwAddr
)) == -1) return FALSE
;
1068 if (module
->addr_sorttab
[idx
]->symt
.tag
!= SymTagFunction
) return FALSE
;
1069 if (!symt_fill_func_line_info(module
,
1070 (struct symt_function
*)module
->addr_sorttab
[idx
],
1071 dwAddr
, Line
)) return FALSE
;
1072 *pdwDisplacement
= dwAddr
- Line
->Address
;
1076 /******************************************************************
1077 * copy_line_64_from_32 (internal)
1080 static void copy_line_64_from_32(IMAGEHLP_LINE64
* l64
, const IMAGEHLP_LINE
* l32
)
1083 l64
->Key
= l32
->Key
;
1084 l64
->LineNumber
= l32
->LineNumber
;
1085 l64
->FileName
= l32
->FileName
;
1086 l64
->Address
= l32
->Address
;
1089 /******************************************************************
1090 * copy_line_32_from_64 (internal)
1093 static void copy_line_32_from_64(IMAGEHLP_LINE
* l32
, const IMAGEHLP_LINE64
* l64
)
1096 l32
->Key
= l64
->Key
;
1097 l32
->LineNumber
= l64
->LineNumber
;
1098 l32
->FileName
= l64
->FileName
;
1099 l32
->Address
= l64
->Address
;
1102 /******************************************************************
1103 * SymGetLineFromAddr64 (DBGHELP.@)
1106 BOOL WINAPI
SymGetLineFromAddr64(HANDLE hProcess
, DWORD64 dwAddr
,
1107 PDWORD pdwDisplacement
, PIMAGEHLP_LINE64 Line
)
1109 IMAGEHLP_LINE line32
;
1111 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1112 if (!validate_addr64(dwAddr
)) return FALSE
;
1113 line32
.SizeOfStruct
= sizeof(line32
);
1114 if (!SymGetLineFromAddr(hProcess
, (DWORD
)dwAddr
, pdwDisplacement
, &line32
))
1116 copy_line_64_from_32(Line
, &line32
);
1120 /******************************************************************
1121 * SymGetLinePrev (DBGHELP.@)
1124 BOOL WINAPI
SymGetLinePrev(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
1126 struct process
* pcs
= process_find_by_handle(hProcess
);
1127 struct module
* module
;
1128 struct line_info
* li
;
1129 BOOL in_search
= FALSE
;
1131 TRACE("(%p %p)\n", hProcess
, Line
);
1133 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1135 if (!pcs
) return FALSE
;
1136 module
= module_find_by_addr(pcs
, Line
->Address
, DMT_UNKNOWN
);
1137 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
1139 if (Line
->Key
== 0) return FALSE
;
1140 li
= (struct line_info
*)Line
->Key
;
1141 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1142 * element we have to go back until we find the prev one to get the real
1143 * source file name for the DLIT_OFFSET element just before
1144 * the first DLIT_SOURCEFILE
1146 while (!li
->is_first
)
1149 if (!li
->is_source_file
)
1151 Line
->LineNumber
= li
->line_number
;
1152 Line
->Address
= li
->u
.pc_offset
;
1154 if (!in_search
) return TRUE
;
1160 Line
->FileName
= (char*)source_get(module
, li
->u
.source_file
);
1166 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
1170 /******************************************************************
1171 * SymGetLinePrev64 (DBGHELP.@)
1174 BOOL WINAPI
SymGetLinePrev64(HANDLE hProcess
, PIMAGEHLP_LINE64 Line
)
1176 IMAGEHLP_LINE line32
;
1178 line32
.SizeOfStruct
= sizeof(line32
);
1179 copy_line_32_from_64(&line32
, Line
);
1180 if (!SymGetLinePrev(hProcess
, &line32
)) return FALSE
;
1181 copy_line_64_from_32(Line
, &line32
);
1185 BOOL
symt_get_func_line_next(struct module
* module
, PIMAGEHLP_LINE line
)
1187 struct line_info
* li
;
1189 if (line
->Key
== 0) return FALSE
;
1190 li
= (struct line_info
*)line
->Key
;
1191 while (!li
->is_last
)
1194 if (!li
->is_source_file
)
1196 line
->LineNumber
= li
->line_number
;
1197 line
->Address
= li
->u
.pc_offset
;
1201 line
->FileName
= (char*)source_get(module
, li
->u
.source_file
);
1206 /******************************************************************
1207 * SymGetLineNext (DBGHELP.@)
1210 BOOL WINAPI
SymGetLineNext(HANDLE hProcess
, PIMAGEHLP_LINE Line
)
1212 struct process
* pcs
= process_find_by_handle(hProcess
);
1213 struct module
* module
;
1215 TRACE("(%p %p)\n", hProcess
, Line
);
1217 if (Line
->SizeOfStruct
< sizeof(*Line
)) return FALSE
;
1218 if (!pcs
) return FALSE
;
1219 module
= module_find_by_addr(pcs
, Line
->Address
, DMT_UNKNOWN
);
1220 if (!(module
= module_get_debug(pcs
, module
))) return FALSE
;
1222 if (symt_get_func_line_next(module
, Line
)) return TRUE
;
1223 SetLastError(ERROR_NO_MORE_ITEMS
); /* FIXME */
1227 /******************************************************************
1228 * SymGetLineNext64 (DBGHELP.@)
1231 BOOL WINAPI
SymGetLineNext64(HANDLE hProcess
, PIMAGEHLP_LINE64 Line
)
1233 IMAGEHLP_LINE line32
;
1235 line32
.SizeOfStruct
= sizeof(line32
);
1236 copy_line_32_from_64(&line32
, Line
);
1237 if (!SymGetLineNext(hProcess
, &line32
)) return FALSE
;
1238 copy_line_64_from_32(Line
, &line32
);
1242 /***********************************************************************
1243 * SymFunctionTableAccess (DBGHELP.@)
1245 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1247 FIXME("(%p, 0x%08lx): stub\n", hProcess
, AddrBase
);
1248 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1252 /***********************************************************************
1253 * SymUnDName (DBGHELP.@)
1255 BOOL WINAPI
SymUnDName(PIMAGEHLP_SYMBOL sym
, LPSTR UnDecName
, DWORD UnDecNameLength
)
1257 TRACE("(%p %s %lu): stub\n", sym
, UnDecName
, UnDecNameLength
);
1258 return UnDecorateSymbolName(sym
->Name
, UnDecName
, UnDecNameLength
,
1259 UNDNAME_COMPLETE
) != 0;
1262 static void* und_alloc(size_t len
) { return HeapAlloc(GetProcessHeap(), 0, len
); }
1263 static void und_free (void* ptr
) { HeapFree(GetProcessHeap(), 0, ptr
); }
1265 /***********************************************************************
1266 * UnDecorateSymbolName (DBGHELP.@)
1268 DWORD WINAPI
UnDecorateSymbolName(LPCSTR DecoratedName
, LPSTR UnDecoratedName
,
1269 DWORD UndecoratedLength
, DWORD Flags
)
1271 /* undocumented from msvcrt */
1272 static char* (*p_undname
)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1273 static WCHAR szMsvcrt
[] = {'m','s','v','c','r','t','.','d','l','l',0};
1275 TRACE("(%s, %p, %ld, 0x%08lx): stub\n",
1276 debugstr_a(DecoratedName
), UnDecoratedName
, UndecoratedLength
, Flags
);
1280 if (!hMsvcrt
) hMsvcrt
= LoadLibraryW(szMsvcrt
);
1281 if (hMsvcrt
) p_undname
= (void*)GetProcAddress(hMsvcrt
, "__unDName");
1282 if (!p_undname
) return 0;
1285 if (!UnDecoratedName
) return 0;
1286 if (!p_undname(UnDecoratedName
, DecoratedName
, UndecoratedLength
,
1287 und_alloc
, und_free
, Flags
))
1289 return strlen(UnDecoratedName
);