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 struct symt_data* symt_add_func_local(struct module* module,
289 struct symt_function* func,
290 int regno, int offset,
291 struct symt_block* block,
292 struct symt* type, const char* name)
294 struct symt_data* locsym;
298 assert(func->symt.tag == SymTagFunction);
300 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
301 module->module.ModuleName, func->hash_elt.name,
303 locsym = pool_alloc(&module->pool, sizeof(*locsym));
304 locsym->symt.tag = SymTagData;
305 locsym->hash_elt.name = pool_strdup(&module->pool, name);
306 locsym->hash_elt.next = NULL;
307 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
308 locsym->container = &block->symt;
312 locsym->u.s.reg_id = regno;
313 locsym->u.s.offset = 0;
314 locsym->u.s.length = 0;
318 locsym->u.s.reg_id = 0;
319 locsym->u.s.offset = offset * 8;
320 locsym->u.s.length = 0;
323 p = vector_add(&block->vchildren, &module->pool);
325 p = vector_add(&func->vchildren, &module->pool);
330 struct symt_block* symt_open_func_block(struct module* module,
331 struct symt_function* func,
332 struct symt_block* parent_block,
333 unsigned pc, unsigned len)
335 struct symt_block* block;
339 assert(func->symt.tag == SymTagFunction);
341 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
342 block = pool_alloc(&module->pool, sizeof(*block));
343 block->symt.tag = SymTagBlock;
344 block->address = func->address + pc;
346 block->container = parent_block ? &parent_block->symt : &func->symt;
347 vector_init(&block->vchildren, sizeof(struct symt*), 4);
349 p = vector_add(&parent_block->vchildren, &module->pool);
351 p = vector_add(&func->vchildren, &module->pool);
357 struct symt_block* symt_close_func_block(struct module* module,
358 struct symt_function* func,
359 struct symt_block* block, unsigned pc)
361 assert(func->symt.tag == SymTagFunction);
363 if (pc) block->size = func->address + pc - block->address;
364 return (block->container->tag == SymTagBlock) ?
365 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
368 struct symt_function_point* symt_add_function_point(struct module* module,
369 struct symt_function* func,
370 enum SymTagEnum point,
371 unsigned offset, const char* name)
373 struct symt_function_point* sym;
376 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
378 sym->symt.tag = point;
380 sym->offset = offset;
381 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
382 p = vector_add(&func->vchildren, &module->pool);
388 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
391 struct line_info* dli;
394 /* We aren't adding any more locals or line numbers to this function.
395 * Free any spare memory that we might have allocated.
397 assert(func->symt.tag == SymTagFunction);
399 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
400 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
402 len = vector_length(&func->vlines);
405 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
406 dli = vector_at(&func->vlines, len); dli->is_last = 1;
411 struct symt_thunk* symt_new_thunk(struct module* module,
412 struct symt_compiland* compiland,
413 const char* name, THUNK_ORDINAL ord,
414 unsigned long addr, unsigned long size)
416 struct symt_thunk* sym;
418 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
419 module->module.ModuleName, name, addr, addr + size - 1);
421 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
423 sym->symt.tag = SymTagThunk;
424 sym->hash_elt.name = pool_strdup(&module->pool, name);
425 hash_table_add(&module->ht_symbols, &sym->hash_elt);
426 module->sortlist_valid = FALSE;
427 sym->container = &compiland->symt;
434 p = vector_add(&compiland->vchildren, &module->pool);
441 /* expect sym_info->MaxNameLen to be set before being called */
442 static void symt_fill_sym_info(const struct module* module,
443 const struct symt* sym, SYMBOL_INFO* sym_info)
448 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
449 sym_info->TypeIndex = 0;
450 sym_info->info = (DWORD)sym;
451 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
452 sym_info->TypeIndex &&
453 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size))
455 sym_info->Size = (DWORD)size;
456 sym_info->ModBase = module->module.BaseOfImage;
462 const struct symt_data* data = (const struct symt_data*)sym;
467 if (data->u.s.reg_id)
469 sym_info->Flags |= SYMFLAG_REGISTER;
470 sym_info->Register = data->u.s.reg_id;
471 sym_info->Address = 0;
475 if (data->u.s.offset < 0)
476 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
478 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
479 /* FIXME: needed ? moreover, it's i386 dependent !!! */
480 sym_info->Register = CV_REG_EBP;
481 sym_info->Address = data->u.s.offset / 8;
485 case DataIsFileStatic:
486 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
487 sym_info->Register = 0;
490 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
491 switch (data->u.value.n1.n2.vt)
493 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
494 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
495 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
496 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
497 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
498 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
500 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
504 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
508 case SymTagPublicSymbol:
509 sym_info->Flags |= SYMFLAG_EXPORT;
510 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
513 sym_info->Flags |= SYMFLAG_FUNCTION;
514 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
517 sym_info->Flags |= SYMFLAG_THUNK;
518 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
521 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
522 sym_info->Register = 0;
525 sym_info->Scope = 0; /* FIXME */
526 sym_info->Tag = sym->tag;
527 name = symt_get_name(sym);
528 if (sym_info->MaxNameLen)
530 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
531 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
532 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
534 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
535 memcpy(sym_info->Name, name, sym_info->NameLen);
536 sym_info->Name[sym_info->NameLen] = '\0';
539 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
540 sym, sym_info->Name, sym_info->Size,
541 wine_dbgstr_longlong(sym_info->Address));
544 static BOOL symt_enum_module(struct module* module, regex_t* regex,
545 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
547 char buffer[sizeof(SYMBOL_INFO) + 256];
548 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
550 struct symt_ht* sym = NULL;
551 struct hash_table_iter hti;
553 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
554 while ((ptr = hash_table_iter_up(&hti)))
556 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
557 if (sym->hash_elt.name &&
558 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
560 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
561 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
562 symt_fill_sym_info(module, &sym->symt, sym_info);
563 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
569 /***********************************************************************
572 * Rebuild sorted list of symbols for a module.
574 static BOOL resort_symbols(struct module* module)
579 struct hash_table_iter hti;
581 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
582 while ((ptr = hash_table_iter_up(&hti)))
585 if (!(module->module.NumSyms = nsym)) return FALSE;
587 if (module->addr_sorttab)
588 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
589 module->addr_sorttab,
590 nsym * sizeof(struct symt_ht*));
592 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
593 nsym * sizeof(struct symt_ht*));
594 if (!module->addr_sorttab) return FALSE;
597 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
598 while ((ptr = hash_table_iter_up(&hti)))
600 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
602 module->addr_sorttab[nsym++] = sym;
605 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
606 return module->sortlist_valid = TRUE;
609 /* assume addr is in module */
610 int symt_find_nearest(struct module* module, DWORD addr)
613 ULONG64 ref_addr, ref_size;
615 if (!module->sortlist_valid || !module->addr_sorttab)
617 if (!resort_symbols(module)) return -1;
621 * Binary search to find closest symbol.
624 high = module->module.NumSyms;
626 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
627 if (addr < ref_addr) return -1;
630 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
631 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
632 ref_size = 0x1000; /* arbitrary value */
633 if (addr >= ref_addr + ref_size) return -1;
636 while (high > low + 1)
638 mid = (high + low) / 2;
639 if (cmp_sorttab_addr(module, mid, addr) < 0)
644 if (low != high && high != module->module.NumSyms &&
645 cmp_sorttab_addr(module, high, addr) <= 0)
648 /* If found symbol is a public symbol, check if there are any other entries that
649 * might also have the same address, but would get better information
651 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
653 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
655 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
656 !cmp_sorttab_addr(module, low - 1, ref_addr))
658 else if (low < module->module.NumSyms - 1 &&
659 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
660 !cmp_sorttab_addr(module, low + 1, ref_addr))
663 /* finally check that we fit into the found symbol */
664 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
665 if (addr < ref_addr) return -1;
666 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
667 ref_size = 0x1000; /* arbitrary value */
668 if (addr >= ref_addr + ref_size) return -1;
673 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
674 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
675 PVOID user, SYMBOL_INFO* sym_info,
678 struct symt** plsym = NULL;
679 struct symt* lsym = NULL;
680 DWORD pc = pcs->ctx_frame.InstructionOffset;
682 while ((plsym = vector_iter_up(v, plsym)))
689 struct symt_block* block = (struct symt_block*)lsym;
690 if (pc < block->address || block->address + block->size <= pc)
692 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
693 sym_info, &block->vchildren))
698 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
700 symt_fill_sym_info(module, lsym, sym_info);
701 if (!cb(sym_info, sym_info->Size, user))
706 case SymTagFuncDebugStart:
707 case SymTagFuncDebugEnd:
710 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
717 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
718 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
721 struct module* module;
723 char buffer[sizeof(SYMBOL_INFO) + 256];
724 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
725 DWORD pc = pcs->ctx_frame.InstructionOffset;
728 sym_info->SizeOfStruct = sizeof(*sym_info);
729 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
731 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
732 if (!(module = module_get_debug(pcs, module))) return FALSE;
733 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
735 sym = module->addr_sorttab[idx];
736 if (sym->symt.tag == SymTagFunction)
741 compile_regex(mask ? mask : "*", -1, &preg,
742 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
743 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
744 UserContext, sym_info,
745 &((struct symt_function*)sym)->vchildren);
750 symt_fill_sym_info(module, &sym->symt, sym_info);
751 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
754 /******************************************************************
755 * SymEnumSymbols (DBGHELP.@)
757 * cases BaseOfDll = 0
758 * !foo fails always (despite what MSDN states)
759 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
760 * no ! in Mask, lookup in local Context
761 * cases BaseOfDll != 0
762 * !foo fails always (despite what MSDN states)
763 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
765 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
766 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
769 struct process* pcs = process_find_by_handle(hProcess);
770 struct module* module;
771 struct module* dbg_module;
773 regex_t mod_regex, sym_regex;
775 TRACE("(%p %s %s %p %p)\n",
776 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
777 EnumSymbolsCallback, UserContext);
779 if (!pcs) return FALSE;
783 /* do local variables ? */
784 if (!Mask || !(bang = strchr(Mask, '!')))
785 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
787 if (bang == Mask) return FALSE;
789 compile_regex(Mask, bang - Mask, &mod_regex,
790 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
791 compile_regex(bang + 1, -1, &sym_regex,
792 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
794 for (module = pcs->lmodules; module; module = module->next)
796 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
798 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
799 symt_enum_module(dbg_module, &sym_regex,
800 EnumSymbolsCallback, UserContext))
804 /* not found in PE modules, retry on the ELF ones
806 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
808 for (module = pcs->lmodules; module; module = module->next)
810 if (module->type == DMT_ELF &&
811 !module_get_containee(pcs, module) &&
812 (dbg_module = module_get_debug(pcs, module)))
814 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
815 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
824 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
825 if (!(module = module_get_debug(pcs, module)))
828 /* we always ignore module name from Mask when BaseOfDll is defined */
829 if (Mask && (bang = strchr(Mask, '!')))
831 if (bang == Mask) return FALSE;
835 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
836 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
837 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
846 PSYM_ENUMSYMBOLS_CALLBACK cb;
849 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
851 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
852 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
855 /***********************************************************************
856 * SymEnumerateSymbols (DBGHELP.@)
858 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
859 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
862 struct sym_enumerate se;
864 se.ctx = UserContext;
865 se.cb = EnumSymbolsCallback;
867 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
870 /******************************************************************
871 * SymFromAddr (DBGHELP.@)
874 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
875 DWORD64* Displacement, PSYMBOL_INFO Symbol)
877 struct process* pcs = process_find_by_handle(hProcess);
878 struct module* module;
882 if (!pcs) return FALSE;
883 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
884 if (!(module = module_get_debug(pcs, module))) return FALSE;
885 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
887 sym = module->addr_sorttab[idx];
889 symt_fill_sym_info(module, &sym->symt, Symbol);
890 *Displacement = Address - Symbol->Address;
894 /******************************************************************
895 * SymGetSymFromAddr (DBGHELP.@)
898 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
899 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
901 char buffer[sizeof(SYMBOL_INFO) + 256];
902 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
904 DWORD64 Displacement64;
906 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
907 si->SizeOfStruct = sizeof(*si);
908 si->MaxNameLen = 256;
909 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
913 *Displacement = Displacement64;
914 Symbol->Address = si->Address;
915 Symbol->Size = si->Size;
916 Symbol->Flags = si->Flags;
917 len = min(Symbol->MaxNameLength, si->MaxNameLen);
918 lstrcpynA(Symbol->Name, si->Name, len);
922 /******************************************************************
923 * SymFromName (DBGHELP.@)
926 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
928 struct process* pcs = process_find_by_handle(hProcess);
929 struct module* module;
930 struct hash_table_iter hti;
932 struct symt_ht* sym = NULL;
935 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
936 if (!pcs) return FALSE;
937 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
938 name = strchr(Name, '!');
942 assert(name - Name < sizeof(tmp));
943 memcpy(tmp, Name, name - Name);
944 tmp[name - Name] = '\0';
945 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
946 if (!module) return FALSE;
947 Name = (char*)(name + 1);
949 else module = pcs->lmodules;
951 /* FIXME: Name could be made out of a regular expression */
952 for (; module; module = (name) ? NULL : module->next)
954 if (module->module.SymType == SymNone) continue;
955 if (module->module.SymType == SymDeferred)
957 struct module* xmodule = module_get_debug(pcs, module);
958 if (!xmodule || xmodule != module) continue;
960 hash_table_iter_init(&module->ht_symbols, &hti, Name);
961 while ((ptr = hash_table_iter_up(&hti)))
963 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
965 if (!strcmp(sym->hash_elt.name, Name))
967 symt_fill_sym_info(module, &sym->symt, Symbol);
975 /***********************************************************************
976 * SymGetSymFromName (DBGHELP.@)
978 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
980 char buffer[sizeof(SYMBOL_INFO) + 256];
981 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
984 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
985 si->SizeOfStruct = sizeof(*si);
986 si->MaxNameLen = 256;
987 if (!SymFromName(hProcess, Name, si)) return FALSE;
989 Symbol->Address = si->Address;
990 Symbol->Size = si->Size;
991 Symbol->Flags = si->Flags;
992 len = min(Symbol->MaxNameLength, si->MaxNameLen);
993 lstrcpynA(Symbol->Name, si->Name, len);
997 /******************************************************************
998 * sym_fill_func_line_info
1000 * fills information about a file
1002 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
1003 DWORD addr, IMAGEHLP_LINE* line)
1005 struct line_info* dli = NULL;
1008 assert(func->symt.tag == SymTagFunction);
1010 while ((dli = vector_iter_down(&func->vlines, dli)))
1012 if (!dli->is_source_file)
1014 if (found || dli->u.pc_offset > addr) continue;
1015 line->LineNumber = dli->line_number;
1016 line->Address = dli->u.pc_offset;
1023 line->FileName = (char*)source_get(module, dli->u.source_file);
1030 /***********************************************************************
1031 * SymGetSymNext (DBGHELP.@)
1033 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1036 * get module from Symbol.Address
1037 * get index in module.addr_sorttab of Symbol.Address
1039 * if out of module bounds, move to next module in process address space
1041 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1042 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1046 /***********************************************************************
1047 * SymGetSymPrev (DBGHELP.@)
1050 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1052 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1053 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1057 /******************************************************************
1058 * SymGetLineFromAddr (DBGHELP.@)
1061 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1062 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1064 struct process* pcs = process_find_by_handle(hProcess);
1065 struct module* module;
1068 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1070 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1072 if (!pcs) return FALSE;
1073 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1074 if (!(module = module_get_debug(pcs, module))) return FALSE;
1075 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1077 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1078 if (!symt_fill_func_line_info(module,
1079 (struct symt_function*)module->addr_sorttab[idx],
1080 dwAddr, Line)) return FALSE;
1081 *pdwDisplacement = dwAddr - Line->Address;
1085 /******************************************************************
1086 * copy_line_64_from_32 (internal)
1089 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1092 l64->Key = l32->Key;
1093 l64->LineNumber = l32->LineNumber;
1094 l64->FileName = l32->FileName;
1095 l64->Address = l32->Address;
1098 /******************************************************************
1099 * copy_line_32_from_64 (internal)
1102 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1105 l32->Key = l64->Key;
1106 l32->LineNumber = l64->LineNumber;
1107 l32->FileName = l64->FileName;
1108 l32->Address = l64->Address;
1111 /******************************************************************
1112 * SymGetLineFromAddr64 (DBGHELP.@)
1115 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1116 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1118 IMAGEHLP_LINE line32;
1120 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1121 if (!validate_addr64(dwAddr)) return FALSE;
1122 line32.SizeOfStruct = sizeof(line32);
1123 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1125 copy_line_64_from_32(Line, &line32);
1129 /******************************************************************
1130 * SymGetLinePrev (DBGHELP.@)
1133 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1135 struct process* pcs = process_find_by_handle(hProcess);
1136 struct module* module;
1137 struct line_info* li;
1138 BOOL in_search = FALSE;
1140 TRACE("(%p %p)\n", hProcess, Line);
1142 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1144 if (!pcs) return FALSE;
1145 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1146 if (!(module = module_get_debug(pcs, module))) return FALSE;
1148 if (Line->Key == 0) return FALSE;
1149 li = (struct line_info*)Line->Key;
1150 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1151 * element we have to go back until we find the prev one to get the real
1152 * source file name for the DLIT_OFFSET element just before
1153 * the first DLIT_SOURCEFILE
1155 while (!li->is_first)
1158 if (!li->is_source_file)
1160 Line->LineNumber = li->line_number;
1161 Line->Address = li->u.pc_offset;
1163 if (!in_search) return TRUE;
1169 Line->FileName = (char*)source_get(module, li->u.source_file);
1175 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1179 /******************************************************************
1180 * SymGetLinePrev64 (DBGHELP.@)
1183 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1185 IMAGEHLP_LINE line32;
1187 line32.SizeOfStruct = sizeof(line32);
1188 copy_line_32_from_64(&line32, Line);
1189 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1190 copy_line_64_from_32(Line, &line32);
1194 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1196 struct line_info* li;
1198 if (line->Key == 0) return FALSE;
1199 li = (struct line_info*)line->Key;
1200 while (!li->is_last)
1203 if (!li->is_source_file)
1205 line->LineNumber = li->line_number;
1206 line->Address = li->u.pc_offset;
1210 line->FileName = (char*)source_get(module, li->u.source_file);
1215 /******************************************************************
1216 * SymGetLineNext (DBGHELP.@)
1219 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1221 struct process* pcs = process_find_by_handle(hProcess);
1222 struct module* module;
1224 TRACE("(%p %p)\n", hProcess, Line);
1226 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1227 if (!pcs) return FALSE;
1228 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1229 if (!(module = module_get_debug(pcs, module))) return FALSE;
1231 if (symt_get_func_line_next(module, Line)) return TRUE;
1232 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1236 /******************************************************************
1237 * SymGetLineNext64 (DBGHELP.@)
1240 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1242 IMAGEHLP_LINE line32;
1244 line32.SizeOfStruct = sizeof(line32);
1245 copy_line_32_from_64(&line32, Line);
1246 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1247 copy_line_64_from_32(Line, &line32);
1251 /***********************************************************************
1252 * SymFunctionTableAccess (DBGHELP.@)
1254 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1256 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1257 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1261 /***********************************************************************
1262 * SymUnDName (DBGHELP.@)
1264 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1266 TRACE("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1267 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1268 UNDNAME_COMPLETE) != 0;
1271 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1272 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1274 /***********************************************************************
1275 * UnDecorateSymbolName (DBGHELP.@)
1277 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1278 DWORD UndecoratedLength, DWORD Flags)
1280 /* undocumented from msvcrt */
1281 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1282 static WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1284 TRACE("(%s, %p, %ld, 0x%08lx): stub\n",
1285 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1289 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1290 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1291 if (!p_undname) return 0;
1294 if (!UnDecoratedName) return 0;
1295 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1296 und_alloc, und_free, Flags))
1298 return strlen(UnDecoratedName);
1301 /******************************************************************
1302 * SymMatchString (DBGHELP.@)
1305 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1310 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1312 compile_regex(re, -1, &preg, _case);
1313 ret = regexec(&preg, string, 0, NULL, 0) == 0;
1318 /******************************************************************
1319 * SymSearch (DBGHELP.@)
1321 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1322 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1323 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1324 PVOID UserContext, DWORD Options)
1326 TRACE("(%p %s %lu %lu %s %s %p %p %lx)\n",
1327 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1328 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1329 UserContext, Options);
1333 FIXME("Unsupported searching for a given Index (%lu)\n", Index);
1334 SetLastError(ERROR_INVALID_PARAMETER);
1339 FIXME("Unsupported searching for a given SymTag (%lu)\n", SymTag);
1340 SetLastError(ERROR_INVALID_PARAMETER);
1345 FIXME("Unsupported searching for a given Address (%s)\n", wine_dbgstr_longlong(Address));
1346 SetLastError(ERROR_INVALID_PARAMETER);
1349 if (Options != SYMSEARCH_GLOBALSONLY)
1351 FIXME("Unsupported searching with options (%lx)\n", Options);
1352 SetLastError(ERROR_INVALID_PARAMETER);
1355 return SymEnumSymbols(hProcess, BaseOfDll, Mask, EnumSymbolsCallback, UserContext);