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, BOOL _case)
86 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
88 BOOL in_escape = FALSE;
89 unsigned flags = REG_NOSUB;
91 re_append(&mask, &len, '^');
93 while (*str && numchar--)
95 /* FIXME: this shouldn't be valid on '-' */
98 re_append(&mask, &len, '\\');
99 re_append(&mask, &len, *str);
104 case '\\': in_escape = TRUE; break;
105 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
106 case '?': re_append(&mask, &len, '.'); break;
107 case '#': re_append(&mask, &len, '*'); break;
108 /* escape some valid characters in dbghelp reg exp:s */
109 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
110 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
111 default: re_append(&mask, &len, *str); break;
117 re_append(&mask, &len, '\\');
118 re_append(&mask, &len, '\\');
120 re_append(&mask, &len, '$');
121 mask[len - 1] = '\0';
122 if (_case) flags |= REG_ICASE;
123 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
124 HeapFree(GetProcessHeap(), 0, mask);
127 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
129 struct symt_compiland* sym;
131 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
132 module->module.ModuleName, name);
133 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
135 sym->symt.tag = SymTagCompiland;
136 sym->source = source_new(module, name);
137 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
142 struct symt_public* symt_new_public(struct module* module,
143 struct symt_compiland* compiland,
145 unsigned long address, unsigned size,
146 BOOL in_code, BOOL is_func)
148 struct symt_public* sym;
151 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
152 module->module.ModuleName, name, address);
153 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
154 symt_find_nearest(module, address) != -1)
156 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
158 sym->symt.tag = SymTagPublicSymbol;
159 sym->hash_elt.name = pool_strdup(&module->pool, name);
160 hash_table_add(&module->ht_symbols, &sym->hash_elt);
161 module->sortlist_valid = FALSE;
162 sym->container = compiland ? &compiland->symt : NULL;
163 sym->address = address;
165 sym->in_code = in_code;
166 sym->is_function = is_func;
169 p = vector_add(&compiland->vchildren, &module->pool);
176 struct symt_data* symt_new_global_variable(struct module* module,
177 struct symt_compiland* compiland,
178 const char* name, unsigned is_static,
179 unsigned long addr, unsigned long size,
182 struct symt_data* sym;
186 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
187 module->module.ModuleName, name, addr, type);
188 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
190 sym->symt.tag = SymTagData;
191 sym->hash_elt.name = pool_strdup(&module->pool, name);
192 hash_table_add(&module->ht_symbols, &sym->hash_elt);
193 module->sortlist_valid = FALSE;
194 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
195 sym->container = compiland ? &compiland->symt : NULL;
197 sym->u.address = addr;
198 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
201 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
202 module->module.ModuleName, name,
203 wine_dbgstr_longlong(tsz), size);
207 p = vector_add(&compiland->vchildren, &module->pool);
214 struct symt_function* symt_new_function(struct module* module,
215 struct symt_compiland* compiland,
217 unsigned long addr, unsigned long size,
218 struct symt* sig_type)
220 struct symt_function* sym;
223 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
224 module->module.ModuleName, name, addr, addr + size - 1);
226 assert(!sig_type || sig_type->tag == SymTagFunctionType);
227 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
229 sym->symt.tag = SymTagFunction;
230 sym->hash_elt.name = pool_strdup(&module->pool, name);
231 hash_table_add(&module->ht_symbols, &sym->hash_elt);
232 module->sortlist_valid = FALSE;
233 sym->container = &compiland->symt;
235 sym->type = sig_type;
237 vector_init(&sym->vlines, sizeof(struct line_info), 64);
238 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
241 p = vector_add(&compiland->vchildren, &module->pool);
248 void symt_add_func_line(struct module* module, struct symt_function* func,
249 unsigned source_idx, int line_num, unsigned long offset)
251 struct line_info* dli;
252 BOOL last_matches = FALSE;
254 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
256 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
257 func, func->hash_elt.name, offset,
258 source_get(module, source_idx), line_num);
260 assert(func->symt.tag == SymTagFunction);
263 while ((dli = vector_iter_down(&func->vlines, dli)))
265 if (dli->is_source_file)
267 last_matches = (source_idx == dli->u.source_file);
274 /* we shouldn't have line changes on first line of function */
275 dli = vector_add(&func->vlines, &module->pool);
276 dli->is_source_file = 1;
277 dli->is_first = dli->is_last = 0;
278 dli->line_number = 0;
279 dli->u.source_file = source_idx;
281 dli = vector_add(&func->vlines, &module->pool);
282 dli->is_source_file = 0;
283 dli->is_first = dli->is_last = 0;
284 dli->line_number = line_num;
285 dli->u.pc_offset = func->address + offset;
288 /******************************************************************
289 * symt_add_func_local
291 * Adds a new local/parameter to a given function:
292 * If regno it's not 0:
293 * - then variable is stored in a register
294 * - if offset is > 0, then it's a parameter to the function (in a register)
295 * - if offset is = 0, then it's a local variable (in a register)
296 * Otherwise, the variable is stored on the stack:
297 * - if offset is > 0, then it's a parameter to the function
298 * - otherwise, it's a local variable
299 * FIXME: this is too i386 centric
301 struct symt_data* symt_add_func_local(struct module* module,
302 struct symt_function* func,
303 int regno, int offset,
304 struct symt_block* block,
305 struct symt* type, const char* name)
307 struct symt_data* locsym;
311 assert(func->symt.tag == SymTagFunction);
313 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
314 module->module.ModuleName, func->hash_elt.name,
316 locsym = pool_alloc(&module->pool, sizeof(*locsym));
317 locsym->symt.tag = SymTagData;
318 locsym->hash_elt.name = pool_strdup(&module->pool, name);
319 locsym->hash_elt.next = NULL;
320 locsym->kind = (offset > 0) ? DataIsParam : DataIsLocal;
321 locsym->container = &block->symt;
325 locsym->u.s.reg_id = regno;
326 locsym->u.s.offset = 0;
327 locsym->u.s.length = 0;
331 locsym->u.s.reg_id = 0;
332 locsym->u.s.offset = offset * 8;
333 locsym->u.s.length = 0;
336 p = vector_add(&block->vchildren, &module->pool);
338 p = vector_add(&func->vchildren, &module->pool);
343 struct symt_block* symt_open_func_block(struct module* module,
344 struct symt_function* func,
345 struct symt_block* parent_block,
346 unsigned pc, unsigned len)
348 struct symt_block* block;
352 assert(func->symt.tag == SymTagFunction);
354 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
355 block = pool_alloc(&module->pool, sizeof(*block));
356 block->symt.tag = SymTagBlock;
357 block->address = func->address + pc;
359 block->container = parent_block ? &parent_block->symt : &func->symt;
360 vector_init(&block->vchildren, sizeof(struct symt*), 4);
362 p = vector_add(&parent_block->vchildren, &module->pool);
364 p = vector_add(&func->vchildren, &module->pool);
370 struct symt_block* symt_close_func_block(struct module* module,
371 struct symt_function* func,
372 struct symt_block* block, unsigned pc)
374 assert(func->symt.tag == SymTagFunction);
376 if (pc) block->size = func->address + pc - block->address;
377 return (block->container->tag == SymTagBlock) ?
378 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
381 struct symt_function_point* symt_add_function_point(struct module* module,
382 struct symt_function* func,
383 enum SymTagEnum point,
384 unsigned offset, const char* name)
386 struct symt_function_point* sym;
389 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
391 sym->symt.tag = point;
393 sym->offset = offset;
394 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
395 p = vector_add(&func->vchildren, &module->pool);
401 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
404 struct line_info* dli;
407 /* We aren't adding any more locals or line numbers to this function.
408 * Free any spare memory that we might have allocated.
410 assert(func->symt.tag == SymTagFunction);
412 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
413 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
415 len = vector_length(&func->vlines);
418 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
419 dli = vector_at(&func->vlines, len); dli->is_last = 1;
424 struct symt_thunk* symt_new_thunk(struct module* module,
425 struct symt_compiland* compiland,
426 const char* name, THUNK_ORDINAL ord,
427 unsigned long addr, unsigned long size)
429 struct symt_thunk* sym;
431 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
432 module->module.ModuleName, name, addr, addr + size - 1);
434 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
436 sym->symt.tag = SymTagThunk;
437 sym->hash_elt.name = pool_strdup(&module->pool, name);
438 hash_table_add(&module->ht_symbols, &sym->hash_elt);
439 module->sortlist_valid = FALSE;
440 sym->container = &compiland->symt;
447 p = vector_add(&compiland->vchildren, &module->pool);
454 /* expect sym_info->MaxNameLen to be set before being called */
455 static void symt_fill_sym_info(const struct module* module,
456 const struct symt* sym, SYMBOL_INFO* sym_info)
461 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
462 sym_info->TypeIndex = 0;
463 sym_info->info = (DWORD)sym;
464 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
465 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
466 (!sym_info->TypeIndex ||
467 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
469 sym_info->Size = (DWORD)size;
470 sym_info->ModBase = module->module.BaseOfImage;
478 const struct symt_data* data = (const struct symt_data*)sym;
482 sym_info->Flags |= SYMFLAG_PARAMETER;
485 if (data->u.s.reg_id)
487 sym_info->Flags |= SYMFLAG_REGISTER;
488 sym_info->Register = data->u.s.reg_id;
489 sym_info->Address = 0;
493 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
494 /* FIXME: needed ? moreover, it's i386 dependent !!! */
495 sym_info->Register = CV_REG_EBP;
496 sym_info->Address = data->u.s.offset / 8;
500 case DataIsFileStatic:
501 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
502 sym_info->Register = 0;
505 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
506 switch (data->u.value.n1.n2.vt)
508 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
509 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
510 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
511 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
512 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
513 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
515 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
519 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
523 case SymTagPublicSymbol:
524 sym_info->Flags |= SYMFLAG_EXPORT;
525 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
528 sym_info->Flags |= SYMFLAG_FUNCTION;
529 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
532 sym_info->Flags |= SYMFLAG_THUNK;
533 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
536 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
537 sym_info->Register = 0;
540 sym_info->Scope = 0; /* FIXME */
541 sym_info->Tag = sym->tag;
542 name = symt_get_name(sym);
543 if (sym_info->MaxNameLen)
545 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
546 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
547 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
549 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
550 memcpy(sym_info->Name, name, sym_info->NameLen);
551 sym_info->Name[sym_info->NameLen] = '\0';
554 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
555 sym, sym_info->Name, sym_info->Size,
556 wine_dbgstr_longlong(sym_info->Address));
559 static BOOL symt_enum_module(struct module* module, regex_t* regex,
560 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
562 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
563 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
565 struct symt_ht* sym = NULL;
566 struct hash_table_iter hti;
568 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
569 while ((ptr = hash_table_iter_up(&hti)))
571 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
572 if (sym->hash_elt.name &&
573 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
575 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
576 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
577 symt_fill_sym_info(module, &sym->symt, sym_info);
578 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
584 /***********************************************************************
587 * Rebuild sorted list of symbols for a module.
589 static BOOL resort_symbols(struct module* module)
594 struct hash_table_iter hti;
596 if (!module_compute_num_syms(module)) return FALSE;
598 if (module->addr_sorttab)
599 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
600 module->addr_sorttab,
601 module->module.NumSyms * sizeof(struct symt_ht*));
603 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
604 module->module.NumSyms * sizeof(struct symt_ht*));
605 if (!module->addr_sorttab) return FALSE;
608 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
609 while ((ptr = hash_table_iter_up(&hti)))
611 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
613 module->addr_sorttab[nsym++] = sym;
616 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
617 return module->sortlist_valid = TRUE;
620 /* assume addr is in module */
621 int symt_find_nearest(struct module* module, DWORD addr)
624 ULONG64 ref_addr, ref_size;
626 if (!module->sortlist_valid || !module->addr_sorttab)
628 if (!resort_symbols(module)) return -1;
632 * Binary search to find closest symbol.
635 high = module->module.NumSyms;
637 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
638 if (addr < ref_addr) return -1;
641 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
642 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
643 ref_size = 0x1000; /* arbitrary value */
644 if (addr >= ref_addr + ref_size) return -1;
647 while (high > low + 1)
649 mid = (high + low) / 2;
650 if (cmp_sorttab_addr(module, mid, addr) < 0)
655 if (low != high && high != module->module.NumSyms &&
656 cmp_sorttab_addr(module, high, addr) <= 0)
659 /* If found symbol is a public symbol, check if there are any other entries that
660 * might also have the same address, but would get better information
662 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
664 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
666 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
667 !cmp_sorttab_addr(module, low - 1, ref_addr))
669 else if (low < module->module.NumSyms - 1 &&
670 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
671 !cmp_sorttab_addr(module, low + 1, ref_addr))
674 /* finally check that we fit into the found symbol */
675 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
676 if (addr < ref_addr) return -1;
677 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
678 ref_size = 0x1000; /* arbitrary value */
679 if (addr >= ref_addr + ref_size) return -1;
684 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
685 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
686 PVOID user, SYMBOL_INFO* sym_info,
689 struct symt** plsym = NULL;
690 struct symt* lsym = NULL;
691 DWORD pc = pcs->ctx_frame.InstructionOffset;
693 while ((plsym = vector_iter_up(v, plsym)))
700 struct symt_block* block = (struct symt_block*)lsym;
701 if (pc < block->address || block->address + block->size <= pc)
703 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
704 sym_info, &block->vchildren))
709 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
711 symt_fill_sym_info(module, lsym, sym_info);
712 if (!cb(sym_info, sym_info->Size, user))
717 case SymTagFuncDebugStart:
718 case SymTagFuncDebugEnd:
721 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
728 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
729 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
732 struct module* module;
734 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
735 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
736 DWORD pc = pcs->ctx_frame.InstructionOffset;
739 sym_info->SizeOfStruct = sizeof(*sym_info);
740 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
742 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
743 if (!(module = module_get_debug(pcs, module))) return FALSE;
744 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
746 sym = module->addr_sorttab[idx];
747 if (sym->symt.tag == SymTagFunction)
752 compile_regex(mask ? mask : "*", -1, &preg,
753 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
754 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
755 UserContext, sym_info,
756 &((struct symt_function*)sym)->vchildren);
761 symt_fill_sym_info(module, &sym->symt, sym_info);
762 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
765 /******************************************************************
766 * SymEnumSymbols (DBGHELP.@)
768 * cases BaseOfDll = 0
769 * !foo fails always (despite what MSDN states)
770 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
771 * no ! in Mask, lookup in local Context
772 * cases BaseOfDll != 0
773 * !foo fails always (despite what MSDN states)
774 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
776 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
777 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
780 struct process* pcs = process_find_by_handle(hProcess);
781 struct module* module;
782 struct module* dbg_module;
784 regex_t mod_regex, sym_regex;
786 TRACE("(%p %s %s %p %p)\n",
787 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
788 EnumSymbolsCallback, UserContext);
790 if (!pcs) return FALSE;
794 /* do local variables ? */
795 if (!Mask || !(bang = strchr(Mask, '!')))
796 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
798 if (bang == Mask) return FALSE;
800 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
801 compile_regex(bang + 1, -1, &sym_regex,
802 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
804 for (module = pcs->lmodules; module; module = module->next)
806 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
808 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
809 symt_enum_module(dbg_module, &sym_regex,
810 EnumSymbolsCallback, UserContext))
814 /* not found in PE modules, retry on the ELF ones
816 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
818 for (module = pcs->lmodules; module; module = module->next)
820 if (module->type == DMT_ELF &&
821 !module_get_containee(pcs, module) &&
822 (dbg_module = module_get_debug(pcs, module)))
824 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
825 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
834 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
835 if (!(module = module_get_debug(pcs, module)))
838 /* we always ignore module name from Mask when BaseOfDll is defined */
839 if (Mask && (bang = strchr(Mask, '!')))
841 if (bang == Mask) return FALSE;
845 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
846 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
847 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
856 PSYM_ENUMSYMBOLS_CALLBACK cb;
859 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
861 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
862 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
865 /***********************************************************************
866 * SymEnumerateSymbols (DBGHELP.@)
868 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
869 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
872 struct sym_enumerate se;
874 se.ctx = UserContext;
875 se.cb = EnumSymbolsCallback;
877 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
880 /******************************************************************
881 * SymFromAddr (DBGHELP.@)
884 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
885 DWORD64* Displacement, PSYMBOL_INFO Symbol)
887 struct process* pcs = process_find_by_handle(hProcess);
888 struct module* module;
892 if (!pcs) return FALSE;
893 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
894 if (!(module = module_get_debug(pcs, module))) return FALSE;
895 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
897 sym = module->addr_sorttab[idx];
899 symt_fill_sym_info(module, &sym->symt, Symbol);
900 *Displacement = Address - Symbol->Address;
904 /******************************************************************
905 * SymGetSymFromAddr (DBGHELP.@)
908 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
909 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
911 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
912 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
914 DWORD64 Displacement64;
916 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
917 si->SizeOfStruct = sizeof(*si);
918 si->MaxNameLen = MAX_SYM_NAME;
919 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
923 *Displacement = Displacement64;
924 Symbol->Address = si->Address;
925 Symbol->Size = si->Size;
926 Symbol->Flags = si->Flags;
927 len = min(Symbol->MaxNameLength, si->MaxNameLen);
928 lstrcpynA(Symbol->Name, si->Name, len);
932 /******************************************************************
933 * SymFromName (DBGHELP.@)
936 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
938 struct process* pcs = process_find_by_handle(hProcess);
939 struct module* module;
940 struct hash_table_iter hti;
942 struct symt_ht* sym = NULL;
945 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
946 if (!pcs) return FALSE;
947 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
948 name = strchr(Name, '!');
952 assert(name - Name < sizeof(tmp));
953 memcpy(tmp, Name, name - Name);
954 tmp[name - Name] = '\0';
955 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
956 if (!module) return FALSE;
957 Name = (char*)(name + 1);
959 else module = pcs->lmodules;
961 /* FIXME: Name could be made out of a regular expression */
962 for (; module; module = (name) ? NULL : module->next)
964 if (module->module.SymType == SymNone) continue;
965 if (module->module.SymType == SymDeferred)
967 struct module* xmodule = module_get_debug(pcs, module);
968 if (!xmodule || xmodule != module) continue;
970 hash_table_iter_init(&module->ht_symbols, &hti, Name);
971 while ((ptr = hash_table_iter_up(&hti)))
973 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
975 if (!strcmp(sym->hash_elt.name, Name))
977 symt_fill_sym_info(module, &sym->symt, Symbol);
985 /***********************************************************************
986 * SymGetSymFromName (DBGHELP.@)
988 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
990 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
991 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
994 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
995 si->SizeOfStruct = sizeof(*si);
996 si->MaxNameLen = MAX_SYM_NAME;
997 if (!SymFromName(hProcess, Name, si)) return FALSE;
999 Symbol->Address = si->Address;
1000 Symbol->Size = si->Size;
1001 Symbol->Flags = si->Flags;
1002 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1003 lstrcpynA(Symbol->Name, si->Name, len);
1007 /******************************************************************
1008 * sym_fill_func_line_info
1010 * fills information about a file
1012 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
1013 DWORD addr, IMAGEHLP_LINE* line)
1015 struct line_info* dli = NULL;
1018 assert(func->symt.tag == SymTagFunction);
1020 while ((dli = vector_iter_down(&func->vlines, dli)))
1022 if (!dli->is_source_file)
1024 if (found || dli->u.pc_offset > addr) continue;
1025 line->LineNumber = dli->line_number;
1026 line->Address = dli->u.pc_offset;
1033 line->FileName = (char*)source_get(module, dli->u.source_file);
1040 /***********************************************************************
1041 * SymGetSymNext (DBGHELP.@)
1043 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1046 * get module from Symbol.Address
1047 * get index in module.addr_sorttab of Symbol.Address
1049 * if out of module bounds, move to next module in process address space
1051 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1052 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1056 /***********************************************************************
1057 * SymGetSymPrev (DBGHELP.@)
1060 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1062 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1063 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1067 /******************************************************************
1068 * SymGetLineFromAddr (DBGHELP.@)
1071 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1072 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1074 struct process* pcs = process_find_by_handle(hProcess);
1075 struct module* module;
1078 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1080 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1082 if (!pcs) return FALSE;
1083 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1084 if (!(module = module_get_debug(pcs, module))) return FALSE;
1085 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1087 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1088 if (!symt_fill_func_line_info(module,
1089 (struct symt_function*)module->addr_sorttab[idx],
1090 dwAddr, Line)) return FALSE;
1091 *pdwDisplacement = dwAddr - Line->Address;
1095 /******************************************************************
1096 * copy_line_64_from_32 (internal)
1099 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1102 l64->Key = l32->Key;
1103 l64->LineNumber = l32->LineNumber;
1104 l64->FileName = l32->FileName;
1105 l64->Address = l32->Address;
1108 /******************************************************************
1109 * copy_line_32_from_64 (internal)
1112 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1115 l32->Key = l64->Key;
1116 l32->LineNumber = l64->LineNumber;
1117 l32->FileName = l64->FileName;
1118 l32->Address = l64->Address;
1121 /******************************************************************
1122 * SymGetLineFromAddr64 (DBGHELP.@)
1125 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1126 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1128 IMAGEHLP_LINE line32;
1130 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1131 if (!validate_addr64(dwAddr)) return FALSE;
1132 line32.SizeOfStruct = sizeof(line32);
1133 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1135 copy_line_64_from_32(Line, &line32);
1139 /******************************************************************
1140 * SymGetLinePrev (DBGHELP.@)
1143 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1145 struct process* pcs = process_find_by_handle(hProcess);
1146 struct module* module;
1147 struct line_info* li;
1148 BOOL in_search = FALSE;
1150 TRACE("(%p %p)\n", hProcess, Line);
1152 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1154 if (!pcs) return FALSE;
1155 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1156 if (!(module = module_get_debug(pcs, module))) return FALSE;
1158 if (Line->Key == 0) return FALSE;
1159 li = (struct line_info*)Line->Key;
1160 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1161 * element we have to go back until we find the prev one to get the real
1162 * source file name for the DLIT_OFFSET element just before
1163 * the first DLIT_SOURCEFILE
1165 while (!li->is_first)
1168 if (!li->is_source_file)
1170 Line->LineNumber = li->line_number;
1171 Line->Address = li->u.pc_offset;
1173 if (!in_search) return TRUE;
1179 Line->FileName = (char*)source_get(module, li->u.source_file);
1185 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1189 /******************************************************************
1190 * SymGetLinePrev64 (DBGHELP.@)
1193 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1195 IMAGEHLP_LINE line32;
1197 line32.SizeOfStruct = sizeof(line32);
1198 copy_line_32_from_64(&line32, Line);
1199 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1200 copy_line_64_from_32(Line, &line32);
1204 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1206 struct line_info* li;
1208 if (line->Key == 0) return FALSE;
1209 li = (struct line_info*)line->Key;
1210 while (!li->is_last)
1213 if (!li->is_source_file)
1215 line->LineNumber = li->line_number;
1216 line->Address = li->u.pc_offset;
1220 line->FileName = (char*)source_get(module, li->u.source_file);
1225 /******************************************************************
1226 * SymGetLineNext (DBGHELP.@)
1229 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1231 struct process* pcs = process_find_by_handle(hProcess);
1232 struct module* module;
1234 TRACE("(%p %p)\n", hProcess, Line);
1236 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1237 if (!pcs) return FALSE;
1238 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1239 if (!(module = module_get_debug(pcs, module))) return FALSE;
1241 if (symt_get_func_line_next(module, Line)) return TRUE;
1242 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1246 /******************************************************************
1247 * SymGetLineNext64 (DBGHELP.@)
1250 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1252 IMAGEHLP_LINE line32;
1254 line32.SizeOfStruct = sizeof(line32);
1255 copy_line_32_from_64(&line32, Line);
1256 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1257 copy_line_64_from_32(Line, &line32);
1261 /***********************************************************************
1262 * SymFunctionTableAccess (DBGHELP.@)
1264 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1266 WARN("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1270 /***********************************************************************
1271 * SymFunctionTableAccess64 (DBGHELP.@)
1273 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1275 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1279 /***********************************************************************
1280 * SymUnDName (DBGHELP.@)
1282 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1284 TRACE("(%p %s %lu)\n", sym, UnDecName, UnDecNameLength);
1285 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1286 UNDNAME_COMPLETE) != 0;
1289 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1290 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1292 /***********************************************************************
1293 * UnDecorateSymbolName (DBGHELP.@)
1295 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1296 DWORD UndecoratedLength, DWORD Flags)
1298 /* undocumented from msvcrt */
1299 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1300 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1302 TRACE("(%s, %p, %ld, 0x%08lx)\n",
1303 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1307 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1308 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1309 if (!p_undname) return 0;
1312 if (!UnDecoratedName) return 0;
1313 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1314 und_alloc, und_free, Flags))
1316 return strlen(UnDecoratedName);
1319 /******************************************************************
1320 * SymMatchString (DBGHELP.@)
1323 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1328 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1330 compile_regex(re, -1, &preg, _case);
1331 ret = regexec(&preg, string, 0, NULL, 0) == 0;
1336 /******************************************************************
1337 * SymSearch (DBGHELP.@)
1339 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1340 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1341 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1342 PVOID UserContext, DWORD Options)
1344 TRACE("(%p %s %lu %lu %s %s %p %p %lx)\n",
1345 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1346 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1347 UserContext, Options);
1351 FIXME("Unsupported searching for a given Index (%lu)\n", Index);
1352 SetLastError(ERROR_INVALID_PARAMETER);
1357 FIXME("Unsupported searching for a given SymTag (%lu)\n", SymTag);
1358 SetLastError(ERROR_INVALID_PARAMETER);
1363 FIXME("Unsupported searching for a given Address (%s)\n", wine_dbgstr_longlong(Address));
1364 SetLastError(ERROR_INVALID_PARAMETER);
1367 if (Options != SYMSEARCH_GLOBALSONLY)
1369 FIXME("Unsupported searching with options (%lx)\n", Options);
1370 SetLastError(ERROR_INVALID_PARAMETER);
1373 return SymEnumSymbols(hProcess, BaseOfDll, Mask, EnumSymbolsCallback, UserContext);