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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include <sys/types.h>
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
44 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
46 if (a1 > a2) return 1;
47 if (a1 < a2) return -1;
51 static inline int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
55 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
56 return cmp_addr(ref, addr);
59 int symt_cmp_addr(const void* p1, const void* p2)
61 const struct symt* sym1 = *(const struct symt* const *)p1;
62 const struct symt* sym2 = *(const struct symt* const *)p2;
65 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
66 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
67 return cmp_addr(a1, a2);
72 /* transforms a dbghelp's regular expression into a POSIX one
73 * Here are the valid dbghelp reg ex characters:
74 * * 0 or more characters
75 * ? a single character
77 * # 0 or more of preceding char
78 * + 1 or more of preceding char
79 * escapes \ on #, ?, [, ], *, +. don't work on -
81 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
84 BOOL in_escape = FALSE;
85 unsigned flags = REG_NOSUB;
87 if (numchar == -1) numchar = strlen( str );
89 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
92 while (*str && numchar--)
94 /* FIXME: this shouldn't be valid on '-' */
103 case '\\': in_escape = TRUE; break;
104 case '*': *p++ = '.'; *p++ = '*'; break;
105 case '?': *p++ = '.'; break;
106 case '#': *p++ = '*'; break;
107 /* escape some valid characters in dbghelp reg exp:s */
108 case '$': *p++ = '\\'; *p++ = '$'; break;
109 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
110 default: *p++ = *str; break;
121 if (_case) flags |= REG_ICASE;
122 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
123 HeapFree(GetProcessHeap(), 0, mask);
126 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
131 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
133 if (!srcfile || !*srcfile) *p++ = '*';
134 else while (*srcfile)
158 ret = !regcomp(re, mask, REG_NOSUB);
159 HeapFree(GetProcessHeap(), 0, mask);
162 FIXME("Couldn't compile %s\n", mask);
163 SetLastError(ERROR_INVALID_PARAMETER);
168 static int match_regexp( const regex_t *re, const char *str )
170 return !regexec( re, str, 0, NULL, 0 );
173 #else /* HAVE_REGEX_H */
175 /* if we don't have regexp support, fall back to a simple string comparison */
183 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
185 if (numchar == -1) numchar = strlen( str );
187 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
188 memcpy( re->str, str, numchar );
189 re->str[numchar] = 0;
193 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
195 compile_regex( srcfile, -1, re, FALSE );
199 static int match_regexp( const regex_t *re, const char *str )
201 if (re->icase) return !lstrcmpiA( re->str, str );
202 return !strcmp( re->str, str );
205 static void regfree( regex_t *re )
207 HeapFree( GetProcessHeap(), 0, re->str );
210 #endif /* HAVE_REGEX_H */
212 struct symt_compiland* symt_new_compiland(struct module* module,
213 unsigned long address, unsigned src_idx)
215 struct symt_compiland* sym;
217 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
218 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
219 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
221 sym->symt.tag = SymTagCompiland;
222 sym->address = address;
223 sym->source = src_idx;
224 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
229 struct symt_public* symt_new_public(struct module* module,
230 struct symt_compiland* compiland,
232 unsigned long address, unsigned size,
233 BOOL in_code, BOOL is_func)
235 struct symt_public* sym;
238 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
239 debugstr_w(module->module.ModuleName), name, address);
240 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
241 symt_find_nearest(module, address) != NULL)
243 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
245 sym->symt.tag = SymTagPublicSymbol;
246 sym->hash_elt.name = pool_strdup(&module->pool, name);
247 hash_table_add(&module->ht_symbols, &sym->hash_elt);
248 module->sortlist_valid = FALSE;
249 sym->container = compiland ? &compiland->symt : NULL;
250 sym->address = address;
252 sym->in_code = in_code;
253 sym->is_function = is_func;
256 p = vector_add(&compiland->vchildren, &module->pool);
263 struct symt_data* symt_new_global_variable(struct module* module,
264 struct symt_compiland* compiland,
265 const char* name, unsigned is_static,
266 unsigned long addr, unsigned long size,
269 struct symt_data* sym;
273 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
274 debugstr_w(module->module.ModuleName), name, addr, type);
275 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
277 sym->symt.tag = SymTagData;
278 sym->hash_elt.name = pool_strdup(&module->pool, name);
279 hash_table_add(&module->ht_symbols, &sym->hash_elt);
280 module->sortlist_valid = FALSE;
281 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
282 sym->container = compiland ? &compiland->symt : NULL;
284 sym->u.var.offset = addr;
285 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
288 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
289 debugstr_w(module->module.ModuleName), name,
290 wine_dbgstr_longlong(tsz), size);
294 p = vector_add(&compiland->vchildren, &module->pool);
301 struct symt_function* symt_new_function(struct module* module,
302 struct symt_compiland* compiland,
304 unsigned long addr, unsigned long size,
305 struct symt* sig_type)
307 struct symt_function* sym;
310 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
311 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
313 assert(!sig_type || sig_type->tag == SymTagFunctionType);
314 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
316 sym->symt.tag = SymTagFunction;
317 sym->hash_elt.name = pool_strdup(&module->pool, name);
318 hash_table_add(&module->ht_symbols, &sym->hash_elt);
319 module->sortlist_valid = FALSE;
320 sym->container = &compiland->symt;
322 sym->type = sig_type;
324 vector_init(&sym->vlines, sizeof(struct line_info), 64);
325 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
328 p = vector_add(&compiland->vchildren, &module->pool);
335 void symt_add_func_line(struct module* module, struct symt_function* func,
336 unsigned source_idx, int line_num, unsigned long offset)
338 struct line_info* dli;
339 BOOL last_matches = FALSE;
342 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
344 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
345 func, func->hash_elt.name, offset,
346 source_get(module, source_idx), line_num);
348 assert(func->symt.tag == SymTagFunction);
350 for (i=vector_length(&func->vlines)-1; i>=0; i--)
352 dli = vector_at(&func->vlines, i);
353 if (dli->is_source_file)
355 last_matches = (source_idx == dli->u.source_file);
362 /* we shouldn't have line changes on first line of function */
363 dli = vector_add(&func->vlines, &module->pool);
364 dli->is_source_file = 1;
365 dli->is_first = dli->is_last = 0;
366 dli->line_number = 0;
367 dli->u.source_file = source_idx;
369 dli = vector_add(&func->vlines, &module->pool);
370 dli->is_source_file = 0;
371 dli->is_first = dli->is_last = 0;
372 dli->line_number = line_num;
373 dli->u.pc_offset = func->address + offset;
376 /******************************************************************
377 * symt_add_func_local
379 * Adds a new local/parameter to a given function:
380 * In any cases, dt tells whether it's a local variable or a parameter
381 * If regno it's not 0:
382 * - then variable is stored in a register
383 * - otherwise, value is referenced by register + offset
384 * Otherwise, the variable is stored on the stack:
385 * - offset is then the offset from the frame register
387 struct symt_data* symt_add_func_local(struct module* module,
388 struct symt_function* func,
390 const struct location* loc,
391 struct symt_block* block,
392 struct symt* type, const char* name)
394 struct symt_data* locsym;
397 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
398 debugstr_w(module->module.ModuleName), func->hash_elt.name,
402 assert(func->symt.tag == SymTagFunction);
403 assert(dt == DataIsParam || dt == DataIsLocal);
405 locsym = pool_alloc(&module->pool, sizeof(*locsym));
406 locsym->symt.tag = SymTagData;
407 locsym->hash_elt.name = pool_strdup(&module->pool, name);
408 locsym->hash_elt.next = NULL;
410 locsym->container = &block->symt;
412 locsym->u.var = *loc;
414 p = vector_add(&block->vchildren, &module->pool);
416 p = vector_add(&func->vchildren, &module->pool);
422 struct symt_block* symt_open_func_block(struct module* module,
423 struct symt_function* func,
424 struct symt_block* parent_block,
425 unsigned pc, unsigned len)
427 struct symt_block* block;
431 assert(func->symt.tag == SymTagFunction);
433 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
434 block = pool_alloc(&module->pool, sizeof(*block));
435 block->symt.tag = SymTagBlock;
436 block->address = func->address + pc;
438 block->container = parent_block ? &parent_block->symt : &func->symt;
439 vector_init(&block->vchildren, sizeof(struct symt*), 4);
441 p = vector_add(&parent_block->vchildren, &module->pool);
443 p = vector_add(&func->vchildren, &module->pool);
449 struct symt_block* symt_close_func_block(struct module* module,
450 struct symt_function* func,
451 struct symt_block* block, unsigned pc)
454 assert(func->symt.tag == SymTagFunction);
456 if (pc) block->size = func->address + pc - block->address;
457 return (block->container->tag == SymTagBlock) ?
458 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
461 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
462 struct symt_function* func,
463 enum SymTagEnum point,
464 const struct location* loc,
467 struct symt_hierarchy_point*sym;
470 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
472 sym->symt.tag = point;
473 sym->parent = &func->symt;
475 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
476 p = vector_add(&func->vchildren, &module->pool);
482 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
485 struct line_info* dli;
488 /* We aren't adding any more locals or line numbers to this function.
489 * Free any spare memory that we might have allocated.
491 assert(func->symt.tag == SymTagFunction);
493 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
494 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
496 len = vector_length(&func->vlines);
499 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
500 dli = vector_at(&func->vlines, len); dli->is_last = 1;
505 struct symt_thunk* symt_new_thunk(struct module* module,
506 struct symt_compiland* compiland,
507 const char* name, THUNK_ORDINAL ord,
508 unsigned long addr, unsigned long size)
510 struct symt_thunk* sym;
512 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
513 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
515 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
517 sym->symt.tag = SymTagThunk;
518 sym->hash_elt.name = pool_strdup(&module->pool, name);
519 hash_table_add(&module->ht_symbols, &sym->hash_elt);
520 module->sortlist_valid = FALSE;
521 sym->container = &compiland->symt;
528 p = vector_add(&compiland->vchildren, &module->pool);
535 struct symt_data* symt_new_constant(struct module* module,
536 struct symt_compiland* compiland,
537 const char* name, struct symt* type,
540 struct symt_data* sym;
542 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
543 debugstr_w(module->module.ModuleName), name);
545 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
547 sym->symt.tag = SymTagData;
548 sym->hash_elt.name = pool_strdup(&module->pool, name);
549 hash_table_add(&module->ht_symbols, &sym->hash_elt);
550 module->sortlist_valid = FALSE;
551 sym->kind = DataIsConstant;
552 sym->container = compiland ? &compiland->symt : NULL;
558 p = vector_add(&compiland->vchildren, &module->pool);
565 struct symt_hierarchy_point* symt_new_label(struct module* module,
566 struct symt_compiland* compiland,
567 const char* name, unsigned long address)
569 struct symt_hierarchy_point* sym;
571 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
572 debugstr_w(module->module.ModuleName), name);
574 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
576 sym->symt.tag = SymTagLabel;
577 sym->hash_elt.name = pool_strdup(&module->pool, name);
578 hash_table_add(&module->ht_symbols, &sym->hash_elt);
579 module->sortlist_valid = FALSE;
580 sym->loc.kind = loc_absolute;
581 sym->loc.offset = address;
582 sym->parent = compiland ? &compiland->symt : NULL;
586 p = vector_add(&compiland->vchildren, &module->pool);
593 /* expect sym_info->MaxNameLen to be set before being called */
594 static void symt_fill_sym_info(const struct module_pair* pair,
595 const struct symt_function* func,
596 const struct symt* sym, SYMBOL_INFO* sym_info)
601 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
602 sym_info->TypeIndex = 0;
603 sym_info->info = (DWORD)sym;
604 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
605 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
606 (!sym_info->TypeIndex ||
607 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
609 sym_info->Size = (DWORD)size;
610 sym_info->ModBase = pair->requested->module.BaseOfImage;
618 const struct symt_data* data = (const struct symt_data*)sym;
622 sym_info->Flags |= SYMFLAG_PARAMETER;
626 struct location loc = data->u.var;
628 if (loc.kind >= loc_user)
629 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
634 /* for now we report error cases as a negative register number */
635 sym_info->Flags |= SYMFLAG_LOCAL;
638 sym_info->Flags |= SYMFLAG_REGISTER;
639 sym_info->Register = loc.reg;
640 sym_info->Address = 0;
643 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
644 /* FIXME: it's i386 dependent !!! */
645 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
646 sym_info->Address = loc.offset;
649 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
655 case DataIsFileStatic:
656 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
657 sym_info->Register = 0;
660 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
661 switch (data->u.value.n1.n2.vt)
663 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
664 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
665 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
666 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
667 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
668 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
669 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
671 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
677 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
681 case SymTagPublicSymbol:
682 sym_info->Flags |= SYMFLAG_EXPORT;
683 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
686 sym_info->Flags |= SYMFLAG_FUNCTION;
687 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
690 sym_info->Flags |= SYMFLAG_THUNK;
691 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
694 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
695 sym_info->Register = 0;
698 sym_info->Scope = 0; /* FIXME */
699 sym_info->Tag = sym->tag;
700 name = symt_get_name(sym);
701 if (sym_info->MaxNameLen)
703 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
704 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
705 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
707 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
708 memcpy(sym_info->Name, name, sym_info->NameLen);
709 sym_info->Name[sym_info->NameLen] = '\0';
712 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
713 sym, sym_info->Name, sym_info->Size,
714 wine_dbgstr_longlong(sym_info->Address));
719 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
721 SYMBOL_INFO* sym_info;
725 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
728 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
729 const struct symt_function* func, const struct symt* sym)
731 symt_fill_sym_info(pair, func, sym, se->sym_info);
732 if (se->index && se->sym_info->info != se->index) return FALSE;
733 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
734 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
735 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
738 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
739 const struct sym_enum* se)
742 struct symt_ht* sym = NULL;
743 struct hash_table_iter hti;
745 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
746 while ((ptr = hash_table_iter_up(&hti)))
748 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
749 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
751 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
752 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
753 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
759 /***********************************************************************
762 * Rebuild sorted list of symbols for a module.
764 static BOOL resort_symbols(struct module* module)
768 struct hash_table_iter hti;
771 if (!(module->module.NumSyms = module->ht_symbols.num_elts))
774 if (module->addr_sorttab)
775 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
776 module->addr_sorttab,
777 module->module.NumSyms * sizeof(struct symt_ht*));
779 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
780 module->module.NumSyms * sizeof(struct symt_ht*));
781 if (!module->addr_sorttab) return FALSE;
783 module->num_sorttab = 0;
784 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
785 while ((ptr = hash_table_iter_up(&hti)))
787 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
789 /* Don't store in sorttab symbol without address, they are of
790 * no use here (e.g. constant values)
791 * As the number of those symbols is very couple (a couple per module)
792 * we don't bother for the unused spots at the end of addr_sorttab
794 if (symt_get_info(&sym->symt, TI_GET_ADDRESS, &addr))
795 module->addr_sorttab[module->num_sorttab++] = sym;
797 qsort(module->addr_sorttab, module->num_sorttab, sizeof(struct symt_ht*), symt_cmp_addr);
798 return module->sortlist_valid = TRUE;
801 static void symt_get_length(struct symt* symt, ULONG64* size)
805 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
808 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
809 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
810 *size = 0x1000; /* arbitrary value */
813 /* assume addr is in module */
814 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
817 ULONG64 ref_addr, ref_size;
819 if (!module->sortlist_valid || !module->addr_sorttab)
821 if (!resort_symbols(module)) return NULL;
825 * Binary search to find closest symbol.
828 high = module->num_sorttab;
830 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
831 if (addr < ref_addr) return NULL;
834 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
835 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
836 if (addr >= ref_addr + ref_size) return NULL;
839 while (high > low + 1)
841 mid = (high + low) / 2;
842 if (cmp_sorttab_addr(module, mid, addr) < 0)
847 if (low != high && high != module->num_sorttab &&
848 cmp_sorttab_addr(module, high, addr) <= 0)
851 /* If found symbol is a public symbol, check if there are any other entries that
852 * might also have the same address, but would get better information
854 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
856 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
858 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
859 !cmp_sorttab_addr(module, low - 1, ref_addr))
861 else if (low < module->num_sorttab - 1 &&
862 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
863 !cmp_sorttab_addr(module, low + 1, ref_addr))
866 /* finally check that we fit into the found symbol */
867 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
868 if (addr < ref_addr) return NULL;
869 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
870 if (addr >= ref_addr + ref_size) return NULL;
872 return module->addr_sorttab[low];
875 static BOOL symt_enum_locals_helper(struct module_pair* pair,
876 regex_t* preg, const struct sym_enum* se,
877 struct symt_function* func, const struct vector* v)
879 struct symt* lsym = NULL;
880 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
883 for (i=0; i<vector_length(v); i++)
885 lsym = *(struct symt**)vector_at(v, i);
890 struct symt_block* block = (struct symt_block*)lsym;
891 if (pc < block->address || block->address + block->size <= pc)
893 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
898 if (match_regexp(preg, symt_get_name(lsym)))
900 if (send_symbol(se, pair, func, lsym)) return FALSE;
904 case SymTagFuncDebugStart:
905 case SymTagFuncDebugEnd:
909 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
916 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
917 const struct sym_enum* se)
919 struct module_pair pair;
921 DWORD pc = pcs->ctx_frame.InstructionOffset;
923 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
924 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
927 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
928 if (!module_get_debug(&pair)) return FALSE;
929 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
931 if (sym->symt.tag == SymTagFunction)
936 compile_regex(mask ? mask : "*", -1, &preg,
937 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
938 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
939 &((struct symt_function*)sym)->vchildren);
944 return send_symbol(se, &pair, NULL, &sym->symt);
947 /******************************************************************
950 * Helper for transforming an ANSI symbol info into a UNICODE one.
951 * Assume that MaxNameLen is the same for both version (A & W).
953 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
955 siw->SizeOfStruct = si->SizeOfStruct;
956 siw->TypeIndex = si->TypeIndex;
957 siw->Reserved[0] = si->Reserved[0];
958 siw->Reserved[1] = si->Reserved[1];
959 siw->Index = si->info; /* FIXME: see dbghelp.h */
960 siw->Size = si->Size;
961 siw->ModBase = si->ModBase;
962 siw->Flags = si->Flags;
963 siw->Value = si->Value;
964 siw->Address = si->Address;
965 siw->Register = si->Register;
966 siw->Scope = si->Scope;
968 siw->NameLen = si->NameLen;
969 siw->MaxNameLen = si->MaxNameLen;
970 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
973 /******************************************************************
976 * Core routine for most of the enumeration of symbols
978 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
979 const struct sym_enum* se)
981 struct module_pair pair;
983 regex_t mod_regex, sym_regex;
985 pair.pcs = process_find_by_handle(hProcess);
988 /* do local variables ? */
989 if (!Mask || !(bang = strchr(Mask, '!')))
990 return symt_enum_locals(pair.pcs, Mask, se);
992 if (bang == Mask) return FALSE;
994 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
995 compile_regex(bang + 1, -1, &sym_regex,
996 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
998 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1000 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1002 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1003 symt_enum_module(&pair, &sym_regex, se))
1007 /* not found in PE modules, retry on the ELF ones
1009 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
1011 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1013 if (pair.requested->type == DMT_ELF &&
1014 !module_get_containee(pair.pcs, pair.requested) &&
1015 module_get_debug(&pair))
1017 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1018 symt_enum_module(&pair, &sym_regex, se))
1023 regfree(&mod_regex);
1024 regfree(&sym_regex);
1027 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1028 if (!module_get_debug(&pair))
1031 /* we always ignore module name from Mask when BaseOfDll is defined */
1032 if (Mask && (bang = strchr(Mask, '!')))
1034 if (bang == Mask) return FALSE;
1038 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1039 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1040 symt_enum_module(&pair, &sym_regex, se);
1041 regfree(&sym_regex);
1046 /******************************************************************
1047 * SymEnumSymbols (DBGHELP.@)
1049 * cases BaseOfDll = 0
1050 * !foo fails always (despite what MSDN states)
1051 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1052 * no ! in Mask, lookup in local Context
1053 * cases BaseOfDll != 0
1054 * !foo fails always (despite what MSDN states)
1055 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1057 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1058 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1063 TRACE("(%p %s %s %p %p)\n",
1064 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1065 EnumSymbolsCallback, UserContext);
1067 se.cb = EnumSymbolsCallback;
1068 se.user = UserContext;
1072 se.sym_info = (PSYMBOL_INFO)se.buffer;
1074 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1079 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1081 PSYMBOL_INFOW sym_info;
1082 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1086 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1088 struct sym_enumW* sew = ctx;
1090 copy_symbolW(sew->sym_info, si);
1092 return (sew->cb)(sew->sym_info, size, sew->ctx);
1095 /******************************************************************
1096 * SymEnumSymbolsW (DBGHELP.@)
1099 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1100 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1103 struct sym_enumW sew;
1107 sew.ctx = UserContext;
1108 sew.cb = EnumSymbolsCallback;
1109 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1113 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1114 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1115 if (!maskA) return FALSE;
1116 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1118 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1119 HeapFree(GetProcessHeap(), 0, maskA);
1124 struct sym_enumerate
1127 PSYM_ENUMSYMBOLS_CALLBACK cb;
1130 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1132 struct sym_enumerate* se = ctx;
1133 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1136 /***********************************************************************
1137 * SymEnumerateSymbols (DBGHELP.@)
1139 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1140 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1143 struct sym_enumerate se;
1145 se.ctx = UserContext;
1146 se.cb = EnumSymbolsCallback;
1148 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1151 struct sym_enumerate64
1154 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1157 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1159 struct sym_enumerate64* se = ctx;
1160 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1163 /***********************************************************************
1164 * SymEnumerateSymbols64 (DBGHELP.@)
1166 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1167 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1170 struct sym_enumerate64 se;
1172 se.ctx = UserContext;
1173 se.cb = EnumSymbolsCallback;
1175 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1178 /******************************************************************
1179 * SymFromAddr (DBGHELP.@)
1182 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1183 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1185 struct module_pair pair;
1186 struct symt_ht* sym;
1188 pair.pcs = process_find_by_handle(hProcess);
1189 if (!pair.pcs) return FALSE;
1190 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1191 if (!module_get_debug(&pair)) return FALSE;
1192 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1194 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1195 *Displacement = Address - Symbol->Address;
1199 /******************************************************************
1200 * SymFromAddrW (DBGHELP.@)
1203 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1204 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1210 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1211 si = HeapAlloc(GetProcessHeap(), 0, len);
1212 if (!si) return FALSE;
1214 si->SizeOfStruct = sizeof(*si);
1215 si->MaxNameLen = Symbol->MaxNameLen;
1216 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1218 copy_symbolW(Symbol, si);
1220 HeapFree(GetProcessHeap(), 0, si);
1224 /******************************************************************
1225 * SymGetSymFromAddr (DBGHELP.@)
1228 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1229 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1231 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1232 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1234 DWORD64 Displacement64;
1236 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1237 si->SizeOfStruct = sizeof(*si);
1238 si->MaxNameLen = MAX_SYM_NAME;
1239 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1243 *Displacement = Displacement64;
1244 Symbol->Address = si->Address;
1245 Symbol->Size = si->Size;
1246 Symbol->Flags = si->Flags;
1247 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1248 lstrcpynA(Symbol->Name, si->Name, len);
1252 /******************************************************************
1253 * SymGetSymFromAddr64 (DBGHELP.@)
1256 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1257 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1259 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1260 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1262 DWORD64 Displacement64;
1264 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1265 si->SizeOfStruct = sizeof(*si);
1266 si->MaxNameLen = MAX_SYM_NAME;
1267 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1271 *Displacement = Displacement64;
1272 Symbol->Address = si->Address;
1273 Symbol->Size = si->Size;
1274 Symbol->Flags = si->Flags;
1275 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1276 lstrcpynA(Symbol->Name, si->Name, len);
1280 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1281 SYMBOL_INFO* symbol)
1283 struct hash_table_iter hti;
1285 struct symt_ht* sym = NULL;
1286 struct module_pair pair;
1289 if (!(pair.requested = module)) return FALSE;
1290 if (!module_get_debug(&pair)) return FALSE;
1292 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1293 while ((ptr = hash_table_iter_up(&hti)))
1295 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1297 if (!strcmp(sym->hash_elt.name, name))
1299 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1306 /******************************************************************
1307 * SymFromName (DBGHELP.@)
1310 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1312 struct process* pcs = process_find_by_handle(hProcess);
1313 struct module* module;
1316 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1317 if (!pcs) return FALSE;
1318 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1319 name = strchr(Name, '!');
1323 assert(name - Name < sizeof(tmp));
1324 memcpy(tmp, Name, name - Name);
1325 tmp[name - Name] = '\0';
1326 module = module_find_by_nameA(pcs, tmp);
1327 return find_name(pcs, module, name + 1, Symbol);
1329 for (module = pcs->lmodules; module; module = module->next)
1331 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1334 /* not found in PE modules, retry on the ELF ones
1336 if (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES)
1338 for (module = pcs->lmodules; module; module = module->next)
1340 if (module->type == DMT_ELF && !module_get_containee(pcs, module) &&
1341 find_name(pcs, module, Name, Symbol))
1348 /***********************************************************************
1349 * SymGetSymFromName (DBGHELP.@)
1351 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1353 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1354 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1357 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1358 si->SizeOfStruct = sizeof(*si);
1359 si->MaxNameLen = MAX_SYM_NAME;
1360 if (!SymFromName(hProcess, Name, si)) return FALSE;
1362 Symbol->Address = si->Address;
1363 Symbol->Size = si->Size;
1364 Symbol->Flags = si->Flags;
1365 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1366 lstrcpynA(Symbol->Name, si->Name, len);
1370 /******************************************************************
1371 * sym_fill_func_line_info
1373 * fills information about a file
1375 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1376 DWORD addr, IMAGEHLP_LINE* line)
1378 struct line_info* dli = NULL;
1382 assert(func->symt.tag == SymTagFunction);
1384 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1386 dli = vector_at(&func->vlines, i);
1387 if (!dli->is_source_file)
1389 if (found || dli->u.pc_offset > addr) continue;
1390 line->LineNumber = dli->line_number;
1391 line->Address = dli->u.pc_offset;
1398 line->FileName = (char*)source_get(module, dli->u.source_file);
1405 /***********************************************************************
1406 * SymGetSymNext (DBGHELP.@)
1408 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1411 * get module from Symbol.Address
1412 * get index in module.addr_sorttab of Symbol.Address
1414 * if out of module bounds, move to next module in process address space
1416 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1417 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1421 /***********************************************************************
1422 * SymGetSymPrev (DBGHELP.@)
1425 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1427 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1428 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1432 /******************************************************************
1433 * SymGetLineFromAddr (DBGHELP.@)
1436 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1437 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1439 struct module_pair pair;
1440 struct symt_ht* symt;
1442 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1444 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1446 pair.pcs = process_find_by_handle(hProcess);
1447 if (!pair.pcs) return FALSE;
1448 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1449 if (!module_get_debug(&pair)) return FALSE;
1450 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1452 if (symt->symt.tag != SymTagFunction) return FALSE;
1453 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1454 dwAddr, Line)) return FALSE;
1455 *pdwDisplacement = dwAddr - Line->Address;
1459 /******************************************************************
1460 * copy_line_64_from_32 (internal)
1463 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1466 l64->Key = l32->Key;
1467 l64->LineNumber = l32->LineNumber;
1468 l64->FileName = l32->FileName;
1469 l64->Address = l32->Address;
1472 /******************************************************************
1473 * copy_line_W64_from_32 (internal)
1476 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1480 l64->Key = l32->Key;
1481 l64->LineNumber = l32->LineNumber;
1482 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1483 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1484 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1485 l64->Address = l32->Address;
1488 /******************************************************************
1489 * copy_line_32_from_64 (internal)
1492 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1495 l32->Key = l64->Key;
1496 l32->LineNumber = l64->LineNumber;
1497 l32->FileName = l64->FileName;
1498 l32->Address = l64->Address;
1501 /******************************************************************
1502 * SymGetLineFromAddr64 (DBGHELP.@)
1505 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1506 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1508 IMAGEHLP_LINE line32;
1510 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1511 if (!validate_addr64(dwAddr)) return FALSE;
1512 line32.SizeOfStruct = sizeof(line32);
1513 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1515 copy_line_64_from_32(Line, &line32);
1519 /******************************************************************
1520 * SymGetLineFromAddrW64 (DBGHELP.@)
1523 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1524 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1526 struct process* pcs = process_find_by_handle(hProcess);
1527 IMAGEHLP_LINE line32;
1529 if (!pcs) return FALSE;
1530 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1531 if (!validate_addr64(dwAddr)) return FALSE;
1532 line32.SizeOfStruct = sizeof(line32);
1533 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1535 copy_line_W64_from_32(pcs, Line, &line32);
1539 /******************************************************************
1540 * SymGetLinePrev (DBGHELP.@)
1543 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1545 struct module_pair pair;
1546 struct line_info* li;
1547 BOOL in_search = FALSE;
1549 TRACE("(%p %p)\n", hProcess, Line);
1551 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1553 pair.pcs = process_find_by_handle(hProcess);
1554 if (!pair.pcs) return FALSE;
1555 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1556 if (!module_get_debug(&pair)) return FALSE;
1558 if (Line->Key == 0) return FALSE;
1560 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1561 * element we have to go back until we find the prev one to get the real
1562 * source file name for the DLIT_OFFSET element just before
1563 * the first DLIT_SOURCEFILE
1565 while (!li->is_first)
1568 if (!li->is_source_file)
1570 Line->LineNumber = li->line_number;
1571 Line->Address = li->u.pc_offset;
1573 if (!in_search) return TRUE;
1579 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1585 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1589 /******************************************************************
1590 * SymGetLinePrev64 (DBGHELP.@)
1593 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1595 IMAGEHLP_LINE line32;
1597 line32.SizeOfStruct = sizeof(line32);
1598 copy_line_32_from_64(&line32, Line);
1599 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1600 copy_line_64_from_32(Line, &line32);
1604 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1606 struct line_info* li;
1608 if (line->Key == 0) return FALSE;
1610 while (!li->is_last)
1613 if (!li->is_source_file)
1615 line->LineNumber = li->line_number;
1616 line->Address = li->u.pc_offset;
1620 line->FileName = (char*)source_get(module, li->u.source_file);
1625 /******************************************************************
1626 * SymGetLineNext (DBGHELP.@)
1629 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1631 struct module_pair pair;
1633 TRACE("(%p %p)\n", hProcess, Line);
1635 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1636 pair.pcs = process_find_by_handle(hProcess);
1637 if (!pair.pcs) return FALSE;
1638 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1639 if (!module_get_debug(&pair)) return FALSE;
1641 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1642 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1646 /******************************************************************
1647 * SymGetLineNext64 (DBGHELP.@)
1650 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1652 IMAGEHLP_LINE line32;
1654 line32.SizeOfStruct = sizeof(line32);
1655 copy_line_32_from_64(&line32, Line);
1656 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1657 copy_line_64_from_32(Line, &line32);
1661 /***********************************************************************
1662 * SymFunctionTableAccess (DBGHELP.@)
1664 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1666 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1670 /***********************************************************************
1671 * SymFunctionTableAccess64 (DBGHELP.@)
1673 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1675 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1679 /***********************************************************************
1680 * SymUnDName (DBGHELP.@)
1682 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1684 TRACE("(%p %s %u)\n", sym, UnDecName, UnDecNameLength);
1685 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1686 UNDNAME_COMPLETE) != 0;
1689 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1690 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1692 /***********************************************************************
1693 * UnDecorateSymbolName (DBGHELP.@)
1695 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1696 DWORD UndecoratedLength, DWORD Flags)
1698 /* undocumented from msvcrt */
1699 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1700 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1702 TRACE("(%s, %p, %d, 0x%08x)\n",
1703 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1707 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1708 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1709 if (!p_undname) return 0;
1712 if (!UnDecoratedName) return 0;
1713 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1714 und_alloc, und_free, Flags))
1716 return strlen(UnDecoratedName);
1719 /******************************************************************
1720 * SymMatchString (DBGHELP.@)
1723 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1728 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1730 compile_regex(re, -1, &preg, _case);
1731 ret = match_regexp(&preg, string);
1736 /******************************************************************
1737 * SymSearch (DBGHELP.@)
1739 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1740 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1741 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1742 PVOID UserContext, DWORD Options)
1746 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1747 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1748 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1749 UserContext, Options);
1751 if (Options != SYMSEARCH_GLOBALSONLY)
1753 FIXME("Unsupported searching with options (%x)\n", Options);
1754 SetLastError(ERROR_INVALID_PARAMETER);
1758 se.cb = EnumSymbolsCallback;
1759 se.user = UserContext;
1763 se.sym_info = (PSYMBOL_INFO)se.buffer;
1765 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1768 /******************************************************************
1769 * SymSearchW (DBGHELP.@)
1771 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1772 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1773 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1774 PVOID UserContext, DWORD Options)
1776 struct sym_enumW sew;
1780 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1781 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1782 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1783 UserContext, Options);
1785 sew.ctx = UserContext;
1786 sew.cb = EnumSymbolsCallback;
1787 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1791 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1792 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1793 if (!maskA) return FALSE;
1794 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1796 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1797 sym_enumW, &sew, Options);
1798 HeapFree(GetProcessHeap(), 0, maskA);
1803 /******************************************************************
1804 * SymAddSymbol (DBGHELP.@)
1807 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1808 DWORD64 addr, DWORD size, DWORD flags)
1810 WCHAR nameW[MAX_SYM_NAME];
1812 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1813 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1816 /******************************************************************
1817 * SymAddSymbolW (DBGHELP.@)
1820 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1821 DWORD64 addr, DWORD size, DWORD flags)
1823 struct module_pair pair;
1825 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1827 pair.pcs = process_find_by_handle(hProcess);
1828 if (!pair.pcs) return FALSE;
1829 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1830 if (!module_get_debug(&pair)) return FALSE;
1832 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1836 /******************************************************************
1837 * SymSetScopeFromAddr (DBGHELP.@)
1839 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1841 struct process* pcs;
1843 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1845 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1849 /******************************************************************
1850 * SymEnumLines (DBGHELP.@)
1853 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1854 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1856 struct module_pair pair;
1857 struct hash_table_iter hti;
1858 struct symt_ht* sym;
1860 struct line_info* dli;
1865 if (!cb) return FALSE;
1866 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1868 pair.pcs = process_find_by_handle(hProcess);
1869 if (!pair.pcs) return FALSE;
1870 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1871 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1872 if (!module_get_debug(&pair)) return FALSE;
1873 if (!compile_file_regex(&re, srcfile)) return FALSE;
1875 sci.SizeOfStruct = sizeof(sci);
1878 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1879 while ((ptr = hash_table_iter_up(&hti)))
1883 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1884 if (sym->symt.tag != SymTagFunction) continue;
1886 sci.FileName[0] = '\0';
1887 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1889 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1890 if (dli->is_source_file)
1892 file = source_get(pair.effective, dli->u.source_file);
1893 if (!match_regexp(&re, file)) file = "";
1894 strcpy(sci.FileName, file);
1896 else if (sci.FileName[0])
1899 sci.Obj[0] = '\0'; /* FIXME */
1900 sci.LineNumber = dli->line_number;
1901 sci.Address = dli->u.pc_offset;
1902 if (!cb(&sci, user)) break;