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);
45 unsigned long is_first : 1,
51 unsigned long pc_offset; /* if is_source_file isn't set */
52 unsigned source_file; /* if is_source_file is set */
56 inline static int cmp_addr(DWORD a1, DWORD a2)
58 if (a1 > a2) return 1;
59 if (a1 < a2) return -1;
63 inline static int cmp_sorttab_addr(const struct module* module, int idx, DWORD addr)
67 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
68 return cmp_addr(ref, addr);
71 int symt_cmp_addr(const void* p1, const void* p2)
73 const struct symt* sym1 = *(const struct symt* const *)p1;
74 const struct symt* sym2 = *(const struct symt* const *)p2;
77 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
78 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
79 return cmp_addr(a1, a2);
82 static inline void re_append(char** mask, unsigned* len, char ch)
84 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
85 (*mask)[*len - 2] = ch;
88 /* transforms a dbghelp's regular expression into a POSIX one
89 * Here are the valid dbghelp reg ex characters:
90 * * 0 or more characters
91 * ? a single character
93 * # 0 or more of preceding char
94 * + 1 or more of preceding char
95 * escapes \ on #, ?, [, ], *, +. don't work on -
97 static void compile_regex(const char* str, int numchar, regex_t* re)
99 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
101 BOOL in_escape = FALSE;
103 re_append(&mask, &len, '^');
105 while (*str && numchar--)
107 /* FIXME: this shouldn't be valid on '-' */
110 re_append(&mask, &len, '\\');
111 re_append(&mask, &len, *str);
116 case '\\': in_escape = TRUE; break;
117 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
118 case '?': re_append(&mask, &len, '.'); break;
119 case '#': re_append(&mask, &len, '*'); break;
120 /* escape some valid characters in dbghelp reg exp:s */
121 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
122 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
123 default: re_append(&mask, &len, *str); break;
129 re_append(&mask, &len, '\\');
130 re_append(&mask, &len, '\\');
132 re_append(&mask, &len, '$');
133 mask[len - 1] = '\0';
134 if (regcomp(re, mask, REG_NOSUB)) FIXME("Couldn't compile %s\n", mask);
135 HeapFree(GetProcessHeap(), 0, mask);
138 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
140 struct symt_compiland* sym;
142 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
143 module->module.ModuleName, name);
144 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
146 sym->symt.tag = SymTagCompiland;
147 sym->source = source_new(module, name);
148 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
153 struct symt_public* symt_new_public(struct module* module,
154 struct symt_compiland* compiland,
156 unsigned long address, unsigned size,
157 BOOL in_code, BOOL is_func)
159 struct symt_public* sym;
162 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
163 module->module.ModuleName, name, address);
164 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
166 sym->symt.tag = SymTagPublicSymbol;
167 sym->hash_elt.name = pool_strdup(&module->pool, name);
168 hash_table_add(&module->ht_symbols, &sym->hash_elt);
169 module->sortlist_valid = FALSE;
170 sym->container = compiland ? &compiland->symt : NULL;
171 sym->address = address;
173 sym->in_code = in_code;
174 sym->is_function = is_func;
177 p = vector_add(&compiland->vchildren, &module->pool);
184 struct symt_data* symt_new_global_variable(struct module* module,
185 struct symt_compiland* compiland,
186 const char* name, unsigned is_static,
187 unsigned long addr, unsigned long size,
190 struct symt_data* sym;
194 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
195 module->module.ModuleName, name, addr, type);
196 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
198 sym->symt.tag = SymTagData;
199 sym->hash_elt.name = pool_strdup(&module->pool, name);
200 hash_table_add(&module->ht_symbols, &sym->hash_elt);
201 module->sortlist_valid = FALSE;
202 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
203 sym->container = compiland ? &compiland->symt : NULL;
205 sym->u.address = addr;
206 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
209 FIXME("Size mismatch for %s.%s between type (%lu) and src (%lu)\n",
210 module->module.ModuleName, name, tsz, size);
214 p = vector_add(&compiland->vchildren, &module->pool);
221 struct symt_function* symt_new_function(struct module* module,
222 struct symt_compiland* compiland,
224 unsigned long addr, unsigned long size,
225 struct symt* sig_type)
227 struct symt_function* sym;
230 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
231 module->module.ModuleName, name, addr, addr + size - 1);
233 assert(!sig_type || sig_type->tag == SymTagFunctionType);
234 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
236 sym->symt.tag = SymTagFunction;
237 sym->hash_elt.name = pool_strdup(&module->pool, name);
238 hash_table_add(&module->ht_symbols, &sym->hash_elt);
239 module->sortlist_valid = FALSE;
240 sym->container = &compiland->symt;
242 sym->type = sig_type;
244 vector_init(&sym->vlines, sizeof(struct line_info), 64);
245 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
248 p = vector_add(&compiland->vchildren, &module->pool);
255 void symt_add_func_line(struct module* module, struct symt_function* func,
256 unsigned source_idx, int line_num, unsigned long offset)
258 struct line_info* dli;
259 BOOL last_matches = FALSE;
261 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
263 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
264 func, func->hash_elt.name, offset,
265 source_get(module, source_idx), line_num);
267 assert(func->symt.tag == SymTagFunction);
270 while ((dli = vector_iter_down(&func->vlines, dli)))
272 if (dli->is_source_file)
274 last_matches = (source_idx == dli->u.source_file);
281 /* we shouldn't have line changes on first line of function */
282 dli = vector_add(&func->vlines, &module->pool);
283 dli->is_source_file = 1;
284 dli->is_first = dli->is_last = 0;
285 dli->line_number = 0;
286 dli->u.source_file = source_idx;
288 dli = vector_add(&func->vlines, &module->pool);
289 dli->is_source_file = 0;
290 dli->is_first = dli->is_last = 0;
291 dli->line_number = line_num;
292 dli->u.pc_offset = func->address + offset;
295 struct symt_data* symt_add_func_local(struct module* module,
296 struct symt_function* func,
297 int regno, int offset,
298 struct symt_block* block,
299 struct symt* type, const char* name)
301 struct symt_data* locsym;
305 assert(func->symt.tag == SymTagFunction);
307 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
308 module->module.ModuleName, func->hash_elt.name,
310 locsym = pool_alloc(&module->pool, sizeof(*locsym));
311 locsym->symt.tag = SymTagData;
312 locsym->hash_elt.name = pool_strdup(&module->pool, name);
313 locsym->hash_elt.next = NULL;
314 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
315 locsym->container = &block->symt;
319 locsym->u.s.reg_id = regno;
320 locsym->u.s.offset = 0;
321 locsym->u.s.length = 0;
325 locsym->u.s.reg_id = 0;
326 locsym->u.s.offset = offset * 8;
327 locsym->u.s.length = 0;
330 p = vector_add(&block->vchildren, &module->pool);
332 p = vector_add(&func->vchildren, &module->pool);
337 struct symt_block* symt_open_func_block(struct module* module,
338 struct symt_function* func,
339 struct symt_block* parent_block,
340 unsigned pc, unsigned len)
342 struct symt_block* block;
346 assert(func->symt.tag == SymTagFunction);
348 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
349 block = pool_alloc(&module->pool, sizeof(*block));
350 block->symt.tag = SymTagBlock;
351 block->address = func->address + pc;
353 block->container = parent_block ? &parent_block->symt : &func->symt;
354 vector_init(&block->vchildren, sizeof(struct symt*), 4);
356 p = vector_add(&parent_block->vchildren, &module->pool);
358 p = vector_add(&func->vchildren, &module->pool);
364 struct symt_block* symt_close_func_block(struct module* module,
365 struct symt_function* func,
366 struct symt_block* block, unsigned pc)
368 assert(func->symt.tag == SymTagFunction);
370 if (pc) block->size = func->address + pc - block->address;
371 return (block->container->tag == SymTagBlock) ?
372 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
375 struct symt_function_point* symt_add_function_point(struct module* module,
376 struct symt_function* func,
377 enum SymTagEnum point,
378 unsigned offset, const char* name)
380 struct symt_function_point* sym;
383 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
385 sym->symt.tag = point;
387 sym->offset = offset;
388 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
389 p = vector_add(&func->vchildren, &module->pool);
395 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
398 struct line_info* dli;
401 /* We aren't adding any more locals or line numbers to this function.
402 * Free any spare memory that we might have allocated.
404 assert(func->symt.tag == SymTagFunction);
406 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
407 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
409 len = vector_length(&func->vlines);
412 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
413 dli = vector_at(&func->vlines, len); dli->is_last = 1;
418 struct symt_thunk* symt_new_thunk(struct module* module,
419 struct symt_compiland* compiland,
420 const char* name, THUNK_ORDINAL ord,
421 unsigned long addr, unsigned long size)
423 struct symt_thunk* sym;
425 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
426 module->module.ModuleName, name, addr, addr + size - 1);
428 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
430 sym->symt.tag = SymTagThunk;
431 sym->hash_elt.name = pool_strdup(&module->pool, name);
432 hash_table_add(&module->ht_symbols, &sym->hash_elt);
433 module->sortlist_valid = FALSE;
434 sym->container = &compiland->symt;
441 p = vector_add(&compiland->vchildren, &module->pool);
448 /* expect sym_info->MaxNameLen to be set before being called */
449 static void symt_fill_sym_info(const struct module* module,
450 const struct symt* sym, SYMBOL_INFO* sym_info)
454 sym_info->TypeIndex = (DWORD)sym;
455 sym_info->info = 0; /* TBD */
456 symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
457 sym_info->ModBase = module->module.BaseOfImage;
463 const struct symt_data* data = (const struct symt_data*)sym;
468 if (data->u.s.reg_id)
470 sym_info->Flags |= SYMFLAG_REGISTER;
471 sym_info->Register = data->u.s.reg_id;
472 sym_info->Address = 0;
476 if (data->u.s.offset < 0)
477 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
479 sym_info->Flags |= SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
480 sym_info->Register = CV_REG_EBP; /* FIXME: needed ? */
481 sym_info->Address = data->u.s.offset;
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 sym_info->NameLen = strlen(name) + 1;
529 if (sym_info->MaxNameLen)
531 strncpy(sym_info->Name, name, min(sym_info->NameLen, sym_info->MaxNameLen));
532 sym_info->Name[sym_info->MaxNameLen - 1] = '\0';
534 TRACE_(dbghelp_symt)("%p => %s %lu %lx\n",
535 sym, sym_info->Name, sym_info->Size, 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 /* FIXME: this is not true, we should only drop the public
552 * symbol iff no other one is found
554 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
555 sym->symt.tag == SymTagPublicSymbol) continue;
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 DWORD ref_addr, ref_size;
615 if (!module->sortlist_valid && !resort_symbols(module)) return -1;
618 * Binary search to find closest symbol.
621 high = module->module.NumSyms;
623 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
624 if (addr < ref_addr) return -1;
627 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
628 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
629 ref_size = 0x1000; /* arbitrary value */
630 if (addr >= ref_addr + ref_size) return -1;
633 while (high > low + 1)
635 mid = (high + low) / 2;
636 if (cmp_sorttab_addr(module, mid, addr) < 0)
641 if (low != high && high != module->module.NumSyms &&
642 cmp_sorttab_addr(module, high, addr) <= 0)
645 /* If found symbol is a public symbol, check if there are any other entries that
646 * might also have the same address, but would get better information
648 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
650 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
652 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
653 !cmp_sorttab_addr(module, low - 1, ref_addr))
655 else if (low < module->module.NumSyms - 1 &&
656 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
657 !cmp_sorttab_addr(module, low + 1, ref_addr))
660 /* finally check that we fit into the found symbol */
661 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
662 if (addr < ref_addr) return -1;
663 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
664 ref_size = 0x1000; /* arbitrary value */
665 if (addr >= ref_addr + ref_size) return -1;
670 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
671 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
672 PVOID user, SYMBOL_INFO* sym_info,
675 struct symt** plsym = NULL;
676 struct symt* lsym = NULL;
677 DWORD pc = pcs->ctx_frame.InstructionOffset;
679 while ((plsym = vector_iter_up(v, plsym)))
686 struct symt_block* block = (struct symt_block*)lsym;
687 if (pc < block->address || block->address + block->size <= pc)
689 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
690 sym_info, &block->vchildren))
695 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
697 symt_fill_sym_info(module, lsym, sym_info);
698 if (!cb(sym_info, sym_info->Size, user))
703 case SymTagFuncDebugStart:
704 case SymTagFuncDebugEnd:
707 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
714 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
715 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
718 struct module* module;
720 char buffer[sizeof(SYMBOL_INFO) + 256];
721 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
722 DWORD pc = pcs->ctx_frame.InstructionOffset;
725 sym_info->SizeOfStruct = sizeof(*sym_info);
726 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
728 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
729 if (!(module = module_get_debug(pcs, module))) return FALSE;
730 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
732 sym = module->addr_sorttab[idx];
733 if (sym->symt.tag == SymTagFunction)
738 compile_regex(mask ? mask : "*", -1, &preg);
739 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
740 UserContext, sym_info,
741 &((struct symt_function*)sym)->vchildren);
746 symt_fill_sym_info(module, &sym->symt, sym_info);
747 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
750 /******************************************************************
751 * SymEnumSymbols (DBGHELP.@)
753 * cases BaseOfDll = 0
754 * !foo fails always (despite what MSDN states)
755 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
756 * no ! in Mask, lookup in local Context
757 * cases BaseOfDll != 0
758 * !foo fails always (despite what MSDN states)
759 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
761 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG BaseOfDll, PCSTR Mask,
762 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
765 struct process* pcs = process_find_by_handle(hProcess);
766 struct module* module;
767 struct module* dbg_module;
769 regex_t mod_regex, sym_regex;
771 TRACE("(%p %08lx %s %p %p)\n",
772 hProcess, BaseOfDll, debugstr_a(Mask), EnumSymbolsCallback, UserContext);
774 if (!pcs) return FALSE;
778 /* do local variables ? */
779 if (!Mask || !(bang = strchr(Mask, '!')))
780 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
782 if (bang == Mask) return FALSE;
784 compile_regex(Mask, bang - Mask, &mod_regex);
785 compile_regex(bang + 1, -1, &sym_regex);
787 for (module = pcs->lmodules; module; module = module->next)
789 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
791 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
792 symt_enum_module(dbg_module, &sym_regex,
793 EnumSymbolsCallback, UserContext))
797 /* not found in PE modules, retry on the ELF ones
799 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
801 for (module = pcs->lmodules; module; module = module->next)
803 if (module->type == DMT_ELF &&
804 !module_get_containee(pcs, module) &&
805 (dbg_module = module_get_debug(pcs, module)))
807 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
808 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
817 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
818 if (!(module = module_get_debug(pcs, module)))
821 /* we always ignore module name from Mask when BaseOfDll is defined */
822 if (Mask && (bang = strchr(Mask, '!')))
824 if (bang == Mask) return FALSE;
828 compile_regex(Mask ? Mask : "*", -1, &sym_regex);
829 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
838 PSYM_ENUMSYMBOLS_CALLBACK cb;
841 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
843 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
844 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
847 /***********************************************************************
848 * SymEnumerateSymbols (DBGHELP.@)
850 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
851 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
854 struct sym_enumerate se;
856 se.ctx = UserContext;
857 se.cb = EnumSymbolsCallback;
859 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
862 /******************************************************************
863 * SymFromAddr (DBGHELP.@)
866 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD Address,
867 DWORD* Displacement, PSYMBOL_INFO Symbol)
869 struct process* pcs = process_find_by_handle(hProcess);
870 struct module* module;
874 if (!pcs) return FALSE;
875 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
876 if (!(module = module_get_debug(pcs, module))) return FALSE;
877 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
879 sym = module->addr_sorttab[idx];
881 symt_fill_sym_info(module, &sym->symt, Symbol);
882 if (Displacement) *Displacement = Address - Symbol->Address;
886 /******************************************************************
887 * SymGetSymFromAddr (DBGHELP.@)
890 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
891 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
893 char buffer[sizeof(SYMBOL_INFO) + 256];
894 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
897 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
898 si->SizeOfStruct = sizeof(*si);
899 si->MaxNameLen = 256;
900 if (!SymFromAddr(hProcess, Address, Displacement, si))
903 Symbol->Address = si->Address;
904 Symbol->Size = si->Size;
905 Symbol->Flags = si->Flags;
906 len = min(Symbol->MaxNameLength, si->MaxNameLen);
907 strncpy(Symbol->Name, si->Name, len);
908 Symbol->Name[len - 1] = '\0';
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 strncpy(Symbol->Name, si->Name, len);
984 Symbol->Name[len - 1] = '\0';
988 /******************************************************************
989 * sym_fill_func_line_info
991 * fills information about a file
993 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
994 DWORD addr, IMAGEHLP_LINE* line)
996 struct line_info* dli = NULL;
999 assert(func->symt.tag == SymTagFunction);
1001 while ((dli = vector_iter_down(&func->vlines, dli)))
1003 if (!dli->is_source_file)
1005 if (found || dli->u.pc_offset > addr) continue;
1006 line->LineNumber = dli->line_number;
1007 line->Address = dli->u.pc_offset;
1014 line->FileName = (char*)source_get(module, dli->u.source_file);
1021 /***********************************************************************
1022 * SymGetSymNext (DBGHELP.@)
1024 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1027 * get module from Symbol.Address
1028 * get index in module.addr_sorttab of Symbol.Address
1030 * if out of module bounds, move to next module in process address space
1032 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1033 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1037 /***********************************************************************
1038 * SymGetSymPrev (DBGHELP.@)
1041 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1043 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1044 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1048 /******************************************************************
1049 * SymGetLineFromAddr (DBGHELP.@)
1052 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1053 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1055 struct process* pcs = process_find_by_handle(hProcess);
1056 struct module* module;
1059 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1061 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1063 if (!pcs) return FALSE;
1064 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1065 if (!(module = module_get_debug(pcs, module))) return FALSE;
1066 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1068 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1069 if (!symt_fill_func_line_info(module,
1070 (struct symt_function*)module->addr_sorttab[idx],
1071 dwAddr, Line)) return FALSE;
1072 if (pdwDisplacement) *pdwDisplacement = dwAddr - Line->Address;
1076 /******************************************************************
1077 * SymGetLinePrev (DBGHELP.@)
1080 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1082 struct process* pcs = process_find_by_handle(hProcess);
1083 struct module* module;
1084 struct line_info* li;
1085 BOOL in_search = FALSE;
1087 TRACE("(%p %p)\n", hProcess, Line);
1089 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1091 if (!pcs) return FALSE;
1092 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1093 if (!(module = module_get_debug(pcs, module))) return FALSE;
1095 if (Line->Key == 0) return FALSE;
1096 li = (struct line_info*)Line->Key;
1097 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1098 * element we have to go back until we find the prev one to get the real
1099 * source file name for the DLIT_OFFSET element just before
1100 * the first DLIT_SOURCEFILE
1102 while (!li->is_first)
1105 if (!li->is_source_file)
1107 Line->LineNumber = li->line_number;
1108 Line->Address = li->u.pc_offset;
1110 if (!in_search) return TRUE;
1116 Line->FileName = (char*)source_get(module, li->u.source_file);
1122 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1126 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1128 struct line_info* li;
1130 if (line->Key == 0) return FALSE;
1131 li = (struct line_info*)line->Key;
1132 while (!li->is_last)
1135 if (!li->is_source_file)
1137 line->LineNumber = li->line_number;
1138 line->Address = li->u.pc_offset;
1142 line->FileName = (char*)source_get(module, li->u.source_file);
1147 /******************************************************************
1148 * SymGetLineNext (DBGHELP.@)
1151 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1153 struct process* pcs = process_find_by_handle(hProcess);
1154 struct module* module;
1156 TRACE("(%p %p)\n", hProcess, Line);
1158 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1159 if (!pcs) return FALSE;
1160 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1161 if (!(module = module_get_debug(pcs, module))) return FALSE;
1163 if (symt_get_func_line_next(module, Line)) return TRUE;
1164 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1168 /***********************************************************************
1169 * SymFunctionTableAccess (DBGHELP.@)
1171 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1173 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1174 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1178 /***********************************************************************
1179 * SymUnDName (DBGHELP.@)
1181 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1183 FIXME("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1184 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1188 /***********************************************************************
1189 * UnDecorateSymbolName (DBGHELP.@)
1191 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1192 DWORD UndecoratedLength, DWORD Flags)
1194 FIXME("(%s, %p, %ld, 0x%08lx): stub\n",
1195 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1197 strncpy(UnDecoratedName, DecoratedName, UndecoratedLength);
1198 UnDecoratedName[UndecoratedLength - 1] = '\0';