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>
34 #include "wine/debug.h"
35 #include "dbghelp_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
39 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
41 static WCHAR starW[] = {'*','\0'};
43 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
45 if (a1 > a2) return 1;
46 if (a1 < a2) return -1;
50 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
53 symt_get_address(&module->addr_sorttab[idx]->symt, &ref);
54 return cmp_addr(ref, addr);
57 int symt_cmp_addr(const void* p1, const void* p2)
59 const struct symt* sym1 = *(const struct symt* const *)p1;
60 const struct symt* sym2 = *(const struct symt* const *)p2;
63 symt_get_address(sym1, &a1);
64 symt_get_address(sym2, &a2);
65 return cmp_addr(a1, a2);
68 DWORD symt_ptr2index(struct module* module, const struct symt* sym)
71 const struct symt** c;
72 int len = vector_length(&module->vsymt), i;
74 /* FIXME: this is inefficient */
75 for (i = 0; i < len; i++)
77 if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
81 c = vector_add(&module->vsymt, &module->pool);
89 struct symt* symt_index2ptr(struct module* module, DWORD id)
92 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
93 return *(struct symt**)vector_at(&module->vsymt, id);
95 return (struct symt*)id;
99 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
101 struct symt_ht** new;
104 if (sz <= module->sorttab_size) return TRUE;
105 if (module->addr_sorttab)
107 size = module->sorttab_size * 2;
108 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
109 size * sizeof(struct symt_ht*));
114 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
116 if (!new) return FALSE;
117 module->sorttab_size = size;
118 module->addr_sorttab = new;
122 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
126 hash_table_add(&module->ht_symbols, &ht->hash_elt);
127 /* Don't store in sorttab a symbol without address, they are of
128 * no use here (e.g. constant values)
130 if (symt_get_address(&ht->symt, &addr) &&
131 symt_grow_sorttab(module, module->num_symbols + 1))
133 module->addr_sorttab[module->num_symbols++] = ht;
134 module->sortlist_valid = FALSE;
138 static WCHAR* file_regex(const char* srcfile)
143 if (!srcfile || !*srcfile)
145 if (!(p = mask = HeapAlloc(GetProcessHeap(), 0, 3 * sizeof(WCHAR)))) return NULL;
151 DWORD sz = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
154 /* FIXME: we use here the largest conversion for every char... could be optimized */
155 p = mask = HeapAlloc(GetProcessHeap(), 0, (5 * strlen(srcfile) + 1 + sz) * sizeof(WCHAR));
156 if (!mask) return NULL;
157 srcfileW = mask + 5 * strlen(srcfile) + 1;
158 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, srcfileW, sz);
186 struct symt_compiland* symt_new_compiland(struct module* module,
187 unsigned long address, unsigned src_idx)
189 struct symt_compiland* sym;
191 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
192 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
193 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
195 sym->symt.tag = SymTagCompiland;
196 sym->address = address;
197 sym->source = src_idx;
198 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
203 struct symt_public* symt_new_public(struct module* module,
204 struct symt_compiland* compiland,
206 unsigned long address, unsigned size)
208 struct symt_public* sym;
211 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
212 debugstr_w(module->module.ModuleName), name, address);
213 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
214 symt_find_nearest(module, address) != NULL)
216 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
218 sym->symt.tag = SymTagPublicSymbol;
219 sym->hash_elt.name = pool_strdup(&module->pool, name);
220 sym->container = compiland ? &compiland->symt : NULL;
221 sym->address = address;
223 symt_add_module_ht(module, (struct symt_ht*)sym);
226 p = vector_add(&compiland->vchildren, &module->pool);
233 struct symt_data* symt_new_global_variable(struct module* module,
234 struct symt_compiland* compiland,
235 const char* name, unsigned is_static,
236 struct location loc, unsigned long size,
239 struct symt_data* sym;
243 TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
244 debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
245 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
247 sym->symt.tag = SymTagData;
248 sym->hash_elt.name = pool_strdup(&module->pool, name);
249 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
250 sym->container = compiland ? &compiland->symt : NULL;
253 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
256 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
257 debugstr_w(module->module.ModuleName), name,
258 wine_dbgstr_longlong(tsz), size);
260 symt_add_module_ht(module, (struct symt_ht*)sym);
263 p = vector_add(&compiland->vchildren, &module->pool);
270 struct symt_function* symt_new_function(struct module* module,
271 struct symt_compiland* compiland,
273 unsigned long addr, unsigned long size,
274 struct symt* sig_type)
276 struct symt_function* sym;
279 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
280 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
282 assert(!sig_type || sig_type->tag == SymTagFunctionType);
283 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
285 sym->symt.tag = SymTagFunction;
286 sym->hash_elt.name = pool_strdup(&module->pool, name);
287 sym->container = &compiland->symt;
289 sym->type = sig_type;
291 vector_init(&sym->vlines, sizeof(struct line_info), 64);
292 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
293 symt_add_module_ht(module, (struct symt_ht*)sym);
296 p = vector_add(&compiland->vchildren, &module->pool);
303 void symt_add_func_line(struct module* module, struct symt_function* func,
304 unsigned source_idx, int line_num, unsigned long offset)
306 struct line_info* dli;
307 BOOL last_matches = FALSE;
310 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
312 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
313 func, func->hash_elt.name, offset,
314 source_get(module, source_idx), line_num);
316 assert(func->symt.tag == SymTagFunction);
318 for (i=vector_length(&func->vlines)-1; i>=0; i--)
320 dli = vector_at(&func->vlines, i);
321 if (dli->is_source_file)
323 last_matches = (source_idx == dli->u.source_file);
330 /* we shouldn't have line changes on first line of function */
331 dli = vector_add(&func->vlines, &module->pool);
332 dli->is_source_file = 1;
333 dli->is_first = dli->is_last = 0;
334 dli->line_number = 0;
335 dli->u.source_file = source_idx;
337 dli = vector_add(&func->vlines, &module->pool);
338 dli->is_source_file = 0;
339 dli->is_first = dli->is_last = 0;
340 dli->line_number = line_num;
341 dli->u.pc_offset = func->address + offset;
344 /******************************************************************
345 * symt_add_func_local
347 * Adds a new local/parameter to a given function:
348 * In any cases, dt tells whether it's a local variable or a parameter
349 * If regno it's not 0:
350 * - then variable is stored in a register
351 * - otherwise, value is referenced by register + offset
352 * Otherwise, the variable is stored on the stack:
353 * - offset is then the offset from the frame register
355 struct symt_data* symt_add_func_local(struct module* module,
356 struct symt_function* func,
358 const struct location* loc,
359 struct symt_block* block,
360 struct symt* type, const char* name)
362 struct symt_data* locsym;
365 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
366 debugstr_w(module->module.ModuleName), func->hash_elt.name,
370 assert(func->symt.tag == SymTagFunction);
371 assert(dt == DataIsParam || dt == DataIsLocal);
373 locsym = pool_alloc(&module->pool, sizeof(*locsym));
374 locsym->symt.tag = SymTagData;
375 locsym->hash_elt.name = pool_strdup(&module->pool, name);
376 locsym->hash_elt.next = NULL;
378 locsym->container = block ? &block->symt : &func->symt;
380 locsym->u.var = *loc;
382 p = vector_add(&block->vchildren, &module->pool);
384 p = vector_add(&func->vchildren, &module->pool);
390 struct symt_block* symt_open_func_block(struct module* module,
391 struct symt_function* func,
392 struct symt_block* parent_block,
393 unsigned pc, unsigned len)
395 struct symt_block* block;
399 assert(func->symt.tag == SymTagFunction);
401 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
402 block = pool_alloc(&module->pool, sizeof(*block));
403 block->symt.tag = SymTagBlock;
404 block->address = func->address + pc;
406 block->container = parent_block ? &parent_block->symt : &func->symt;
407 vector_init(&block->vchildren, sizeof(struct symt*), 4);
409 p = vector_add(&parent_block->vchildren, &module->pool);
411 p = vector_add(&func->vchildren, &module->pool);
417 struct symt_block* symt_close_func_block(struct module* module,
418 const struct symt_function* func,
419 struct symt_block* block, unsigned pc)
422 assert(func->symt.tag == SymTagFunction);
424 if (pc) block->size = func->address + pc - block->address;
425 return (block->container->tag == SymTagBlock) ?
426 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
429 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
430 struct symt_function* func,
431 enum SymTagEnum point,
432 const struct location* loc,
435 struct symt_hierarchy_point*sym;
438 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
440 sym->symt.tag = point;
441 sym->parent = &func->symt;
443 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
444 p = vector_add(&func->vchildren, &module->pool);
450 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
453 struct line_info* dli;
456 /* We aren't adding any more locals or line numbers to this function.
457 * Free any spare memory that we might have allocated.
459 assert(func->symt.tag == SymTagFunction);
461 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
462 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
464 len = vector_length(&func->vlines);
467 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
468 dli = vector_at(&func->vlines, len); dli->is_last = 1;
473 struct symt_thunk* symt_new_thunk(struct module* module,
474 struct symt_compiland* compiland,
475 const char* name, THUNK_ORDINAL ord,
476 unsigned long addr, unsigned long size)
478 struct symt_thunk* sym;
480 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
481 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
483 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
485 sym->symt.tag = SymTagThunk;
486 sym->hash_elt.name = pool_strdup(&module->pool, name);
487 sym->container = &compiland->symt;
491 symt_add_module_ht(module, (struct symt_ht*)sym);
495 p = vector_add(&compiland->vchildren, &module->pool);
502 struct symt_data* symt_new_constant(struct module* module,
503 struct symt_compiland* compiland,
504 const char* name, struct symt* type,
507 struct symt_data* sym;
509 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
510 debugstr_w(module->module.ModuleName), name);
512 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
514 sym->symt.tag = SymTagData;
515 sym->hash_elt.name = pool_strdup(&module->pool, name);
516 sym->kind = DataIsConstant;
517 sym->container = compiland ? &compiland->symt : NULL;
520 symt_add_module_ht(module, (struct symt_ht*)sym);
524 p = vector_add(&compiland->vchildren, &module->pool);
531 struct symt_hierarchy_point* symt_new_label(struct module* module,
532 struct symt_compiland* compiland,
533 const char* name, unsigned long address)
535 struct symt_hierarchy_point* sym;
537 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
538 debugstr_w(module->module.ModuleName), name);
540 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
542 sym->symt.tag = SymTagLabel;
543 sym->hash_elt.name = pool_strdup(&module->pool, name);
544 sym->loc.kind = loc_absolute;
545 sym->loc.offset = address;
546 sym->parent = compiland ? &compiland->symt : NULL;
547 symt_add_module_ht(module, (struct symt_ht*)sym);
551 p = vector_add(&compiland->vchildren, &module->pool);
558 /* expect sym_info->MaxNameLen to be set before being called */
559 static void symt_fill_sym_info(struct module_pair* pair,
560 const struct symt_function* func,
561 const struct symt* sym, SYMBOL_INFO* sym_info)
566 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
567 sym_info->TypeIndex = 0;
568 sym_info->info = symt_ptr2index(pair->effective, sym);
569 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
570 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
571 (!sym_info->TypeIndex ||
572 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
573 TI_GET_LENGTH, &size)))
575 sym_info->Size = (DWORD)size;
576 sym_info->ModBase = pair->requested->module.BaseOfImage;
584 const struct symt_data* data = (const struct symt_data*)sym;
588 sym_info->Flags |= SYMFLAG_PARAMETER;
591 sym_info->Flags |= SYMFLAG_LOCAL;
593 struct location loc = data->u.var;
595 if (loc.kind >= loc_user)
598 struct module_format* modfmt;
600 for (i = 0; i < DFI_LAST; i++)
602 modfmt = pair->effective->format_info[i];
603 if (modfmt && modfmt->loc_compute)
605 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
613 /* for now we report error cases as a negative register number */
616 sym_info->Flags |= SYMFLAG_REGISTER;
617 sym_info->Register = loc.reg;
618 sym_info->Address = 0;
621 sym_info->Flags |= SYMFLAG_REGREL;
622 sym_info->Register = loc.reg;
623 if (loc.reg == CV_REG_NONE || (int)loc.reg < 0 /* error */)
624 FIXME("suspicious register value %x\n", loc.reg);
625 sym_info->Address = loc.offset;
628 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
629 sym_info->Value = loc.offset;
632 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
638 case DataIsFileStatic:
639 switch (data->u.var.kind)
642 sym_info->Flags |= SYMFLAG_TLSREL;
645 symt_get_address(sym, &sym_info->Address);
646 sym_info->Register = 0;
649 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
654 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
655 switch (data->u.value.n1.n2.vt)
657 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
658 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
659 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
660 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
661 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
662 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
663 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
664 case VT_EMPTY: sym_info->Value = 0; break;
666 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
672 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
676 case SymTagPublicSymbol:
677 sym_info->Flags |= SYMFLAG_EXPORT;
678 symt_get_address(sym, &sym_info->Address);
681 sym_info->Flags |= SYMFLAG_FUNCTION;
682 symt_get_address(sym, &sym_info->Address);
685 sym_info->Flags |= SYMFLAG_THUNK;
686 symt_get_address(sym, &sym_info->Address);
689 symt_get_address(sym, &sym_info->Address);
690 sym_info->Register = 0;
693 sym_info->Scope = 0; /* FIXME */
694 sym_info->Tag = sym->tag;
695 name = symt_get_name(sym);
696 if (sym_info->MaxNameLen)
698 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
699 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
700 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
702 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
703 memcpy(sym_info->Name, name, sym_info->NameLen);
704 sym_info->Name[sym_info->NameLen] = '\0';
707 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
708 sym, sym_info->Name, sym_info->Size,
709 wine_dbgstr_longlong(sym_info->Address));
714 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
716 SYMBOL_INFO* sym_info;
720 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
723 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
724 const struct symt_function* func, const struct symt* sym)
726 symt_fill_sym_info(pair, func, sym, se->sym_info);
727 if (se->index && se->sym_info->info != se->index) return FALSE;
728 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
729 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
730 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
733 static BOOL symt_enum_module(struct module_pair* pair, const WCHAR* match,
734 const struct sym_enum* se)
737 struct symt_ht* sym = NULL;
738 struct hash_table_iter hti;
742 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
743 while ((ptr = hash_table_iter_up(&hti)))
745 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
746 nameW = symt_get_nameW(&sym->symt);
747 ret = SymMatchStringW(nameW, match, FALSE);
748 HeapFree(GetProcessHeap(), 0, nameW);
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 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
761 unsigned low = 0, mid = high / 2;
765 symt_get_address(&elt->symt, &addr);
768 switch (cmp_sorttab_addr(module, mid, addr))
771 case -1: low = mid + 1; break;
772 case 1: high = mid; break;
774 mid = low + (high - low) / 2;
775 } while (low < high);
779 /***********************************************************************
782 * Rebuild sorted list of symbols for a module.
784 static BOOL resort_symbols(struct module* module)
788 if (!(module->module.NumSyms = module->num_symbols))
791 /* we know that set from 0 up to num_sorttab is already sorted
792 * so sort the remaining (new) symbols, and merge the two sets
793 * (unless the first set is empty)
795 delta = module->num_symbols - module->num_sorttab;
796 qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
797 if (module->num_sorttab)
799 int i, ins_idx = module->num_sorttab, prev_ins_idx;
800 static struct symt_ht** tmp;
801 static unsigned num_tmp;
805 static struct symt_ht** new;
807 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
809 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
812 module->num_sorttab = 0;
813 return resort_symbols(module);
818 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
819 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
821 for (i = delta - 1; i >= 0; i--)
823 prev_ins_idx = ins_idx;
824 ins_idx = where_to_insert(module, ins_idx, tmp[i]);
825 memmove(&module->addr_sorttab[ins_idx + i + 1],
826 &module->addr_sorttab[ins_idx],
827 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
828 module->addr_sorttab[ins_idx + i] = tmp[i];
831 module->num_sorttab = module->num_symbols;
832 return module->sortlist_valid = TRUE;
835 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
839 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
842 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
843 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
844 *size = 0x1000; /* arbitrary value */
847 /* assume addr is in module */
848 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
851 ULONG64 ref_addr, ref_size;
853 if (!module->sortlist_valid || !module->addr_sorttab)
855 if (!resort_symbols(module)) return NULL;
859 * Binary search to find closest symbol.
862 high = module->num_sorttab;
864 symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
865 if (addr < ref_addr) return NULL;
868 symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
869 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
870 if (addr >= ref_addr + ref_size) return NULL;
873 while (high > low + 1)
875 mid = (high + low) / 2;
876 if (cmp_sorttab_addr(module, mid, addr) < 0)
881 if (low != high && high != module->num_sorttab &&
882 cmp_sorttab_addr(module, high, addr) <= 0)
885 /* If found symbol is a public symbol, check if there are any other entries that
886 * might also have the same address, but would get better information
888 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
890 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
892 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
893 !cmp_sorttab_addr(module, low - 1, ref_addr))
895 else if (low < module->num_sorttab - 1 &&
896 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
897 !cmp_sorttab_addr(module, low + 1, ref_addr))
900 /* finally check that we fit into the found symbol */
901 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
902 if (addr < ref_addr) return NULL;
903 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
904 if (addr >= ref_addr + ref_size) return NULL;
906 return module->addr_sorttab[low];
909 static BOOL symt_enum_locals_helper(struct module_pair* pair,
910 const WCHAR* match, const struct sym_enum* se,
911 struct symt_function* func, const struct vector* v)
913 struct symt* lsym = NULL;
914 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
919 for (i=0; i<vector_length(v); i++)
921 lsym = *(struct symt**)vector_at(v, i);
926 struct symt_block* block = (struct symt_block*)lsym;
927 if (pc < block->address || block->address + block->size <= pc)
929 if (!symt_enum_locals_helper(pair, match, se, func, &block->vchildren))
934 nameW = symt_get_nameW(lsym);
935 ret = SymMatchStringW(nameW, match,
936 !(dbghelp_options & SYMOPT_CASE_INSENSITIVE));
937 HeapFree(GetProcessHeap(), 0, nameW);
940 if (send_symbol(se, pair, func, lsym)) return FALSE;
944 case SymTagFuncDebugStart:
945 case SymTagFuncDebugEnd:
949 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
956 static BOOL symt_enum_locals(struct process* pcs, const WCHAR* mask,
957 const struct sym_enum* se)
959 struct module_pair pair;
961 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
963 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
964 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
967 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
968 if (!module_get_debug(&pair)) return FALSE;
969 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
971 if (sym->symt.tag == SymTagFunction)
973 return symt_enum_locals_helper(&pair, mask ? mask : starW, se, (struct symt_function*)sym,
974 &((struct symt_function*)sym)->vchildren);
979 /******************************************************************
982 * Helper for transforming an ANSI symbol info into a UNICODE one.
983 * Assume that MaxNameLen is the same for both version (A & W).
985 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
987 siw->SizeOfStruct = si->SizeOfStruct;
988 siw->TypeIndex = si->TypeIndex;
989 siw->Reserved[0] = si->Reserved[0];
990 siw->Reserved[1] = si->Reserved[1];
991 siw->Index = si->info; /* FIXME: see dbghelp.h */
992 siw->Size = si->Size;
993 siw->ModBase = si->ModBase;
994 siw->Flags = si->Flags;
995 siw->Value = si->Value;
996 siw->Address = si->Address;
997 siw->Register = si->Register;
998 siw->Scope = si->Scope;
1000 siw->NameLen = si->NameLen;
1001 siw->MaxNameLen = si->MaxNameLen;
1002 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1005 /******************************************************************
1008 * Core routine for most of the enumeration of symbols
1010 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1011 const struct sym_enum* se)
1013 struct module_pair pair;
1017 pair.pcs = process_find_by_handle(hProcess);
1018 if (!pair.pcs) return FALSE;
1021 /* do local variables ? */
1022 if (!Mask || !(bang = strchrW(Mask, '!')))
1023 return symt_enum_locals(pair.pcs, Mask, se);
1025 if (bang == Mask) return FALSE;
1027 mod = HeapAlloc(GetProcessHeap(), 0, (bang - Mask + 1) * sizeof(WCHAR));
1028 if (!mod) return FALSE;
1029 memcpy(mod, Mask, (bang - Mask) * sizeof(WCHAR));
1030 mod[bang - Mask] = 0;
1032 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1034 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1036 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1037 symt_enum_module(&pair, bang + 1, se))
1041 /* not found in PE modules, retry on the ELF ones
1043 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1045 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1047 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1048 !module_get_containee(pair.pcs, pair.requested) &&
1049 module_get_debug(&pair))
1051 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1052 symt_enum_module(&pair, bang + 1, se))
1057 HeapFree(GetProcessHeap(), 0, mod);
1060 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1061 if (!module_get_debug(&pair))
1064 /* we always ignore module name from Mask when BaseOfDll is defined */
1065 if (Mask && (bang = strchrW(Mask, '!')))
1067 if (bang == Mask) return FALSE;
1071 symt_enum_module(&pair, Mask ? Mask : starW, se);
1076 static inline BOOL doSymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1077 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1082 se.cb = EnumSymbolsCallback;
1083 se.user = UserContext;
1087 se.sym_info = (PSYMBOL_INFO)se.buffer;
1089 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1092 /******************************************************************
1093 * SymEnumSymbols (DBGHELP.@)
1095 * cases BaseOfDll = 0
1096 * !foo fails always (despite what MSDN states)
1097 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1098 * no ! in Mask, lookup in local Context
1099 * cases BaseOfDll != 0
1100 * !foo fails always (despite what MSDN states)
1101 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1103 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1104 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1110 TRACE("(%p %s %s %p %p)\n",
1111 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1112 EnumSymbolsCallback, UserContext);
1116 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
1117 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1119 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
1121 ret = doSymEnumSymbols(hProcess, BaseOfDll, maskW, EnumSymbolsCallback, UserContext);
1122 HeapFree(GetProcessHeap(), 0, maskW);
1128 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1130 PSYMBOL_INFOW sym_info;
1131 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1135 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1137 struct sym_enumW* sew = ctx;
1139 copy_symbolW(sew->sym_info, si);
1141 return (sew->cb)(sew->sym_info, size, sew->ctx);
1144 /******************************************************************
1145 * SymEnumSymbolsW (DBGHELP.@)
1148 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1149 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1152 struct sym_enumW sew;
1154 sew.ctx = UserContext;
1155 sew.cb = EnumSymbolsCallback;
1156 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1158 return doSymEnumSymbols(hProcess, BaseOfDll, Mask, sym_enumW, &sew);
1161 struct sym_enumerate
1164 PSYM_ENUMSYMBOLS_CALLBACK cb;
1167 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1169 struct sym_enumerate* se = ctx;
1170 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1173 /***********************************************************************
1174 * SymEnumerateSymbols (DBGHELP.@)
1176 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1177 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1180 struct sym_enumerate se;
1182 se.ctx = UserContext;
1183 se.cb = EnumSymbolsCallback;
1185 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1188 struct sym_enumerate64
1191 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1194 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1196 struct sym_enumerate64* se = ctx;
1197 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1200 /***********************************************************************
1201 * SymEnumerateSymbols64 (DBGHELP.@)
1203 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1204 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1207 struct sym_enumerate64 se;
1209 se.ctx = UserContext;
1210 se.cb = EnumSymbolsCallback;
1212 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1215 /******************************************************************
1216 * SymFromAddr (DBGHELP.@)
1219 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1220 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1222 struct module_pair pair;
1223 struct symt_ht* sym;
1225 pair.pcs = process_find_by_handle(hProcess);
1226 if (!pair.pcs) return FALSE;
1227 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1228 if (!module_get_debug(&pair)) return FALSE;
1229 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1231 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1232 *Displacement = Address - Symbol->Address;
1236 /******************************************************************
1237 * SymFromAddrW (DBGHELP.@)
1240 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1241 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1247 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1248 si = HeapAlloc(GetProcessHeap(), 0, len);
1249 if (!si) return FALSE;
1251 si->SizeOfStruct = sizeof(*si);
1252 si->MaxNameLen = Symbol->MaxNameLen;
1253 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1255 copy_symbolW(Symbol, si);
1257 HeapFree(GetProcessHeap(), 0, si);
1261 /******************************************************************
1262 * SymGetSymFromAddr (DBGHELP.@)
1265 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1266 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1268 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1269 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1271 DWORD64 Displacement64;
1273 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1274 si->SizeOfStruct = sizeof(*si);
1275 si->MaxNameLen = MAX_SYM_NAME;
1276 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1280 *Displacement = Displacement64;
1281 Symbol->Address = si->Address;
1282 Symbol->Size = si->Size;
1283 Symbol->Flags = si->Flags;
1284 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1285 lstrcpynA(Symbol->Name, si->Name, len);
1289 /******************************************************************
1290 * SymGetSymFromAddr64 (DBGHELP.@)
1293 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1294 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1296 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1297 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1299 DWORD64 Displacement64;
1301 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1302 si->SizeOfStruct = sizeof(*si);
1303 si->MaxNameLen = MAX_SYM_NAME;
1304 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1308 *Displacement = Displacement64;
1309 Symbol->Address = si->Address;
1310 Symbol->Size = si->Size;
1311 Symbol->Flags = si->Flags;
1312 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1313 lstrcpynA(Symbol->Name, si->Name, len);
1317 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1318 SYMBOL_INFO* symbol)
1320 struct hash_table_iter hti;
1322 struct symt_ht* sym = NULL;
1323 struct module_pair pair;
1326 if (!(pair.requested = module)) return FALSE;
1327 if (!module_get_debug(&pair)) return FALSE;
1329 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1330 while ((ptr = hash_table_iter_up(&hti)))
1332 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1334 if (!strcmp(sym->hash_elt.name, name))
1336 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1343 /******************************************************************
1344 * SymFromName (DBGHELP.@)
1347 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1349 struct process* pcs = process_find_by_handle(hProcess);
1350 struct module* module;
1353 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1354 if (!pcs) return FALSE;
1355 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1356 name = strchr(Name, '!');
1360 assert(name - Name < sizeof(tmp));
1361 memcpy(tmp, Name, name - Name);
1362 tmp[name - Name] = '\0';
1363 module = module_find_by_nameA(pcs, tmp);
1364 return find_name(pcs, module, name + 1, Symbol);
1366 for (module = pcs->lmodules; module; module = module->next)
1368 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1371 /* not found in PE modules, retry on the ELF ones
1373 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1375 for (module = pcs->lmodules; module; module = module->next)
1377 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1378 !module_get_containee(pcs, module) &&
1379 find_name(pcs, module, Name, Symbol))
1386 /***********************************************************************
1387 * SymGetSymFromName64 (DBGHELP.@)
1389 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1391 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1392 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1395 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1396 si->SizeOfStruct = sizeof(*si);
1397 si->MaxNameLen = MAX_SYM_NAME;
1398 if (!SymFromName(hProcess, Name, si)) return FALSE;
1400 Symbol->Address = si->Address;
1401 Symbol->Size = si->Size;
1402 Symbol->Flags = si->Flags;
1403 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1404 lstrcpynA(Symbol->Name, si->Name, len);
1408 /***********************************************************************
1409 * SymGetSymFromName (DBGHELP.@)
1411 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1413 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1414 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1417 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1418 si->SizeOfStruct = sizeof(*si);
1419 si->MaxNameLen = MAX_SYM_NAME;
1420 if (!SymFromName(hProcess, Name, si)) return FALSE;
1422 Symbol->Address = si->Address;
1423 Symbol->Size = si->Size;
1424 Symbol->Flags = si->Flags;
1425 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1426 lstrcpynA(Symbol->Name, si->Name, len);
1430 /******************************************************************
1431 * sym_fill_func_line_info
1433 * fills information about a file
1435 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1436 DWORD64 addr, IMAGEHLP_LINE64* line)
1438 struct line_info* dli = NULL;
1442 assert(func->symt.tag == SymTagFunction);
1444 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1446 dli = vector_at(&func->vlines, i);
1447 if (!dli->is_source_file)
1449 if (found || dli->u.pc_offset > addr) continue;
1450 line->LineNumber = dli->line_number;
1451 line->Address = dli->u.pc_offset;
1458 line->FileName = (char*)source_get(module, dli->u.source_file);
1465 /***********************************************************************
1466 * SymGetSymNext64 (DBGHELP.@)
1468 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1471 * get module from Symbol.Address
1472 * get index in module.addr_sorttab of Symbol.Address
1474 * if out of module bounds, move to next module in process address space
1476 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1477 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1481 /***********************************************************************
1482 * SymGetSymNext (DBGHELP.@)
1484 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1486 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1487 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1491 /***********************************************************************
1492 * SymGetSymPrev64 (DBGHELP.@)
1494 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1496 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1497 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1501 /***********************************************************************
1502 * SymGetSymPrev (DBGHELP.@)
1504 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1506 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1507 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1511 /******************************************************************
1512 * copy_line_64_from_32 (internal)
1515 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1518 l64->Key = l32->Key;
1519 l64->LineNumber = l32->LineNumber;
1520 l64->FileName = l32->FileName;
1521 l64->Address = l32->Address;
1524 /******************************************************************
1525 * copy_line_W64_from_32 (internal)
1528 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1532 l64w->Key = l64->Key;
1533 l64w->LineNumber = l64->LineNumber;
1534 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1535 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1536 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1537 l64w->Address = l64->Address;
1540 /******************************************************************
1541 * copy_line_32_from_64 (internal)
1544 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1547 l32->Key = l64->Key;
1548 l32->LineNumber = l64->LineNumber;
1549 l32->FileName = l64->FileName;
1550 l32->Address = l64->Address;
1553 /******************************************************************
1554 * SymGetLineFromAddr (DBGHELP.@)
1557 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1558 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1560 IMAGEHLP_LINE64 il64;
1562 il64.SizeOfStruct = sizeof(il64);
1563 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1565 copy_line_32_from_64(Line, &il64);
1569 /******************************************************************
1570 * SymGetLineFromAddr64 (DBGHELP.@)
1573 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1574 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1576 struct module_pair pair;
1577 struct symt_ht* symt;
1579 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1581 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1583 pair.pcs = process_find_by_handle(hProcess);
1584 if (!pair.pcs) return FALSE;
1585 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1586 if (!module_get_debug(&pair)) return FALSE;
1587 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1589 if (symt->symt.tag != SymTagFunction) return FALSE;
1590 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1591 dwAddr, Line)) return FALSE;
1592 *pdwDisplacement = dwAddr - Line->Address;
1596 /******************************************************************
1597 * SymGetLineFromAddrW64 (DBGHELP.@)
1600 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1601 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1603 IMAGEHLP_LINE64 il64;
1605 il64.SizeOfStruct = sizeof(il64);
1606 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1608 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1612 /******************************************************************
1613 * SymGetLinePrev64 (DBGHELP.@)
1616 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1618 struct module_pair pair;
1619 struct line_info* li;
1620 BOOL in_search = FALSE;
1622 TRACE("(%p %p)\n", hProcess, Line);
1624 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1626 pair.pcs = process_find_by_handle(hProcess);
1627 if (!pair.pcs) return FALSE;
1628 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1629 if (!module_get_debug(&pair)) return FALSE;
1631 if (Line->Key == 0) return FALSE;
1633 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1634 * element we have to go back until we find the prev one to get the real
1635 * source file name for the DLIT_OFFSET element just before
1636 * the first DLIT_SOURCEFILE
1638 while (!li->is_first)
1641 if (!li->is_source_file)
1643 Line->LineNumber = li->line_number;
1644 Line->Address = li->u.pc_offset;
1646 if (!in_search) return TRUE;
1652 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1658 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1662 /******************************************************************
1663 * SymGetLinePrev (DBGHELP.@)
1666 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1668 IMAGEHLP_LINE64 line64;
1670 line64.SizeOfStruct = sizeof(line64);
1671 copy_line_64_from_32(&line64, Line);
1672 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1673 copy_line_32_from_64(Line, &line64);
1677 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1679 struct line_info* li;
1681 if (line->Key == 0) return FALSE;
1683 while (!li->is_last)
1686 if (!li->is_source_file)
1688 line->LineNumber = li->line_number;
1689 line->Address = li->u.pc_offset;
1693 line->FileName = (char*)source_get(module, li->u.source_file);
1698 /******************************************************************
1699 * SymGetLineNext64 (DBGHELP.@)
1702 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1704 struct module_pair pair;
1706 TRACE("(%p %p)\n", hProcess, Line);
1708 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1709 pair.pcs = process_find_by_handle(hProcess);
1710 if (!pair.pcs) return FALSE;
1711 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1712 if (!module_get_debug(&pair)) return FALSE;
1714 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1715 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1719 /******************************************************************
1720 * SymGetLineNext (DBGHELP.@)
1723 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1725 IMAGEHLP_LINE64 line64;
1727 line64.SizeOfStruct = sizeof(line64);
1728 copy_line_64_from_32(&line64, Line);
1729 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1730 copy_line_32_from_64(Line, &line64);
1734 /***********************************************************************
1735 * SymUnDName (DBGHELP.@)
1737 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1739 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1740 UNDNAME_COMPLETE) != 0;
1743 /***********************************************************************
1744 * SymUnDName64 (DBGHELP.@)
1746 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1748 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1749 UNDNAME_COMPLETE) != 0;
1752 static void * CDECL und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1753 static void CDECL und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1755 /***********************************************************************
1756 * UnDecorateSymbolName (DBGHELP.@)
1758 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1759 DWORD UndecoratedLength, DWORD Flags)
1761 /* undocumented from msvcrt */
1762 static char* (CDECL *p_undname)(char*, const char*, int, void* (CDECL*)(size_t), void (CDECL*)(void*), unsigned short);
1763 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1765 TRACE("(%s, %p, %d, 0x%08x)\n",
1766 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1770 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1771 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1772 if (!p_undname) return 0;
1775 if (!UnDecoratedName) return 0;
1776 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1777 und_alloc, und_free, Flags))
1779 return strlen(UnDecoratedName);
1782 #define WILDCHAR(x) (-(x))
1784 static int re_fetch_char(const WCHAR** re)
1788 case '\\': (*re)++; return *(*re)++;
1789 case '*': case '[': case '?': case '+': case '#': case ']': return WILDCHAR(*(*re)++);
1790 default: return *(*re)++;
1794 static inline int re_match_char(WCHAR ch1, WCHAR ch2, BOOL _case)
1796 return _case ? ch1 - ch2 : toupperW(ch1) - toupperW(ch2);
1799 static const WCHAR* re_match_one(const WCHAR* string, const WCHAR* elt, BOOL _case)
1804 switch (ch1 = re_fetch_char(&elt))
1807 return (ch1 >= 0 && re_match_char(*string, ch1, _case) == 0) ? ++string : NULL;
1808 case WILDCHAR('?'): return *string ? ++string : NULL;
1809 case WILDCHAR('*'): assert(0);
1810 case WILDCHAR('['): break;
1815 ch1 = re_fetch_char(&elt);
1816 if (ch1 == WILDCHAR(']')) return NULL;
1817 if (state == 1 && ch1 == '-') state = 2;
1820 if (re_match_char(*string, ch1, _case) == 0) return ++string;
1831 if (prev >= 0 && ch1 >= 0 && re_match_char(prev, *string, _case) <= 0 &&
1832 re_match_char(*string, ch1, _case) <= 0)
1841 /******************************************************************
1844 * match a substring of *pstring according to *pre regular expression
1845 * pstring and pre are only updated in case of successful match
1847 static BOOL re_match_multi(const WCHAR** pstring, const WCHAR** pre, BOOL _case)
1849 const WCHAR* re_end = *pre;
1850 const WCHAR* string_end = *pstring;
1851 const WCHAR* re_beg;
1852 const WCHAR* string_beg;
1856 while (*re_end && *string_end)
1858 string_beg = string_end;
1860 switch (ch = re_fetch_char(&re_end))
1862 case WILDCHAR(']'): case WILDCHAR('+'): case WILDCHAR('#'): return FALSE;
1864 /* transform '*' into '?#' */
1865 {static const WCHAR qmW[] = {'?',0}; re_beg = qmW;}
1870 if (!(ch = re_fetch_char(&re_end))) return FALSE;
1871 } while (ch != WILDCHAR(']'));
1881 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1887 while ((next = re_match_one(string_end, re_beg, _case))) string_end = next;
1888 for ( ; string_end >= string_beg; string_end--)
1890 if (re_match_multi(&string_end, &re_end, _case)) goto found;
1894 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1900 if (*re_end || *string_end) return FALSE;
1904 *pstring = string_end;
1908 /******************************************************************
1909 * SymMatchStringA (DBGHELP.@)
1912 BOOL WINAPI SymMatchStringA(PCSTR string, PCSTR re, BOOL _case)
1921 SetLastError(ERROR_INVALID_HANDLE);
1924 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1926 sz = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);
1927 if ((strW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1928 MultiByteToWideChar(CP_ACP, 0, string, -1, strW, sz);
1929 sz = MultiByteToWideChar(CP_ACP, 0, re, -1, NULL, 0);
1930 if ((reW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1931 MultiByteToWideChar(CP_ACP, 0, re, -1, reW, sz);
1934 ret = SymMatchStringW(strW, reW, _case);
1935 HeapFree(GetProcessHeap(), 0, strW);
1936 HeapFree(GetProcessHeap(), 0, reW);
1940 /******************************************************************
1941 * SymMatchStringW (DBGHELP.@)
1944 BOOL WINAPI SymMatchStringW(PCWSTR string, PCWSTR re, BOOL _case)
1946 TRACE("%s %s %c\n", debugstr_w(string), debugstr_w(re), _case ? 'Y' : 'N');
1950 SetLastError(ERROR_INVALID_HANDLE);
1953 return re_match_multi(&string, &re, _case);
1956 static inline BOOL doSymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1957 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1958 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1959 PVOID UserContext, DWORD Options)
1963 if (Options != SYMSEARCH_GLOBALSONLY)
1965 FIXME("Unsupported searching with options (%x)\n", Options);
1966 SetLastError(ERROR_INVALID_PARAMETER);
1970 se.cb = EnumSymbolsCallback;
1971 se.user = UserContext;
1975 se.sym_info = (PSYMBOL_INFO)se.buffer;
1977 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1980 /******************************************************************
1981 * SymSearch (DBGHELP.@)
1983 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1984 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1985 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1986 PVOID UserContext, DWORD Options)
1988 LPWSTR maskW = NULL;
1991 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1992 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1993 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1994 UserContext, Options);
1998 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
2000 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2002 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
2004 ret = doSymSearch(hProcess, BaseOfDll, Index, SymTag, maskW, Address,
2005 EnumSymbolsCallback, UserContext, Options);
2006 HeapFree(GetProcessHeap(), 0, maskW);
2010 /******************************************************************
2011 * SymSearchW (DBGHELP.@)
2013 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2014 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
2015 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
2016 PVOID UserContext, DWORD Options)
2018 struct sym_enumW sew;
2020 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2021 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
2022 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
2023 UserContext, Options);
2025 sew.ctx = UserContext;
2026 sew.cb = EnumSymbolsCallback;
2027 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
2029 return doSymSearch(hProcess, BaseOfDll, Index, SymTag, Mask, Address,
2030 sym_enumW, &sew, Options);
2033 /******************************************************************
2034 * SymAddSymbol (DBGHELP.@)
2037 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
2038 DWORD64 addr, DWORD size, DWORD flags)
2040 WCHAR nameW[MAX_SYM_NAME];
2042 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
2043 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
2046 /******************************************************************
2047 * SymAddSymbolW (DBGHELP.@)
2050 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
2051 DWORD64 addr, DWORD size, DWORD flags)
2053 struct module_pair pair;
2055 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
2057 pair.pcs = process_find_by_handle(hProcess);
2058 if (!pair.pcs) return FALSE;
2059 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
2060 if (!module_get_debug(&pair)) return FALSE;
2062 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2066 /******************************************************************
2067 * SymSetScopeFromAddr (DBGHELP.@)
2069 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
2071 struct process* pcs;
2073 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2075 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2079 /******************************************************************
2080 * SymEnumLines (DBGHELP.@)
2083 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2084 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2086 struct module_pair pair;
2087 struct hash_table_iter hti;
2088 struct symt_ht* sym;
2090 struct line_info* dli;
2095 if (!cb) return FALSE;
2096 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2098 pair.pcs = process_find_by_handle(hProcess);
2099 if (!pair.pcs) return FALSE;
2100 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2101 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2102 if (!module_get_debug(&pair)) return FALSE;
2103 if (!(srcmask = file_regex(srcfile))) return FALSE;
2105 sci.SizeOfStruct = sizeof(sci);
2108 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2109 while ((ptr = hash_table_iter_up(&hti)))
2113 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2114 if (sym->symt.tag != SymTagFunction) continue;
2116 sci.FileName[0] = '\0';
2117 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2119 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2120 if (dli->is_source_file)
2122 file = source_get(pair.effective, dli->u.source_file);
2123 if (!file) sci.FileName[0] = '\0';
2126 DWORD sz = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
2129 if ((fileW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2130 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, sz);
2131 if (SymMatchStringW(fileW, srcmask, FALSE))
2132 strcpy(sci.FileName, file);
2134 sci.FileName[0] = '\0';
2135 HeapFree(GetProcessHeap(), 0, fileW);
2138 else if (sci.FileName[0])
2141 sci.Obj[0] = '\0'; /* FIXME */
2142 sci.LineNumber = dli->line_number;
2143 sci.Address = dli->u.pc_offset;
2144 if (!cb(&sci, user)) break;
2148 HeapFree(GetProcessHeap(), 0, srcmask);
2152 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2153 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2155 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2156 dwLineNumber, plDisplacement, Line);
2160 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2161 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2163 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2164 dwLineNumber, lpDisplacement, Line);
2168 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2169 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2171 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2172 dwLineNumber, plDisplacement, Line);
2176 /******************************************************************
2177 * SymFromIndex (DBGHELP.@)
2180 BOOL WINAPI SymFromIndex(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFO symbol)
2182 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2183 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2188 /******************************************************************
2189 * SymFromIndexW (DBGHELP.@)
2192 BOOL WINAPI SymFromIndexW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFOW symbol)
2194 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2195 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);