2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include <sys/types.h>
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
41 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
43 inline static int cmp_addr(ULONG64 a1, ULONG64 a2)
45 if (a1 > a2) return 1;
46 if (a1 < a2) return -1;
50 inline static int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
54 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
55 return cmp_addr(ref, addr);
58 int symt_cmp_addr(const void* p1, const void* p2)
60 const struct symt* sym1 = *(const struct symt* const *)p1;
61 const struct symt* sym2 = *(const struct symt* const *)p2;
64 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
65 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
66 return cmp_addr(a1, a2);
69 static inline void re_append(char** mask, unsigned* len, char ch)
71 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
72 (*mask)[*len - 2] = ch;
75 /* transforms a dbghelp's regular expression into a POSIX one
76 * Here are the valid dbghelp reg ex characters:
77 * * 0 or more characters
78 * ? a single character
80 * # 0 or more of preceding char
81 * + 1 or more of preceding char
82 * escapes \ on #, ?, [, ], *, +. don't work on -
84 static void compile_regex(const char* str, int numchar, regex_t* re)
86 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
88 BOOL in_escape = FALSE;
90 re_append(&mask, &len, '^');
92 while (*str && numchar--)
94 /* FIXME: this shouldn't be valid on '-' */
97 re_append(&mask, &len, '\\');
98 re_append(&mask, &len, *str);
103 case '\\': in_escape = TRUE; break;
104 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
105 case '?': re_append(&mask, &len, '.'); break;
106 case '#': re_append(&mask, &len, '*'); break;
107 /* escape some valid characters in dbghelp reg exp:s */
108 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
109 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
110 default: re_append(&mask, &len, *str); break;
116 re_append(&mask, &len, '\\');
117 re_append(&mask, &len, '\\');
119 re_append(&mask, &len, '$');
120 mask[len - 1] = '\0';
121 if (regcomp(re, mask, REG_NOSUB)) FIXME("Couldn't compile %s\n", mask);
122 HeapFree(GetProcessHeap(), 0, mask);
125 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
127 struct symt_compiland* sym;
129 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
130 module->module.ModuleName, name);
131 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
133 sym->symt.tag = SymTagCompiland;
134 sym->source = source_new(module, name);
135 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
140 struct symt_public* symt_new_public(struct module* module,
141 struct symt_compiland* compiland,
143 unsigned long address, unsigned size,
144 BOOL in_code, BOOL is_func)
146 struct symt_public* sym;
149 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
150 module->module.ModuleName, name, address);
151 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
152 symt_find_nearest(module, address) != -1)
154 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
156 sym->symt.tag = SymTagPublicSymbol;
157 sym->hash_elt.name = pool_strdup(&module->pool, name);
158 hash_table_add(&module->ht_symbols, &sym->hash_elt);
159 module->sortlist_valid = FALSE;
160 sym->container = compiland ? &compiland->symt : NULL;
161 sym->address = address;
163 sym->in_code = in_code;
164 sym->is_function = is_func;
167 p = vector_add(&compiland->vchildren, &module->pool);
174 struct symt_data* symt_new_global_variable(struct module* module,
175 struct symt_compiland* compiland,
176 const char* name, unsigned is_static,
177 unsigned long addr, unsigned long size,
180 struct symt_data* sym;
184 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
185 module->module.ModuleName, name, addr, type);
186 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
188 sym->symt.tag = SymTagData;
189 sym->hash_elt.name = pool_strdup(&module->pool, name);
190 hash_table_add(&module->ht_symbols, &sym->hash_elt);
191 module->sortlist_valid = FALSE;
192 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
193 sym->container = compiland ? &compiland->symt : NULL;
195 sym->u.address = addr;
196 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
199 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
200 module->module.ModuleName, name,
201 wine_dbgstr_longlong(tsz), size);
205 p = vector_add(&compiland->vchildren, &module->pool);
212 struct symt_function* symt_new_function(struct module* module,
213 struct symt_compiland* compiland,
215 unsigned long addr, unsigned long size,
216 struct symt* sig_type)
218 struct symt_function* sym;
221 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
222 module->module.ModuleName, name, addr, addr + size - 1);
224 assert(!sig_type || sig_type->tag == SymTagFunctionType);
225 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
227 sym->symt.tag = SymTagFunction;
228 sym->hash_elt.name = pool_strdup(&module->pool, name);
229 hash_table_add(&module->ht_symbols, &sym->hash_elt);
230 module->sortlist_valid = FALSE;
231 sym->container = &compiland->symt;
233 sym->type = sig_type;
235 vector_init(&sym->vlines, sizeof(struct line_info), 64);
236 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
239 p = vector_add(&compiland->vchildren, &module->pool);
246 void symt_add_func_line(struct module* module, struct symt_function* func,
247 unsigned source_idx, int line_num, unsigned long offset)
249 struct line_info* dli;
250 BOOL last_matches = FALSE;
252 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
254 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
255 func, func->hash_elt.name, offset,
256 source_get(module, source_idx), line_num);
258 assert(func->symt.tag == SymTagFunction);
261 while ((dli = vector_iter_down(&func->vlines, dli)))
263 if (dli->is_source_file)
265 last_matches = (source_idx == dli->u.source_file);
272 /* we shouldn't have line changes on first line of function */
273 dli = vector_add(&func->vlines, &module->pool);
274 dli->is_source_file = 1;
275 dli->is_first = dli->is_last = 0;
276 dli->line_number = 0;
277 dli->u.source_file = source_idx;
279 dli = vector_add(&func->vlines, &module->pool);
280 dli->is_source_file = 0;
281 dli->is_first = dli->is_last = 0;
282 dli->line_number = line_num;
283 dli->u.pc_offset = func->address + offset;
286 struct symt_data* symt_add_func_local(struct module* module,
287 struct symt_function* func,
288 int regno, int offset,
289 struct symt_block* block,
290 struct symt* type, const char* name)
292 struct symt_data* locsym;
296 assert(func->symt.tag == SymTagFunction);
298 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
299 module->module.ModuleName, func->hash_elt.name,
301 locsym = pool_alloc(&module->pool, sizeof(*locsym));
302 locsym->symt.tag = SymTagData;
303 locsym->hash_elt.name = pool_strdup(&module->pool, name);
304 locsym->hash_elt.next = NULL;
305 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
306 locsym->container = &block->symt;
310 locsym->u.s.reg_id = regno;
311 locsym->u.s.offset = 0;
312 locsym->u.s.length = 0;
316 locsym->u.s.reg_id = 0;
317 locsym->u.s.offset = offset * 8;
318 locsym->u.s.length = 0;
321 p = vector_add(&block->vchildren, &module->pool);
323 p = vector_add(&func->vchildren, &module->pool);
328 struct symt_block* symt_open_func_block(struct module* module,
329 struct symt_function* func,
330 struct symt_block* parent_block,
331 unsigned pc, unsigned len)
333 struct symt_block* block;
337 assert(func->symt.tag == SymTagFunction);
339 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
340 block = pool_alloc(&module->pool, sizeof(*block));
341 block->symt.tag = SymTagBlock;
342 block->address = func->address + pc;
344 block->container = parent_block ? &parent_block->symt : &func->symt;
345 vector_init(&block->vchildren, sizeof(struct symt*), 4);
347 p = vector_add(&parent_block->vchildren, &module->pool);
349 p = vector_add(&func->vchildren, &module->pool);
355 struct symt_block* symt_close_func_block(struct module* module,
356 struct symt_function* func,
357 struct symt_block* block, unsigned pc)
359 assert(func->symt.tag == SymTagFunction);
361 if (pc) block->size = func->address + pc - block->address;
362 return (block->container->tag == SymTagBlock) ?
363 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
366 struct symt_function_point* symt_add_function_point(struct module* module,
367 struct symt_function* func,
368 enum SymTagEnum point,
369 unsigned offset, const char* name)
371 struct symt_function_point* sym;
374 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
376 sym->symt.tag = point;
378 sym->offset = offset;
379 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
380 p = vector_add(&func->vchildren, &module->pool);
386 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
389 struct line_info* dli;
392 /* We aren't adding any more locals or line numbers to this function.
393 * Free any spare memory that we might have allocated.
395 assert(func->symt.tag == SymTagFunction);
397 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
398 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
400 len = vector_length(&func->vlines);
403 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
404 dli = vector_at(&func->vlines, len); dli->is_last = 1;
409 struct symt_thunk* symt_new_thunk(struct module* module,
410 struct symt_compiland* compiland,
411 const char* name, THUNK_ORDINAL ord,
412 unsigned long addr, unsigned long size)
414 struct symt_thunk* sym;
416 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
417 module->module.ModuleName, name, addr, addr + size - 1);
419 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
421 sym->symt.tag = SymTagThunk;
422 sym->hash_elt.name = pool_strdup(&module->pool, name);
423 hash_table_add(&module->ht_symbols, &sym->hash_elt);
424 module->sortlist_valid = FALSE;
425 sym->container = &compiland->symt;
432 p = vector_add(&compiland->vchildren, &module->pool);
439 /* expect sym_info->MaxNameLen to be set before being called */
440 static void symt_fill_sym_info(const struct module* module,
441 const struct symt* sym, SYMBOL_INFO* sym_info)
446 sym_info->TypeIndex = (DWORD)sym;
447 sym_info->info = 0; /* TBD */
448 symt_get_info(sym, TI_GET_LENGTH, &size);
449 sym_info->Size = (DWORD)size;
450 sym_info->ModBase = module->module.BaseOfImage;
456 const struct symt_data* data = (const struct symt_data*)sym;
461 if (data->u.s.reg_id)
463 sym_info->Flags |= SYMFLAG_REGISTER;
464 sym_info->Register = data->u.s.reg_id;
465 sym_info->Address = 0;
469 if (data->u.s.offset < 0)
470 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
472 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
473 /* FIXME: needed ? moreover, it's i386 dependent !!! */
474 sym_info->Register = CV_REG_EBP;
475 sym_info->Address = data->u.s.offset / 8;
479 case DataIsFileStatic:
480 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
481 sym_info->Register = 0;
484 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
485 switch (data->u.value.n1.n2.vt)
487 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
488 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
489 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
490 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
491 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
492 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
494 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
498 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
502 case SymTagPublicSymbol:
503 sym_info->Flags |= SYMFLAG_EXPORT;
504 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
507 sym_info->Flags |= SYMFLAG_FUNCTION;
508 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
511 sym_info->Flags |= SYMFLAG_THUNK;
512 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
515 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
516 sym_info->Register = 0;
519 sym_info->Scope = 0; /* FIXME */
520 sym_info->Tag = sym->tag;
521 name = symt_get_name(sym);
522 if (sym_info->MaxNameLen)
524 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
525 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
526 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
528 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
529 memcpy(sym_info->Name, name, sym_info->NameLen);
530 sym_info->Name[sym_info->NameLen] = '\0';
533 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
534 sym, sym_info->Name, sym_info->Size,
535 wine_dbgstr_longlong(sym_info->Address));
538 static BOOL symt_enum_module(struct module* module, regex_t* regex,
539 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
541 char buffer[sizeof(SYMBOL_INFO) + 256];
542 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
544 struct symt_ht* sym = NULL;
545 struct hash_table_iter hti;
547 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
548 while ((ptr = hash_table_iter_up(&hti)))
550 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
551 if (sym->hash_elt.name &&
552 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
554 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
555 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
556 symt_fill_sym_info(module, &sym->symt, sym_info);
557 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
563 /***********************************************************************
566 * Rebuild sorted list of symbols for a module.
568 static BOOL resort_symbols(struct module* module)
573 struct hash_table_iter hti;
575 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
576 while ((ptr = hash_table_iter_up(&hti)))
579 if (!(module->module.NumSyms = nsym)) return FALSE;
581 if (module->addr_sorttab)
582 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
583 module->addr_sorttab,
584 nsym * sizeof(struct symt_ht*));
586 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
587 nsym * sizeof(struct symt_ht*));
588 if (!module->addr_sorttab) return FALSE;
591 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
592 while ((ptr = hash_table_iter_up(&hti)))
594 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
596 module->addr_sorttab[nsym++] = sym;
599 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
600 return module->sortlist_valid = TRUE;
603 /* assume addr is in module */
604 int symt_find_nearest(struct module* module, DWORD addr)
607 ULONG64 ref_addr, ref_size;
609 if (!module->sortlist_valid || !module->addr_sorttab)
611 if (!resort_symbols(module)) return -1;
615 * Binary search to find closest symbol.
618 high = module->module.NumSyms;
620 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
621 if (addr < ref_addr) return -1;
624 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
625 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
626 ref_size = 0x1000; /* arbitrary value */
627 if (addr >= ref_addr + ref_size) return -1;
630 while (high > low + 1)
632 mid = (high + low) / 2;
633 if (cmp_sorttab_addr(module, mid, addr) < 0)
638 if (low != high && high != module->module.NumSyms &&
639 cmp_sorttab_addr(module, high, addr) <= 0)
642 /* If found symbol is a public symbol, check if there are any other entries that
643 * might also have the same address, but would get better information
645 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
647 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
649 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
650 !cmp_sorttab_addr(module, low - 1, ref_addr))
652 else if (low < module->module.NumSyms - 1 &&
653 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
654 !cmp_sorttab_addr(module, low + 1, ref_addr))
657 /* finally check that we fit into the found symbol */
658 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
659 if (addr < ref_addr) return -1;
660 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
661 ref_size = 0x1000; /* arbitrary value */
662 if (addr >= ref_addr + ref_size) return -1;
667 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
668 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
669 PVOID user, SYMBOL_INFO* sym_info,
672 struct symt** plsym = NULL;
673 struct symt* lsym = NULL;
674 DWORD pc = pcs->ctx_frame.InstructionOffset;
676 while ((plsym = vector_iter_up(v, plsym)))
683 struct symt_block* block = (struct symt_block*)lsym;
684 if (pc < block->address || block->address + block->size <= pc)
686 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
687 sym_info, &block->vchildren))
692 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
694 symt_fill_sym_info(module, lsym, sym_info);
695 if (!cb(sym_info, sym_info->Size, user))
700 case SymTagFuncDebugStart:
701 case SymTagFuncDebugEnd:
704 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
711 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
712 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
715 struct module* module;
717 char buffer[sizeof(SYMBOL_INFO) + 256];
718 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
719 DWORD pc = pcs->ctx_frame.InstructionOffset;
722 sym_info->SizeOfStruct = sizeof(*sym_info);
723 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
725 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
726 if (!(module = module_get_debug(pcs, module))) return FALSE;
727 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
729 sym = module->addr_sorttab[idx];
730 if (sym->symt.tag == SymTagFunction)
735 compile_regex(mask ? mask : "*", -1, &preg);
736 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
737 UserContext, sym_info,
738 &((struct symt_function*)sym)->vchildren);
743 symt_fill_sym_info(module, &sym->symt, sym_info);
744 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
747 /******************************************************************
748 * SymEnumSymbols (DBGHELP.@)
750 * cases BaseOfDll = 0
751 * !foo fails always (despite what MSDN states)
752 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
753 * no ! in Mask, lookup in local Context
754 * cases BaseOfDll != 0
755 * !foo fails always (despite what MSDN states)
756 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
758 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
759 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
762 struct process* pcs = process_find_by_handle(hProcess);
763 struct module* module;
764 struct module* dbg_module;
766 regex_t mod_regex, sym_regex;
768 TRACE("(%p %s %s %p %p)\n",
769 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
770 EnumSymbolsCallback, UserContext);
772 if (!pcs) return FALSE;
776 /* do local variables ? */
777 if (!Mask || !(bang = strchr(Mask, '!')))
778 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
780 if (bang == Mask) return FALSE;
782 compile_regex(Mask, bang - Mask, &mod_regex);
783 compile_regex(bang + 1, -1, &sym_regex);
785 for (module = pcs->lmodules; module; module = module->next)
787 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
789 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
790 symt_enum_module(dbg_module, &sym_regex,
791 EnumSymbolsCallback, UserContext))
795 /* not found in PE modules, retry on the ELF ones
797 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
799 for (module = pcs->lmodules; module; module = module->next)
801 if (module->type == DMT_ELF &&
802 !module_get_containee(pcs, module) &&
803 (dbg_module = module_get_debug(pcs, module)))
805 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
806 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
815 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
816 if (!(module = module_get_debug(pcs, module)))
819 /* we always ignore module name from Mask when BaseOfDll is defined */
820 if (Mask && (bang = strchr(Mask, '!')))
822 if (bang == Mask) return FALSE;
826 compile_regex(Mask ? Mask : "*", -1, &sym_regex);
827 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
836 PSYM_ENUMSYMBOLS_CALLBACK cb;
839 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
841 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
842 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
845 /***********************************************************************
846 * SymEnumerateSymbols (DBGHELP.@)
848 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
849 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
852 struct sym_enumerate se;
854 se.ctx = UserContext;
855 se.cb = EnumSymbolsCallback;
857 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
860 /******************************************************************
861 * SymFromAddr (DBGHELP.@)
864 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
865 DWORD64* Displacement, PSYMBOL_INFO Symbol)
867 struct process* pcs = process_find_by_handle(hProcess);
868 struct module* module;
872 if (!pcs) return FALSE;
873 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
874 if (!(module = module_get_debug(pcs, module))) return FALSE;
875 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
877 sym = module->addr_sorttab[idx];
879 symt_fill_sym_info(module, &sym->symt, Symbol);
880 *Displacement = Address - Symbol->Address;
884 /******************************************************************
885 * SymGetSymFromAddr (DBGHELP.@)
888 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
889 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
891 char buffer[sizeof(SYMBOL_INFO) + 256];
892 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
894 DWORD64 Displacement64;
896 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
897 si->SizeOfStruct = sizeof(*si);
898 si->MaxNameLen = 256;
899 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
903 *Displacement = Displacement64;
904 Symbol->Address = si->Address;
905 Symbol->Size = si->Size;
906 Symbol->Flags = si->Flags;
907 len = min(Symbol->MaxNameLength, si->MaxNameLen);
908 lstrcpynA(Symbol->Name, si->Name, len);
912 /******************************************************************
913 * SymFromName (DBGHELP.@)
916 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
918 struct process* pcs = process_find_by_handle(hProcess);
919 struct module* module;
920 struct hash_table_iter hti;
922 struct symt_ht* sym = NULL;
925 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
926 if (!pcs) return FALSE;
927 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
928 name = strchr(Name, '!');
932 assert(name - Name < sizeof(tmp));
933 memcpy(tmp, Name, name - Name);
934 tmp[name - Name] = '\0';
935 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
936 if (!module) return FALSE;
937 Name = (char*)(name + 1);
939 else module = pcs->lmodules;
941 /* FIXME: Name could be made out of a regular expression */
942 for (; module; module = (name) ? NULL : module->next)
944 if (module->module.SymType == SymNone) continue;
945 if (module->module.SymType == SymDeferred)
947 struct module* xmodule = module_get_debug(pcs, module);
948 if (!xmodule || xmodule != module) continue;
950 hash_table_iter_init(&module->ht_symbols, &hti, Name);
951 while ((ptr = hash_table_iter_up(&hti)))
953 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
955 if (!strcmp(sym->hash_elt.name, Name))
957 symt_fill_sym_info(module, &sym->symt, Symbol);
965 /***********************************************************************
966 * SymGetSymFromName (DBGHELP.@)
968 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
970 char buffer[sizeof(SYMBOL_INFO) + 256];
971 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
974 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
975 si->SizeOfStruct = sizeof(*si);
976 si->MaxNameLen = 256;
977 if (!SymFromName(hProcess, Name, si)) return FALSE;
979 Symbol->Address = si->Address;
980 Symbol->Size = si->Size;
981 Symbol->Flags = si->Flags;
982 len = min(Symbol->MaxNameLength, si->MaxNameLen);
983 lstrcpynA(Symbol->Name, si->Name, len);
987 /******************************************************************
988 * sym_fill_func_line_info
990 * fills information about a file
992 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
993 DWORD addr, IMAGEHLP_LINE* line)
995 struct line_info* dli = NULL;
998 assert(func->symt.tag == SymTagFunction);
1000 while ((dli = vector_iter_down(&func->vlines, dli)))
1002 if (!dli->is_source_file)
1004 if (found || dli->u.pc_offset > addr) continue;
1005 line->LineNumber = dli->line_number;
1006 line->Address = dli->u.pc_offset;
1013 line->FileName = (char*)source_get(module, dli->u.source_file);
1020 /***********************************************************************
1021 * SymGetSymNext (DBGHELP.@)
1023 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1026 * get module from Symbol.Address
1027 * get index in module.addr_sorttab of Symbol.Address
1029 * if out of module bounds, move to next module in process address space
1031 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1032 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1036 /***********************************************************************
1037 * SymGetSymPrev (DBGHELP.@)
1040 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1042 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1043 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1047 /******************************************************************
1048 * SymGetLineFromAddr (DBGHELP.@)
1051 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1052 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1054 struct process* pcs = process_find_by_handle(hProcess);
1055 struct module* module;
1058 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1060 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1062 if (!pcs) return FALSE;
1063 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1064 if (!(module = module_get_debug(pcs, module))) return FALSE;
1065 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1067 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1068 if (!symt_fill_func_line_info(module,
1069 (struct symt_function*)module->addr_sorttab[idx],
1070 dwAddr, Line)) return FALSE;
1071 *pdwDisplacement = dwAddr - Line->Address;
1075 /******************************************************************
1076 * SymGetLinePrev (DBGHELP.@)
1079 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1081 struct process* pcs = process_find_by_handle(hProcess);
1082 struct module* module;
1083 struct line_info* li;
1084 BOOL in_search = FALSE;
1086 TRACE("(%p %p)\n", hProcess, Line);
1088 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1090 if (!pcs) return FALSE;
1091 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1092 if (!(module = module_get_debug(pcs, module))) return FALSE;
1094 if (Line->Key == 0) return FALSE;
1095 li = (struct line_info*)Line->Key;
1096 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1097 * element we have to go back until we find the prev one to get the real
1098 * source file name for the DLIT_OFFSET element just before
1099 * the first DLIT_SOURCEFILE
1101 while (!li->is_first)
1104 if (!li->is_source_file)
1106 Line->LineNumber = li->line_number;
1107 Line->Address = li->u.pc_offset;
1109 if (!in_search) return TRUE;
1115 Line->FileName = (char*)source_get(module, li->u.source_file);
1121 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1125 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1127 struct line_info* li;
1129 if (line->Key == 0) return FALSE;
1130 li = (struct line_info*)line->Key;
1131 while (!li->is_last)
1134 if (!li->is_source_file)
1136 line->LineNumber = li->line_number;
1137 line->Address = li->u.pc_offset;
1141 line->FileName = (char*)source_get(module, li->u.source_file);
1146 /******************************************************************
1147 * SymGetLineNext (DBGHELP.@)
1150 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1152 struct process* pcs = process_find_by_handle(hProcess);
1153 struct module* module;
1155 TRACE("(%p %p)\n", hProcess, Line);
1157 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1158 if (!pcs) return FALSE;
1159 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1160 if (!(module = module_get_debug(pcs, module))) return FALSE;
1162 if (symt_get_func_line_next(module, Line)) return TRUE;
1163 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1167 /***********************************************************************
1168 * SymFunctionTableAccess (DBGHELP.@)
1170 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1172 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1173 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1177 /***********************************************************************
1178 * SymUnDName (DBGHELP.@)
1180 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1182 TRACE("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1183 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1184 UNDNAME_COMPLETE) != 0;
1187 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1188 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1190 /***********************************************************************
1191 * UnDecorateSymbolName (DBGHELP.@)
1193 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1194 DWORD UndecoratedLength, DWORD Flags)
1196 /* undocumented from msvcrt */
1197 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1198 static WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1200 TRACE("(%s, %p, %ld, 0x%08lx): stub\n",
1201 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1205 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1206 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1207 if (!p_undname) return 0;
1210 if (!UnDecoratedName) return 0;
1211 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1212 und_alloc, und_free, Flags))
1214 return strlen(UnDecoratedName);