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 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
133 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
159 ret = !regcomp(re, mask, REG_NOSUB);
160 HeapFree(GetProcessHeap(), 0, mask);
163 FIXME("Couldn't compile %s\n", mask);
164 SetLastError(ERROR_INVALID_PARAMETER);
169 static int match_regexp( const regex_t *re, const char *str )
171 return !regexec( re, str, 0, NULL, 0 );
174 #else /* HAVE_REGEX_H */
176 /* if we don't have regexp support, fall back to a simple string comparison */
184 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
186 if (numchar == -1) numchar = strlen( str );
188 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
189 memcpy( re->str, str, numchar );
190 re->str[numchar] = 0;
194 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
196 if (!srcfile || !*srcfile) re->str = NULL;
197 else compile_regex( srcfile, -1, re, FALSE );
201 static int match_regexp( const regex_t *re, const char *str )
203 if (!re->str) return 1;
204 if (re->icase) return !lstrcmpiA( re->str, str );
205 return !strcmp( re->str, str );
208 static void regfree( regex_t *re )
210 HeapFree( GetProcessHeap(), 0, re->str );
213 #endif /* HAVE_REGEX_H */
215 struct symt_compiland* symt_new_compiland(struct module* module,
216 unsigned long address, unsigned src_idx)
218 struct symt_compiland* sym;
220 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
221 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
222 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
224 sym->symt.tag = SymTagCompiland;
225 sym->address = address;
226 sym->source = src_idx;
227 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
232 struct symt_public* symt_new_public(struct module* module,
233 struct symt_compiland* compiland,
235 unsigned long address, unsigned size,
236 BOOL in_code, BOOL is_func)
238 struct symt_public* sym;
241 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
242 debugstr_w(module->module.ModuleName), name, address);
243 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
244 symt_find_nearest(module, address) != NULL)
246 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
248 sym->symt.tag = SymTagPublicSymbol;
249 sym->hash_elt.name = pool_strdup(&module->pool, name);
250 hash_table_add(&module->ht_symbols, &sym->hash_elt);
251 module->sortlist_valid = FALSE;
252 sym->container = compiland ? &compiland->symt : NULL;
253 sym->address = address;
255 sym->in_code = in_code;
256 sym->is_function = is_func;
259 p = vector_add(&compiland->vchildren, &module->pool);
266 struct symt_data* symt_new_global_variable(struct module* module,
267 struct symt_compiland* compiland,
268 const char* name, unsigned is_static,
269 unsigned long addr, unsigned long size,
272 struct symt_data* sym;
276 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
277 debugstr_w(module->module.ModuleName), name, addr, type);
278 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
280 sym->symt.tag = SymTagData;
281 sym->hash_elt.name = pool_strdup(&module->pool, name);
282 hash_table_add(&module->ht_symbols, &sym->hash_elt);
283 module->sortlist_valid = FALSE;
284 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
285 sym->container = compiland ? &compiland->symt : NULL;
287 sym->u.var.offset = addr;
288 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
291 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
292 debugstr_w(module->module.ModuleName), name,
293 wine_dbgstr_longlong(tsz), size);
297 p = vector_add(&compiland->vchildren, &module->pool);
304 struct symt_function* symt_new_function(struct module* module,
305 struct symt_compiland* compiland,
307 unsigned long addr, unsigned long size,
308 struct symt* sig_type)
310 struct symt_function* sym;
313 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
314 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
316 assert(!sig_type || sig_type->tag == SymTagFunctionType);
317 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
319 sym->symt.tag = SymTagFunction;
320 sym->hash_elt.name = pool_strdup(&module->pool, name);
321 hash_table_add(&module->ht_symbols, &sym->hash_elt);
322 module->sortlist_valid = FALSE;
323 sym->container = &compiland->symt;
325 sym->type = sig_type;
327 vector_init(&sym->vlines, sizeof(struct line_info), 64);
328 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
331 p = vector_add(&compiland->vchildren, &module->pool);
338 void symt_add_func_line(struct module* module, struct symt_function* func,
339 unsigned source_idx, int line_num, unsigned long offset)
341 struct line_info* dli;
342 BOOL last_matches = FALSE;
345 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
347 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
348 func, func->hash_elt.name, offset,
349 source_get(module, source_idx), line_num);
351 assert(func->symt.tag == SymTagFunction);
353 for (i=vector_length(&func->vlines)-1; i>=0; i--)
355 dli = vector_at(&func->vlines, i);
356 if (dli->is_source_file)
358 last_matches = (source_idx == dli->u.source_file);
365 /* we shouldn't have line changes on first line of function */
366 dli = vector_add(&func->vlines, &module->pool);
367 dli->is_source_file = 1;
368 dli->is_first = dli->is_last = 0;
369 dli->line_number = 0;
370 dli->u.source_file = source_idx;
372 dli = vector_add(&func->vlines, &module->pool);
373 dli->is_source_file = 0;
374 dli->is_first = dli->is_last = 0;
375 dli->line_number = line_num;
376 dli->u.pc_offset = func->address + offset;
379 /******************************************************************
380 * symt_add_func_local
382 * Adds a new local/parameter to a given function:
383 * In any cases, dt tells whether it's a local variable or a parameter
384 * If regno it's not 0:
385 * - then variable is stored in a register
386 * - otherwise, value is referenced by register + offset
387 * Otherwise, the variable is stored on the stack:
388 * - offset is then the offset from the frame register
390 struct symt_data* symt_add_func_local(struct module* module,
391 struct symt_function* func,
393 const struct location* loc,
394 struct symt_block* block,
395 struct symt* type, const char* name)
397 struct symt_data* locsym;
400 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
401 debugstr_w(module->module.ModuleName), func->hash_elt.name,
405 assert(func->symt.tag == SymTagFunction);
406 assert(dt == DataIsParam || dt == DataIsLocal);
408 locsym = pool_alloc(&module->pool, sizeof(*locsym));
409 locsym->symt.tag = SymTagData;
410 locsym->hash_elt.name = pool_strdup(&module->pool, name);
411 locsym->hash_elt.next = NULL;
413 locsym->container = &block->symt;
415 locsym->u.var = *loc;
417 p = vector_add(&block->vchildren, &module->pool);
419 p = vector_add(&func->vchildren, &module->pool);
425 struct symt_block* symt_open_func_block(struct module* module,
426 struct symt_function* func,
427 struct symt_block* parent_block,
428 unsigned pc, unsigned len)
430 struct symt_block* block;
434 assert(func->symt.tag == SymTagFunction);
436 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
437 block = pool_alloc(&module->pool, sizeof(*block));
438 block->symt.tag = SymTagBlock;
439 block->address = func->address + pc;
441 block->container = parent_block ? &parent_block->symt : &func->symt;
442 vector_init(&block->vchildren, sizeof(struct symt*), 4);
444 p = vector_add(&parent_block->vchildren, &module->pool);
446 p = vector_add(&func->vchildren, &module->pool);
452 struct symt_block* symt_close_func_block(struct module* module,
453 struct symt_function* func,
454 struct symt_block* block, unsigned pc)
457 assert(func->symt.tag == SymTagFunction);
459 if (pc) block->size = func->address + pc - block->address;
460 return (block->container->tag == SymTagBlock) ?
461 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
464 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
465 struct symt_function* func,
466 enum SymTagEnum point,
467 const struct location* loc,
470 struct symt_hierarchy_point*sym;
473 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
475 sym->symt.tag = point;
476 sym->parent = &func->symt;
478 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
479 p = vector_add(&func->vchildren, &module->pool);
485 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
488 struct line_info* dli;
491 /* We aren't adding any more locals or line numbers to this function.
492 * Free any spare memory that we might have allocated.
494 assert(func->symt.tag == SymTagFunction);
496 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
497 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
499 len = vector_length(&func->vlines);
502 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
503 dli = vector_at(&func->vlines, len); dli->is_last = 1;
508 struct symt_thunk* symt_new_thunk(struct module* module,
509 struct symt_compiland* compiland,
510 const char* name, THUNK_ORDINAL ord,
511 unsigned long addr, unsigned long size)
513 struct symt_thunk* sym;
515 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
516 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
518 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
520 sym->symt.tag = SymTagThunk;
521 sym->hash_elt.name = pool_strdup(&module->pool, name);
522 hash_table_add(&module->ht_symbols, &sym->hash_elt);
523 module->sortlist_valid = FALSE;
524 sym->container = &compiland->symt;
531 p = vector_add(&compiland->vchildren, &module->pool);
538 struct symt_data* symt_new_constant(struct module* module,
539 struct symt_compiland* compiland,
540 const char* name, struct symt* type,
543 struct symt_data* sym;
545 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
546 debugstr_w(module->module.ModuleName), name);
548 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
550 sym->symt.tag = SymTagData;
551 sym->hash_elt.name = pool_strdup(&module->pool, name);
552 hash_table_add(&module->ht_symbols, &sym->hash_elt);
553 module->sortlist_valid = FALSE;
554 sym->kind = DataIsConstant;
555 sym->container = compiland ? &compiland->symt : NULL;
561 p = vector_add(&compiland->vchildren, &module->pool);
568 struct symt_hierarchy_point* symt_new_label(struct module* module,
569 struct symt_compiland* compiland,
570 const char* name, unsigned long address)
572 struct symt_hierarchy_point* sym;
574 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
575 debugstr_w(module->module.ModuleName), name);
577 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
579 sym->symt.tag = SymTagLabel;
580 sym->hash_elt.name = pool_strdup(&module->pool, name);
581 hash_table_add(&module->ht_symbols, &sym->hash_elt);
582 module->sortlist_valid = FALSE;
583 sym->loc.kind = loc_absolute;
584 sym->loc.offset = address;
585 sym->parent = compiland ? &compiland->symt : NULL;
589 p = vector_add(&compiland->vchildren, &module->pool);
596 /* expect sym_info->MaxNameLen to be set before being called */
597 static void symt_fill_sym_info(const struct module_pair* pair,
598 const struct symt_function* func,
599 const struct symt* sym, SYMBOL_INFO* sym_info)
604 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
605 sym_info->TypeIndex = 0;
606 sym_info->info = (DWORD)sym;
607 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
608 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
609 (!sym_info->TypeIndex ||
610 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
612 sym_info->Size = (DWORD)size;
613 sym_info->ModBase = pair->requested->module.BaseOfImage;
621 const struct symt_data* data = (const struct symt_data*)sym;
625 sym_info->Flags |= SYMFLAG_PARAMETER;
629 struct location loc = data->u.var;
631 if (loc.kind >= loc_user)
632 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
637 /* for now we report error cases as a negative register number */
638 sym_info->Flags |= SYMFLAG_LOCAL;
641 sym_info->Flags |= SYMFLAG_REGISTER;
642 sym_info->Register = loc.reg;
643 sym_info->Address = 0;
646 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
647 /* FIXME: it's i386 dependent !!! */
648 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
649 sym_info->Address = loc.offset;
652 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
658 case DataIsFileStatic:
659 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
660 sym_info->Register = 0;
663 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
664 switch (data->u.value.n1.n2.vt)
666 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
667 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
668 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
669 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
670 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
671 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
672 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
674 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
680 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
684 case SymTagPublicSymbol:
685 sym_info->Flags |= SYMFLAG_EXPORT;
686 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
689 sym_info->Flags |= SYMFLAG_FUNCTION;
690 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
693 sym_info->Flags |= SYMFLAG_THUNK;
694 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
697 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
698 sym_info->Register = 0;
701 sym_info->Scope = 0; /* FIXME */
702 sym_info->Tag = sym->tag;
703 name = symt_get_name(sym);
704 if (sym_info->MaxNameLen)
706 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
707 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
708 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
710 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
711 memcpy(sym_info->Name, name, sym_info->NameLen);
712 sym_info->Name[sym_info->NameLen] = '\0';
715 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
716 sym, sym_info->Name, sym_info->Size,
717 wine_dbgstr_longlong(sym_info->Address));
722 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
724 SYMBOL_INFO* sym_info;
728 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
731 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
732 const struct symt_function* func, const struct symt* sym)
734 symt_fill_sym_info(pair, func, sym, se->sym_info);
735 if (se->index && se->sym_info->info != se->index) return FALSE;
736 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
737 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
738 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
741 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
742 const struct sym_enum* se)
745 struct symt_ht* sym = NULL;
746 struct hash_table_iter hti;
748 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
749 while ((ptr = hash_table_iter_up(&hti)))
751 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
752 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
754 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
755 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
756 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
762 /***********************************************************************
765 * Rebuild sorted list of symbols for a module.
767 static BOOL resort_symbols(struct module* module)
771 struct hash_table_iter hti;
774 if (!(module->module.NumSyms = module->ht_symbols.num_elts))
777 if (module->addr_sorttab)
778 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
779 module->addr_sorttab,
780 module->module.NumSyms * sizeof(struct symt_ht*));
782 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
783 module->module.NumSyms * sizeof(struct symt_ht*));
784 if (!module->addr_sorttab) return FALSE;
786 module->num_sorttab = 0;
787 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
788 while ((ptr = hash_table_iter_up(&hti)))
790 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
792 /* Don't store in sorttab symbol without address, they are of
793 * no use here (e.g. constant values)
794 * As the number of those symbols is very couple (a couple per module)
795 * we don't bother for the unused spots at the end of addr_sorttab
797 if (symt_get_info(&sym->symt, TI_GET_ADDRESS, &addr))
798 module->addr_sorttab[module->num_sorttab++] = sym;
800 qsort(module->addr_sorttab, module->num_sorttab, sizeof(struct symt_ht*), symt_cmp_addr);
801 return module->sortlist_valid = TRUE;
804 static void symt_get_length(struct symt* symt, ULONG64* size)
808 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
811 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
812 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
813 *size = 0x1000; /* arbitrary value */
816 /* assume addr is in module */
817 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
820 ULONG64 ref_addr, ref_size;
822 if (!module->sortlist_valid || !module->addr_sorttab)
824 if (!resort_symbols(module)) return NULL;
828 * Binary search to find closest symbol.
831 high = module->num_sorttab;
833 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
834 if (addr < ref_addr) return NULL;
837 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
838 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
839 if (addr >= ref_addr + ref_size) return NULL;
842 while (high > low + 1)
844 mid = (high + low) / 2;
845 if (cmp_sorttab_addr(module, mid, addr) < 0)
850 if (low != high && high != module->num_sorttab &&
851 cmp_sorttab_addr(module, high, addr) <= 0)
854 /* If found symbol is a public symbol, check if there are any other entries that
855 * might also have the same address, but would get better information
857 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
859 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
861 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
862 !cmp_sorttab_addr(module, low - 1, ref_addr))
864 else if (low < module->num_sorttab - 1 &&
865 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
866 !cmp_sorttab_addr(module, low + 1, ref_addr))
869 /* finally check that we fit into the found symbol */
870 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
871 if (addr < ref_addr) return NULL;
872 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
873 if (addr >= ref_addr + ref_size) return NULL;
875 return module->addr_sorttab[low];
878 static BOOL symt_enum_locals_helper(struct module_pair* pair,
879 regex_t* preg, const struct sym_enum* se,
880 struct symt_function* func, const struct vector* v)
882 struct symt* lsym = NULL;
883 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
886 for (i=0; i<vector_length(v); i++)
888 lsym = *(struct symt**)vector_at(v, i);
893 struct symt_block* block = (struct symt_block*)lsym;
894 if (pc < block->address || block->address + block->size <= pc)
896 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
901 if (match_regexp(preg, symt_get_name(lsym)))
903 if (send_symbol(se, pair, func, lsym)) return FALSE;
907 case SymTagFuncDebugStart:
908 case SymTagFuncDebugEnd:
912 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
919 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
920 const struct sym_enum* se)
922 struct module_pair pair;
924 DWORD pc = pcs->ctx_frame.InstructionOffset;
926 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
927 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
930 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
931 if (!module_get_debug(&pair)) return FALSE;
932 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
934 if (sym->symt.tag == SymTagFunction)
939 compile_regex(mask ? mask : "*", -1, &preg,
940 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
941 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
942 &((struct symt_function*)sym)->vchildren);
947 return send_symbol(se, &pair, NULL, &sym->symt);
950 /******************************************************************
953 * Helper for transforming an ANSI symbol info into a UNICODE one.
954 * Assume that MaxNameLen is the same for both version (A & W).
956 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
958 siw->SizeOfStruct = si->SizeOfStruct;
959 siw->TypeIndex = si->TypeIndex;
960 siw->Reserved[0] = si->Reserved[0];
961 siw->Reserved[1] = si->Reserved[1];
962 siw->Index = si->info; /* FIXME: see dbghelp.h */
963 siw->Size = si->Size;
964 siw->ModBase = si->ModBase;
965 siw->Flags = si->Flags;
966 siw->Value = si->Value;
967 siw->Address = si->Address;
968 siw->Register = si->Register;
969 siw->Scope = si->Scope;
971 siw->NameLen = si->NameLen;
972 siw->MaxNameLen = si->MaxNameLen;
973 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
976 /******************************************************************
979 * Core routine for most of the enumeration of symbols
981 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
982 const struct sym_enum* se)
984 struct module_pair pair;
986 regex_t mod_regex, sym_regex;
988 pair.pcs = process_find_by_handle(hProcess);
991 /* do local variables ? */
992 if (!Mask || !(bang = strchr(Mask, '!')))
993 return symt_enum_locals(pair.pcs, Mask, se);
995 if (bang == Mask) return FALSE;
997 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
998 compile_regex(bang + 1, -1, &sym_regex,
999 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1001 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1003 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1005 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1006 symt_enum_module(&pair, &sym_regex, se))
1010 /* not found in PE modules, retry on the ELF ones
1012 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
1014 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1016 if (pair.requested->type == DMT_ELF &&
1017 !module_get_containee(pair.pcs, pair.requested) &&
1018 module_get_debug(&pair))
1020 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1021 symt_enum_module(&pair, &sym_regex, se))
1026 regfree(&mod_regex);
1027 regfree(&sym_regex);
1030 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1031 if (!module_get_debug(&pair))
1034 /* we always ignore module name from Mask when BaseOfDll is defined */
1035 if (Mask && (bang = strchr(Mask, '!')))
1037 if (bang == Mask) return FALSE;
1041 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1042 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1043 symt_enum_module(&pair, &sym_regex, se);
1044 regfree(&sym_regex);
1049 /******************************************************************
1050 * SymEnumSymbols (DBGHELP.@)
1052 * cases BaseOfDll = 0
1053 * !foo fails always (despite what MSDN states)
1054 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1055 * no ! in Mask, lookup in local Context
1056 * cases BaseOfDll != 0
1057 * !foo fails always (despite what MSDN states)
1058 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1060 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1061 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1066 TRACE("(%p %s %s %p %p)\n",
1067 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1068 EnumSymbolsCallback, UserContext);
1070 se.cb = EnumSymbolsCallback;
1071 se.user = UserContext;
1075 se.sym_info = (PSYMBOL_INFO)se.buffer;
1077 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1082 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1084 PSYMBOL_INFOW sym_info;
1085 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1089 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1091 struct sym_enumW* sew = ctx;
1093 copy_symbolW(sew->sym_info, si);
1095 return (sew->cb)(sew->sym_info, size, sew->ctx);
1098 /******************************************************************
1099 * SymEnumSymbolsW (DBGHELP.@)
1102 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1103 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1106 struct sym_enumW sew;
1110 sew.ctx = UserContext;
1111 sew.cb = EnumSymbolsCallback;
1112 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1116 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1117 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1118 if (!maskA) return FALSE;
1119 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1121 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1122 HeapFree(GetProcessHeap(), 0, maskA);
1127 struct sym_enumerate
1130 PSYM_ENUMSYMBOLS_CALLBACK cb;
1133 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1135 struct sym_enumerate* se = ctx;
1136 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1139 /***********************************************************************
1140 * SymEnumerateSymbols (DBGHELP.@)
1142 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1143 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1146 struct sym_enumerate se;
1148 se.ctx = UserContext;
1149 se.cb = EnumSymbolsCallback;
1151 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1154 struct sym_enumerate64
1157 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1160 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1162 struct sym_enumerate64* se = ctx;
1163 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1166 /***********************************************************************
1167 * SymEnumerateSymbols64 (DBGHELP.@)
1169 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1170 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1173 struct sym_enumerate64 se;
1175 se.ctx = UserContext;
1176 se.cb = EnumSymbolsCallback;
1178 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1181 /******************************************************************
1182 * SymFromAddr (DBGHELP.@)
1185 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1186 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1188 struct module_pair pair;
1189 struct symt_ht* sym;
1191 pair.pcs = process_find_by_handle(hProcess);
1192 if (!pair.pcs) return FALSE;
1193 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1194 if (!module_get_debug(&pair)) return FALSE;
1195 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1197 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1198 *Displacement = Address - Symbol->Address;
1202 /******************************************************************
1203 * SymFromAddrW (DBGHELP.@)
1206 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1207 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1213 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1214 si = HeapAlloc(GetProcessHeap(), 0, len);
1215 if (!si) return FALSE;
1217 si->SizeOfStruct = sizeof(*si);
1218 si->MaxNameLen = Symbol->MaxNameLen;
1219 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1221 copy_symbolW(Symbol, si);
1223 HeapFree(GetProcessHeap(), 0, si);
1227 /******************************************************************
1228 * SymGetSymFromAddr (DBGHELP.@)
1231 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1232 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1234 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1235 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1237 DWORD64 Displacement64;
1239 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1240 si->SizeOfStruct = sizeof(*si);
1241 si->MaxNameLen = MAX_SYM_NAME;
1242 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1246 *Displacement = Displacement64;
1247 Symbol->Address = si->Address;
1248 Symbol->Size = si->Size;
1249 Symbol->Flags = si->Flags;
1250 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1251 lstrcpynA(Symbol->Name, si->Name, len);
1255 /******************************************************************
1256 * SymGetSymFromAddr64 (DBGHELP.@)
1259 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1260 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1262 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1263 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1265 DWORD64 Displacement64;
1267 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1268 si->SizeOfStruct = sizeof(*si);
1269 si->MaxNameLen = MAX_SYM_NAME;
1270 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1274 *Displacement = Displacement64;
1275 Symbol->Address = si->Address;
1276 Symbol->Size = si->Size;
1277 Symbol->Flags = si->Flags;
1278 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1279 lstrcpynA(Symbol->Name, si->Name, len);
1283 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1284 SYMBOL_INFO* symbol)
1286 struct hash_table_iter hti;
1288 struct symt_ht* sym = NULL;
1289 struct module_pair pair;
1292 if (!(pair.requested = module)) return FALSE;
1293 if (!module_get_debug(&pair)) return FALSE;
1295 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1296 while ((ptr = hash_table_iter_up(&hti)))
1298 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1300 if (!strcmp(sym->hash_elt.name, name))
1302 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1309 /******************************************************************
1310 * SymFromName (DBGHELP.@)
1313 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1315 struct process* pcs = process_find_by_handle(hProcess);
1316 struct module* module;
1319 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1320 if (!pcs) return FALSE;
1321 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1322 name = strchr(Name, '!');
1326 assert(name - Name < sizeof(tmp));
1327 memcpy(tmp, Name, name - Name);
1328 tmp[name - Name] = '\0';
1329 module = module_find_by_nameA(pcs, tmp);
1330 return find_name(pcs, module, name + 1, Symbol);
1332 for (module = pcs->lmodules; module; module = module->next)
1334 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1337 /* not found in PE modules, retry on the ELF ones
1339 if (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES)
1341 for (module = pcs->lmodules; module; module = module->next)
1343 if (module->type == DMT_ELF && !module_get_containee(pcs, module) &&
1344 find_name(pcs, module, Name, Symbol))
1351 /***********************************************************************
1352 * SymGetSymFromName (DBGHELP.@)
1354 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1356 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1357 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1360 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1361 si->SizeOfStruct = sizeof(*si);
1362 si->MaxNameLen = MAX_SYM_NAME;
1363 if (!SymFromName(hProcess, Name, si)) return FALSE;
1365 Symbol->Address = si->Address;
1366 Symbol->Size = si->Size;
1367 Symbol->Flags = si->Flags;
1368 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1369 lstrcpynA(Symbol->Name, si->Name, len);
1373 /******************************************************************
1374 * sym_fill_func_line_info
1376 * fills information about a file
1378 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1379 DWORD addr, IMAGEHLP_LINE* line)
1381 struct line_info* dli = NULL;
1385 assert(func->symt.tag == SymTagFunction);
1387 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1389 dli = vector_at(&func->vlines, i);
1390 if (!dli->is_source_file)
1392 if (found || dli->u.pc_offset > addr) continue;
1393 line->LineNumber = dli->line_number;
1394 line->Address = dli->u.pc_offset;
1401 line->FileName = (char*)source_get(module, dli->u.source_file);
1408 /***********************************************************************
1409 * SymGetSymNext (DBGHELP.@)
1411 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1414 * get module from Symbol.Address
1415 * get index in module.addr_sorttab of Symbol.Address
1417 * if out of module bounds, move to next module in process address space
1419 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1420 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1424 /***********************************************************************
1425 * SymGetSymPrev (DBGHELP.@)
1428 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1430 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1431 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1435 /******************************************************************
1436 * SymGetLineFromAddr (DBGHELP.@)
1439 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1440 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1442 struct module_pair pair;
1443 struct symt_ht* symt;
1445 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1447 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1449 pair.pcs = process_find_by_handle(hProcess);
1450 if (!pair.pcs) return FALSE;
1451 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1452 if (!module_get_debug(&pair)) return FALSE;
1453 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1455 if (symt->symt.tag != SymTagFunction) return FALSE;
1456 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1457 dwAddr, Line)) return FALSE;
1458 *pdwDisplacement = dwAddr - Line->Address;
1462 /******************************************************************
1463 * copy_line_64_from_32 (internal)
1466 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1469 l64->Key = l32->Key;
1470 l64->LineNumber = l32->LineNumber;
1471 l64->FileName = l32->FileName;
1472 l64->Address = l32->Address;
1475 /******************************************************************
1476 * copy_line_W64_from_32 (internal)
1479 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1483 l64->Key = l32->Key;
1484 l64->LineNumber = l32->LineNumber;
1485 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1486 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1487 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1488 l64->Address = l32->Address;
1491 /******************************************************************
1492 * copy_line_32_from_64 (internal)
1495 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1498 l32->Key = l64->Key;
1499 l32->LineNumber = l64->LineNumber;
1500 l32->FileName = l64->FileName;
1501 l32->Address = l64->Address;
1504 /******************************************************************
1505 * SymGetLineFromAddr64 (DBGHELP.@)
1508 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1509 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1511 IMAGEHLP_LINE line32;
1513 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1514 if (!validate_addr64(dwAddr)) return FALSE;
1515 line32.SizeOfStruct = sizeof(line32);
1516 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1518 copy_line_64_from_32(Line, &line32);
1522 /******************************************************************
1523 * SymGetLineFromAddrW64 (DBGHELP.@)
1526 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1527 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1529 struct process* pcs = process_find_by_handle(hProcess);
1530 IMAGEHLP_LINE line32;
1532 if (!pcs) return FALSE;
1533 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1534 if (!validate_addr64(dwAddr)) return FALSE;
1535 line32.SizeOfStruct = sizeof(line32);
1536 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1538 copy_line_W64_from_32(pcs, Line, &line32);
1542 /******************************************************************
1543 * SymGetLinePrev (DBGHELP.@)
1546 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1548 struct module_pair pair;
1549 struct line_info* li;
1550 BOOL in_search = FALSE;
1552 TRACE("(%p %p)\n", hProcess, Line);
1554 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1556 pair.pcs = process_find_by_handle(hProcess);
1557 if (!pair.pcs) return FALSE;
1558 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1559 if (!module_get_debug(&pair)) return FALSE;
1561 if (Line->Key == 0) return FALSE;
1563 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1564 * element we have to go back until we find the prev one to get the real
1565 * source file name for the DLIT_OFFSET element just before
1566 * the first DLIT_SOURCEFILE
1568 while (!li->is_first)
1571 if (!li->is_source_file)
1573 Line->LineNumber = li->line_number;
1574 Line->Address = li->u.pc_offset;
1576 if (!in_search) return TRUE;
1582 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1588 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1592 /******************************************************************
1593 * SymGetLinePrev64 (DBGHELP.@)
1596 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1598 IMAGEHLP_LINE line32;
1600 line32.SizeOfStruct = sizeof(line32);
1601 copy_line_32_from_64(&line32, Line);
1602 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1603 copy_line_64_from_32(Line, &line32);
1607 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1609 struct line_info* li;
1611 if (line->Key == 0) return FALSE;
1613 while (!li->is_last)
1616 if (!li->is_source_file)
1618 line->LineNumber = li->line_number;
1619 line->Address = li->u.pc_offset;
1623 line->FileName = (char*)source_get(module, li->u.source_file);
1628 /******************************************************************
1629 * SymGetLineNext (DBGHELP.@)
1632 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1634 struct module_pair pair;
1636 TRACE("(%p %p)\n", hProcess, Line);
1638 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1639 pair.pcs = process_find_by_handle(hProcess);
1640 if (!pair.pcs) return FALSE;
1641 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1642 if (!module_get_debug(&pair)) return FALSE;
1644 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1645 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1649 /******************************************************************
1650 * SymGetLineNext64 (DBGHELP.@)
1653 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1655 IMAGEHLP_LINE line32;
1657 line32.SizeOfStruct = sizeof(line32);
1658 copy_line_32_from_64(&line32, Line);
1659 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1660 copy_line_64_from_32(Line, &line32);
1664 /***********************************************************************
1665 * SymFunctionTableAccess (DBGHELP.@)
1667 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1669 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1673 /***********************************************************************
1674 * SymFunctionTableAccess64 (DBGHELP.@)
1676 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1678 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1682 /***********************************************************************
1683 * SymUnDName (DBGHELP.@)
1685 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1687 TRACE("(%p %s %u)\n", sym, UnDecName, UnDecNameLength);
1688 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1689 UNDNAME_COMPLETE) != 0;
1692 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1693 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1695 /***********************************************************************
1696 * UnDecorateSymbolName (DBGHELP.@)
1698 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1699 DWORD UndecoratedLength, DWORD Flags)
1701 /* undocumented from msvcrt */
1702 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1703 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1705 TRACE("(%s, %p, %d, 0x%08x)\n",
1706 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1710 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1711 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1712 if (!p_undname) return 0;
1715 if (!UnDecoratedName) return 0;
1716 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1717 und_alloc, und_free, Flags))
1719 return strlen(UnDecoratedName);
1722 /******************************************************************
1723 * SymMatchString (DBGHELP.@)
1726 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1731 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1733 compile_regex(re, -1, &preg, _case);
1734 ret = match_regexp(&preg, string);
1739 /******************************************************************
1740 * SymSearch (DBGHELP.@)
1742 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1743 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1744 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1745 PVOID UserContext, DWORD Options)
1749 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1750 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1751 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1752 UserContext, Options);
1754 if (Options != SYMSEARCH_GLOBALSONLY)
1756 FIXME("Unsupported searching with options (%x)\n", Options);
1757 SetLastError(ERROR_INVALID_PARAMETER);
1761 se.cb = EnumSymbolsCallback;
1762 se.user = UserContext;
1766 se.sym_info = (PSYMBOL_INFO)se.buffer;
1768 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1771 /******************************************************************
1772 * SymSearchW (DBGHELP.@)
1774 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1775 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1776 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1777 PVOID UserContext, DWORD Options)
1779 struct sym_enumW sew;
1783 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1784 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1785 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1786 UserContext, Options);
1788 sew.ctx = UserContext;
1789 sew.cb = EnumSymbolsCallback;
1790 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1794 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1795 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1796 if (!maskA) return FALSE;
1797 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1799 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1800 sym_enumW, &sew, Options);
1801 HeapFree(GetProcessHeap(), 0, maskA);
1806 /******************************************************************
1807 * SymAddSymbol (DBGHELP.@)
1810 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1811 DWORD64 addr, DWORD size, DWORD flags)
1813 WCHAR nameW[MAX_SYM_NAME];
1815 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1816 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1819 /******************************************************************
1820 * SymAddSymbolW (DBGHELP.@)
1823 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1824 DWORD64 addr, DWORD size, DWORD flags)
1826 struct module_pair pair;
1828 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1830 pair.pcs = process_find_by_handle(hProcess);
1831 if (!pair.pcs) return FALSE;
1832 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1833 if (!module_get_debug(&pair)) return FALSE;
1835 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1839 /******************************************************************
1840 * SymSetScopeFromAddr (DBGHELP.@)
1842 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1844 struct process* pcs;
1846 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1848 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1852 /******************************************************************
1853 * SymEnumLines (DBGHELP.@)
1856 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1857 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1859 struct module_pair pair;
1860 struct hash_table_iter hti;
1861 struct symt_ht* sym;
1863 struct line_info* dli;
1868 if (!cb) return FALSE;
1869 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1871 pair.pcs = process_find_by_handle(hProcess);
1872 if (!pair.pcs) return FALSE;
1873 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1874 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1875 if (!module_get_debug(&pair)) return FALSE;
1876 if (!compile_file_regex(&re, srcfile)) return FALSE;
1878 sci.SizeOfStruct = sizeof(sci);
1881 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1882 while ((ptr = hash_table_iter_up(&hti)))
1886 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1887 if (sym->symt.tag != SymTagFunction) continue;
1889 sci.FileName[0] = '\0';
1890 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1892 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1893 if (dli->is_source_file)
1895 file = source_get(pair.effective, dli->u.source_file);
1896 if (!match_regexp(&re, file)) file = "";
1897 strcpy(sci.FileName, file);
1899 else if (sci.FileName[0])
1902 sci.Obj[0] = '\0'; /* FIXME */
1903 sci.LineNumber = dli->line_number;
1904 sci.Address = dli->u.pc_offset;
1905 if (!cb(&sci, user)) break;