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 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
452 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
453 (!sym_info->TypeIndex ||
454 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
456 sym_info->Size = (DWORD)size;
457 sym_info->ModBase = module->module.BaseOfImage;
465 const struct symt_data* data = (const struct symt_data*)sym;
470 if (data->u.s.reg_id)
472 sym_info->Flags |= SYMFLAG_REGISTER;
473 sym_info->Register = data->u.s.reg_id;
474 sym_info->Address = 0;
478 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
479 /* FIXME: this is i386 dependent */
480 if (data->u.s.offset >= 0) sym_info->Flags |= SYMFLAG_PARAMETER;
481 /* FIXME: needed ? moreover, it's i386 dependent !!! */
482 sym_info->Register = CV_REG_EBP;
483 sym_info->Address = data->u.s.offset / 8;
487 case DataIsFileStatic:
488 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
489 sym_info->Register = 0;
492 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
493 switch (data->u.value.n1.n2.vt)
495 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
496 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
497 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
498 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
499 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
500 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
502 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
506 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
510 case SymTagPublicSymbol:
511 sym_info->Flags |= SYMFLAG_EXPORT;
512 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
515 sym_info->Flags |= SYMFLAG_FUNCTION;
516 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
519 sym_info->Flags |= SYMFLAG_THUNK;
520 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
523 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
524 sym_info->Register = 0;
527 sym_info->Scope = 0; /* FIXME */
528 sym_info->Tag = sym->tag;
529 name = symt_get_name(sym);
530 if (sym_info->MaxNameLen)
532 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
533 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
534 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
536 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
537 memcpy(sym_info->Name, name, sym_info->NameLen);
538 sym_info->Name[sym_info->NameLen] = '\0';
541 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
542 sym, sym_info->Name, sym_info->Size,
543 wine_dbgstr_longlong(sym_info->Address));
546 static BOOL symt_enum_module(struct module* module, regex_t* regex,
547 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
549 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
550 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
552 struct symt_ht* sym = NULL;
553 struct hash_table_iter hti;
555 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
556 while ((ptr = hash_table_iter_up(&hti)))
558 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
559 if (sym->hash_elt.name &&
560 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
562 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
563 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
564 symt_fill_sym_info(module, &sym->symt, sym_info);
565 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
571 /***********************************************************************
574 * Rebuild sorted list of symbols for a module.
576 static BOOL resort_symbols(struct module* module)
581 struct hash_table_iter hti;
583 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
584 while ((ptr = hash_table_iter_up(&hti)))
587 if (!(module->module.NumSyms = nsym)) return FALSE;
589 if (module->addr_sorttab)
590 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
591 module->addr_sorttab,
592 nsym * sizeof(struct symt_ht*));
594 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
595 nsym * sizeof(struct symt_ht*));
596 if (!module->addr_sorttab) return FALSE;
599 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
600 while ((ptr = hash_table_iter_up(&hti)))
602 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
604 module->addr_sorttab[nsym++] = sym;
607 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
608 return module->sortlist_valid = TRUE;
611 /* assume addr is in module */
612 int symt_find_nearest(struct module* module, DWORD addr)
615 ULONG64 ref_addr, ref_size;
617 if (!module->sortlist_valid || !module->addr_sorttab)
619 if (!resort_symbols(module)) return -1;
623 * Binary search to find closest symbol.
626 high = module->module.NumSyms;
628 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
629 if (addr < ref_addr) return -1;
632 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
633 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
634 ref_size = 0x1000; /* arbitrary value */
635 if (addr >= ref_addr + ref_size) return -1;
638 while (high > low + 1)
640 mid = (high + low) / 2;
641 if (cmp_sorttab_addr(module, mid, addr) < 0)
646 if (low != high && high != module->module.NumSyms &&
647 cmp_sorttab_addr(module, high, addr) <= 0)
650 /* If found symbol is a public symbol, check if there are any other entries that
651 * might also have the same address, but would get better information
653 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
655 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
657 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
658 !cmp_sorttab_addr(module, low - 1, ref_addr))
660 else if (low < module->module.NumSyms - 1 &&
661 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
662 !cmp_sorttab_addr(module, low + 1, ref_addr))
665 /* finally check that we fit into the found symbol */
666 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
667 if (addr < ref_addr) return -1;
668 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
669 ref_size = 0x1000; /* arbitrary value */
670 if (addr >= ref_addr + ref_size) return -1;
675 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
676 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
677 PVOID user, SYMBOL_INFO* sym_info,
680 struct symt** plsym = NULL;
681 struct symt* lsym = NULL;
682 DWORD pc = pcs->ctx_frame.InstructionOffset;
684 while ((plsym = vector_iter_up(v, plsym)))
691 struct symt_block* block = (struct symt_block*)lsym;
692 if (pc < block->address || block->address + block->size <= pc)
694 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
695 sym_info, &block->vchildren))
700 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
702 symt_fill_sym_info(module, lsym, sym_info);
703 if (!cb(sym_info, sym_info->Size, user))
708 case SymTagFuncDebugStart:
709 case SymTagFuncDebugEnd:
712 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
719 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
720 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
723 struct module* module;
725 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
726 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
727 DWORD pc = pcs->ctx_frame.InstructionOffset;
730 sym_info->SizeOfStruct = sizeof(*sym_info);
731 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
733 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
734 if (!(module = module_get_debug(pcs, module))) return FALSE;
735 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
737 sym = module->addr_sorttab[idx];
738 if (sym->symt.tag == SymTagFunction)
743 compile_regex(mask ? mask : "*", -1, &preg,
744 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
745 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
746 UserContext, sym_info,
747 &((struct symt_function*)sym)->vchildren);
752 symt_fill_sym_info(module, &sym->symt, sym_info);
753 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
756 /******************************************************************
757 * SymEnumSymbols (DBGHELP.@)
759 * cases BaseOfDll = 0
760 * !foo fails always (despite what MSDN states)
761 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
762 * no ! in Mask, lookup in local Context
763 * cases BaseOfDll != 0
764 * !foo fails always (despite what MSDN states)
765 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
767 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
768 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
771 struct process* pcs = process_find_by_handle(hProcess);
772 struct module* module;
773 struct module* dbg_module;
775 regex_t mod_regex, sym_regex;
777 TRACE("(%p %s %s %p %p)\n",
778 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
779 EnumSymbolsCallback, UserContext);
781 if (!pcs) return FALSE;
785 /* do local variables ? */
786 if (!Mask || !(bang = strchr(Mask, '!')))
787 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
789 if (bang == Mask) return FALSE;
791 compile_regex(Mask, bang - Mask, &mod_regex,
792 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
793 compile_regex(bang + 1, -1, &sym_regex,
794 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
796 for (module = pcs->lmodules; module; module = module->next)
798 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
800 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
801 symt_enum_module(dbg_module, &sym_regex,
802 EnumSymbolsCallback, UserContext))
806 /* not found in PE modules, retry on the ELF ones
808 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
810 for (module = pcs->lmodules; module; module = module->next)
812 if (module->type == DMT_ELF &&
813 !module_get_containee(pcs, module) &&
814 (dbg_module = module_get_debug(pcs, module)))
816 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
817 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
826 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
827 if (!(module = module_get_debug(pcs, module)))
830 /* we always ignore module name from Mask when BaseOfDll is defined */
831 if (Mask && (bang = strchr(Mask, '!')))
833 if (bang == Mask) return FALSE;
837 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
838 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
839 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
848 PSYM_ENUMSYMBOLS_CALLBACK cb;
851 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
853 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
854 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
857 /***********************************************************************
858 * SymEnumerateSymbols (DBGHELP.@)
860 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
861 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
864 struct sym_enumerate se;
866 se.ctx = UserContext;
867 se.cb = EnumSymbolsCallback;
869 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
872 /******************************************************************
873 * SymFromAddr (DBGHELP.@)
876 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
877 DWORD64* Displacement, PSYMBOL_INFO Symbol)
879 struct process* pcs = process_find_by_handle(hProcess);
880 struct module* module;
884 if (!pcs) return FALSE;
885 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
886 if (!(module = module_get_debug(pcs, module))) return FALSE;
887 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
889 sym = module->addr_sorttab[idx];
891 symt_fill_sym_info(module, &sym->symt, Symbol);
892 *Displacement = Address - Symbol->Address;
896 /******************************************************************
897 * SymGetSymFromAddr (DBGHELP.@)
900 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
901 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
903 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
904 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
906 DWORD64 Displacement64;
908 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
909 si->SizeOfStruct = sizeof(*si);
910 si->MaxNameLen = MAX_SYM_NAME;
911 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
915 *Displacement = Displacement64;
916 Symbol->Address = si->Address;
917 Symbol->Size = si->Size;
918 Symbol->Flags = si->Flags;
919 len = min(Symbol->MaxNameLength, si->MaxNameLen);
920 lstrcpynA(Symbol->Name, si->Name, len);
924 /******************************************************************
925 * SymFromName (DBGHELP.@)
928 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
930 struct process* pcs = process_find_by_handle(hProcess);
931 struct module* module;
932 struct hash_table_iter hti;
934 struct symt_ht* sym = NULL;
937 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
938 if (!pcs) return FALSE;
939 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
940 name = strchr(Name, '!');
944 assert(name - Name < sizeof(tmp));
945 memcpy(tmp, Name, name - Name);
946 tmp[name - Name] = '\0';
947 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
948 if (!module) return FALSE;
949 Name = (char*)(name + 1);
951 else module = pcs->lmodules;
953 /* FIXME: Name could be made out of a regular expression */
954 for (; module; module = (name) ? NULL : module->next)
956 if (module->module.SymType == SymNone) continue;
957 if (module->module.SymType == SymDeferred)
959 struct module* xmodule = module_get_debug(pcs, module);
960 if (!xmodule || xmodule != module) continue;
962 hash_table_iter_init(&module->ht_symbols, &hti, Name);
963 while ((ptr = hash_table_iter_up(&hti)))
965 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
967 if (!strcmp(sym->hash_elt.name, Name))
969 symt_fill_sym_info(module, &sym->symt, Symbol);
977 /***********************************************************************
978 * SymGetSymFromName (DBGHELP.@)
980 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
982 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
983 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
986 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
987 si->SizeOfStruct = sizeof(*si);
988 si->MaxNameLen = MAX_SYM_NAME;
989 if (!SymFromName(hProcess, Name, si)) return FALSE;
991 Symbol->Address = si->Address;
992 Symbol->Size = si->Size;
993 Symbol->Flags = si->Flags;
994 len = min(Symbol->MaxNameLength, si->MaxNameLen);
995 lstrcpynA(Symbol->Name, si->Name, len);
999 /******************************************************************
1000 * sym_fill_func_line_info
1002 * fills information about a file
1004 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
1005 DWORD addr, IMAGEHLP_LINE* line)
1007 struct line_info* dli = NULL;
1010 assert(func->symt.tag == SymTagFunction);
1012 while ((dli = vector_iter_down(&func->vlines, dli)))
1014 if (!dli->is_source_file)
1016 if (found || dli->u.pc_offset > addr) continue;
1017 line->LineNumber = dli->line_number;
1018 line->Address = dli->u.pc_offset;
1025 line->FileName = (char*)source_get(module, dli->u.source_file);
1032 /***********************************************************************
1033 * SymGetSymNext (DBGHELP.@)
1035 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1038 * get module from Symbol.Address
1039 * get index in module.addr_sorttab of Symbol.Address
1041 * if out of module bounds, move to next module in process address space
1043 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1044 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1048 /***********************************************************************
1049 * SymGetSymPrev (DBGHELP.@)
1052 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1054 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1055 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1059 /******************************************************************
1060 * SymGetLineFromAddr (DBGHELP.@)
1063 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1064 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1066 struct process* pcs = process_find_by_handle(hProcess);
1067 struct module* module;
1070 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1072 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1074 if (!pcs) return FALSE;
1075 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1076 if (!(module = module_get_debug(pcs, module))) return FALSE;
1077 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1079 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1080 if (!symt_fill_func_line_info(module,
1081 (struct symt_function*)module->addr_sorttab[idx],
1082 dwAddr, Line)) return FALSE;
1083 *pdwDisplacement = dwAddr - Line->Address;
1087 /******************************************************************
1088 * copy_line_64_from_32 (internal)
1091 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1094 l64->Key = l32->Key;
1095 l64->LineNumber = l32->LineNumber;
1096 l64->FileName = l32->FileName;
1097 l64->Address = l32->Address;
1100 /******************************************************************
1101 * copy_line_32_from_64 (internal)
1104 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1107 l32->Key = l64->Key;
1108 l32->LineNumber = l64->LineNumber;
1109 l32->FileName = l64->FileName;
1110 l32->Address = l64->Address;
1113 /******************************************************************
1114 * SymGetLineFromAddr64 (DBGHELP.@)
1117 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1118 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1120 IMAGEHLP_LINE line32;
1122 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1123 if (!validate_addr64(dwAddr)) return FALSE;
1124 line32.SizeOfStruct = sizeof(line32);
1125 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1127 copy_line_64_from_32(Line, &line32);
1131 /******************************************************************
1132 * SymGetLinePrev (DBGHELP.@)
1135 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1137 struct process* pcs = process_find_by_handle(hProcess);
1138 struct module* module;
1139 struct line_info* li;
1140 BOOL in_search = FALSE;
1142 TRACE("(%p %p)\n", hProcess, Line);
1144 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1146 if (!pcs) return FALSE;
1147 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1148 if (!(module = module_get_debug(pcs, module))) return FALSE;
1150 if (Line->Key == 0) return FALSE;
1151 li = (struct line_info*)Line->Key;
1152 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1153 * element we have to go back until we find the prev one to get the real
1154 * source file name for the DLIT_OFFSET element just before
1155 * the first DLIT_SOURCEFILE
1157 while (!li->is_first)
1160 if (!li->is_source_file)
1162 Line->LineNumber = li->line_number;
1163 Line->Address = li->u.pc_offset;
1165 if (!in_search) return TRUE;
1171 Line->FileName = (char*)source_get(module, li->u.source_file);
1177 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1181 /******************************************************************
1182 * SymGetLinePrev64 (DBGHELP.@)
1185 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1187 IMAGEHLP_LINE line32;
1189 line32.SizeOfStruct = sizeof(line32);
1190 copy_line_32_from_64(&line32, Line);
1191 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1192 copy_line_64_from_32(Line, &line32);
1196 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1198 struct line_info* li;
1200 if (line->Key == 0) return FALSE;
1201 li = (struct line_info*)line->Key;
1202 while (!li->is_last)
1205 if (!li->is_source_file)
1207 line->LineNumber = li->line_number;
1208 line->Address = li->u.pc_offset;
1212 line->FileName = (char*)source_get(module, li->u.source_file);
1217 /******************************************************************
1218 * SymGetLineNext (DBGHELP.@)
1221 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1223 struct process* pcs = process_find_by_handle(hProcess);
1224 struct module* module;
1226 TRACE("(%p %p)\n", hProcess, Line);
1228 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1229 if (!pcs) return FALSE;
1230 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1231 if (!(module = module_get_debug(pcs, module))) return FALSE;
1233 if (symt_get_func_line_next(module, Line)) return TRUE;
1234 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1238 /******************************************************************
1239 * SymGetLineNext64 (DBGHELP.@)
1242 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1244 IMAGEHLP_LINE line32;
1246 line32.SizeOfStruct = sizeof(line32);
1247 copy_line_32_from_64(&line32, Line);
1248 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1249 copy_line_64_from_32(Line, &line32);
1253 /***********************************************************************
1254 * SymFunctionTableAccess (DBGHELP.@)
1256 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1258 WARN("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1262 /***********************************************************************
1263 * SymFunctionTableAccess64 (DBGHELP.@)
1265 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1267 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1271 /***********************************************************************
1272 * SymUnDName (DBGHELP.@)
1274 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1276 TRACE("(%p %s %lu)\n", sym, UnDecName, UnDecNameLength);
1277 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1278 UNDNAME_COMPLETE) != 0;
1281 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1282 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1284 /***********************************************************************
1285 * UnDecorateSymbolName (DBGHELP.@)
1287 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1288 DWORD UndecoratedLength, DWORD Flags)
1290 /* undocumented from msvcrt */
1291 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1292 static WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1294 TRACE("(%s, %p, %ld, 0x%08lx)\n",
1295 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1299 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1300 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1301 if (!p_undname) return 0;
1304 if (!UnDecoratedName) return 0;
1305 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1306 und_alloc, und_free, Flags))
1308 return strlen(UnDecoratedName);
1311 /******************************************************************
1312 * SymMatchString (DBGHELP.@)
1315 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1320 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1322 compile_regex(re, -1, &preg, _case);
1323 ret = regexec(&preg, string, 0, NULL, 0) == 0;
1328 /******************************************************************
1329 * SymSearch (DBGHELP.@)
1331 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1332 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1333 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1334 PVOID UserContext, DWORD Options)
1336 TRACE("(%p %s %lu %lu %s %s %p %p %lx)\n",
1337 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1338 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1339 UserContext, Options);
1343 FIXME("Unsupported searching for a given Index (%lu)\n", Index);
1344 SetLastError(ERROR_INVALID_PARAMETER);
1349 FIXME("Unsupported searching for a given SymTag (%lu)\n", SymTag);
1350 SetLastError(ERROR_INVALID_PARAMETER);
1355 FIXME("Unsupported searching for a given Address (%s)\n", wine_dbgstr_longlong(Address));
1356 SetLastError(ERROR_INVALID_PARAMETER);
1359 if (Options != SYMSEARCH_GLOBALSONLY)
1361 FIXME("Unsupported searching with options (%lx)\n", Options);
1362 SetLastError(ERROR_INVALID_PARAMETER);
1365 return SymEnumSymbols(hProcess, BaseOfDll, Mask, EnumSymbolsCallback, UserContext);