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(struct module* module, int idx, ULONG64 addr)
55 symt_get_info(module, &module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
56 return cmp_addr(ref, addr);
59 struct module* symt_cmp_addr_module = NULL;
61 int symt_cmp_addr(const void* p1, const void* p2)
63 const struct symt* sym1 = *(const struct symt* const *)p1;
64 const struct symt* sym2 = *(const struct symt* const *)p2;
67 symt_get_info(symt_cmp_addr_module, sym1, TI_GET_ADDRESS, &a1);
68 symt_get_info(symt_cmp_addr_module, sym2, TI_GET_ADDRESS, &a2);
69 return cmp_addr(a1, a2);
72 DWORD symt_ptr2index(struct module* module, const struct symt* sym)
75 const struct symt** c;
76 int len = vector_length(&module->vsymt), i;
78 /* FIXME: this is inefficient */
79 for (i = 0; i < len; i++)
81 if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
85 c = vector_add(&module->vsymt, &module->pool);
93 struct symt* symt_index2ptr(struct module* module, DWORD id)
96 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
97 return *(struct symt**)vector_at(&module->vsymt, id);
99 return (struct symt*)id;
103 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
105 struct symt_ht** new;
108 if (sz <= module->sorttab_size) return TRUE;
109 if (module->addr_sorttab)
111 size = module->sorttab_size * 2;
112 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
113 size * sizeof(struct symt_ht*));
118 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
120 if (!new) return FALSE;
121 module->sorttab_size = size;
122 module->addr_sorttab = new;
126 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
130 hash_table_add(&module->ht_symbols, &ht->hash_elt);
131 /* Don't store in sorttab a symbol without address, they are of
132 * no use here (e.g. constant values)
134 if (symt_get_info(module, &ht->symt, TI_GET_ADDRESS, &addr) &&
135 symt_grow_sorttab(module, module->num_symbols + 1))
137 module->addr_sorttab[module->num_symbols++] = ht;
138 module->sortlist_valid = FALSE;
144 /* transforms a dbghelp's regular expression into a POSIX one
145 * Here are the valid dbghelp reg ex characters:
146 * * 0 or more characters
147 * ? a single character
149 * # 0 or more of preceding char
150 * + 1 or more of preceding char
151 * escapes \ on #, ?, [, ], *, +. don't work on -
153 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
156 BOOL in_escape = FALSE;
157 unsigned flags = REG_NOSUB;
159 if (numchar == -1) numchar = strlen( str );
161 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
164 while (*str && numchar--)
166 /* FIXME: this shouldn't be valid on '-' */
175 case '\\': in_escape = TRUE; break;
176 case '*': *p++ = '.'; *p++ = '*'; break;
177 case '?': *p++ = '.'; break;
178 case '#': *p++ = '*'; break;
179 /* escape some valid characters in dbghelp reg exp:s */
180 case '$': *p++ = '\\'; *p++ = '$'; break;
181 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
182 default: *p++ = *str; break;
193 if (_case) flags |= REG_ICASE;
194 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
195 HeapFree(GetProcessHeap(), 0, mask);
198 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
203 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
205 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
231 ret = !regcomp(re, mask, REG_NOSUB);
232 HeapFree(GetProcessHeap(), 0, mask);
235 FIXME("Couldn't compile %s\n", mask);
236 SetLastError(ERROR_INVALID_PARAMETER);
241 static int match_regexp( const regex_t *re, const char *str )
243 return !regexec( re, str, 0, NULL, 0 );
246 #else /* HAVE_REGEX_H */
248 /* if we don't have regexp support, fall back to a simple string comparison */
256 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
258 if (numchar == -1) numchar = strlen( str );
260 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
261 memcpy( re->str, str, numchar );
262 re->str[numchar] = 0;
266 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
268 if (!srcfile || !*srcfile) re->str = NULL;
269 else compile_regex( srcfile, -1, re, FALSE );
273 static int match_regexp( const regex_t *re, const char *str )
275 if (!re->str) return 1;
276 if (re->icase) return !lstrcmpiA( re->str, str );
277 return !strcmp( re->str, str );
280 static void regfree( regex_t *re )
282 HeapFree( GetProcessHeap(), 0, re->str );
285 #endif /* HAVE_REGEX_H */
287 struct symt_compiland* symt_new_compiland(struct module* module,
288 unsigned long address, unsigned src_idx)
290 struct symt_compiland* sym;
292 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
293 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
294 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
296 sym->symt.tag = SymTagCompiland;
297 sym->address = address;
298 sym->source = src_idx;
299 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
304 struct symt_public* symt_new_public(struct module* module,
305 struct symt_compiland* compiland,
307 unsigned long address, unsigned size)
309 struct symt_public* sym;
312 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
313 debugstr_w(module->module.ModuleName), name, address);
314 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
315 symt_find_nearest(module, address) != NULL)
317 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
319 sym->symt.tag = SymTagPublicSymbol;
320 sym->hash_elt.name = pool_strdup(&module->pool, name);
321 sym->container = compiland ? &compiland->symt : NULL;
322 sym->address = address;
324 symt_add_module_ht(module, (struct symt_ht*)sym);
327 p = vector_add(&compiland->vchildren, &module->pool);
334 struct symt_data* symt_new_global_variable(struct module* module,
335 struct symt_compiland* compiland,
336 const char* name, unsigned is_static,
337 unsigned long addr, unsigned long size,
340 struct symt_data* sym;
344 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
345 debugstr_w(module->module.ModuleName), name, addr, type);
346 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
348 sym->symt.tag = SymTagData;
349 sym->hash_elt.name = pool_strdup(&module->pool, name);
350 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
351 sym->container = compiland ? &compiland->symt : NULL;
353 sym->u.var.offset = addr;
354 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
357 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
358 debugstr_w(module->module.ModuleName), name,
359 wine_dbgstr_longlong(tsz), size);
361 symt_add_module_ht(module, (struct symt_ht*)sym);
364 p = vector_add(&compiland->vchildren, &module->pool);
371 struct symt_function* symt_new_function(struct module* module,
372 struct symt_compiland* compiland,
374 unsigned long addr, unsigned long size,
375 struct symt* sig_type)
377 struct symt_function* sym;
380 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
381 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
383 assert(!sig_type || sig_type->tag == SymTagFunctionType);
384 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
386 sym->symt.tag = SymTagFunction;
387 sym->hash_elt.name = pool_strdup(&module->pool, name);
388 sym->container = &compiland->symt;
390 sym->type = sig_type;
392 vector_init(&sym->vlines, sizeof(struct line_info), 64);
393 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
394 symt_add_module_ht(module, (struct symt_ht*)sym);
397 p = vector_add(&compiland->vchildren, &module->pool);
404 void symt_add_func_line(struct module* module, struct symt_function* func,
405 unsigned source_idx, int line_num, unsigned long offset)
407 struct line_info* dli;
408 BOOL last_matches = FALSE;
411 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
413 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
414 func, func->hash_elt.name, offset,
415 source_get(module, source_idx), line_num);
417 assert(func->symt.tag == SymTagFunction);
419 for (i=vector_length(&func->vlines)-1; i>=0; i--)
421 dli = vector_at(&func->vlines, i);
422 if (dli->is_source_file)
424 last_matches = (source_idx == dli->u.source_file);
431 /* we shouldn't have line changes on first line of function */
432 dli = vector_add(&func->vlines, &module->pool);
433 dli->is_source_file = 1;
434 dli->is_first = dli->is_last = 0;
435 dli->line_number = 0;
436 dli->u.source_file = source_idx;
438 dli = vector_add(&func->vlines, &module->pool);
439 dli->is_source_file = 0;
440 dli->is_first = dli->is_last = 0;
441 dli->line_number = line_num;
442 dli->u.pc_offset = func->address + offset;
445 /******************************************************************
446 * symt_add_func_local
448 * Adds a new local/parameter to a given function:
449 * In any cases, dt tells whether it's a local variable or a parameter
450 * If regno it's not 0:
451 * - then variable is stored in a register
452 * - otherwise, value is referenced by register + offset
453 * Otherwise, the variable is stored on the stack:
454 * - offset is then the offset from the frame register
456 struct symt_data* symt_add_func_local(struct module* module,
457 struct symt_function* func,
459 const struct location* loc,
460 struct symt_block* block,
461 struct symt* type, const char* name)
463 struct symt_data* locsym;
466 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
467 debugstr_w(module->module.ModuleName), func->hash_elt.name,
471 assert(func->symt.tag == SymTagFunction);
472 assert(dt == DataIsParam || dt == DataIsLocal);
474 locsym = pool_alloc(&module->pool, sizeof(*locsym));
475 locsym->symt.tag = SymTagData;
476 locsym->hash_elt.name = pool_strdup(&module->pool, name);
477 locsym->hash_elt.next = NULL;
479 locsym->container = block ? &block->symt : &func->symt;
481 locsym->u.var = *loc;
483 p = vector_add(&block->vchildren, &module->pool);
485 p = vector_add(&func->vchildren, &module->pool);
491 struct symt_block* symt_open_func_block(struct module* module,
492 struct symt_function* func,
493 struct symt_block* parent_block,
494 unsigned pc, unsigned len)
496 struct symt_block* block;
500 assert(func->symt.tag == SymTagFunction);
502 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
503 block = pool_alloc(&module->pool, sizeof(*block));
504 block->symt.tag = SymTagBlock;
505 block->address = func->address + pc;
507 block->container = parent_block ? &parent_block->symt : &func->symt;
508 vector_init(&block->vchildren, sizeof(struct symt*), 4);
510 p = vector_add(&parent_block->vchildren, &module->pool);
512 p = vector_add(&func->vchildren, &module->pool);
518 struct symt_block* symt_close_func_block(struct module* module,
519 const struct symt_function* func,
520 struct symt_block* block, unsigned pc)
523 assert(func->symt.tag == SymTagFunction);
525 if (pc) block->size = func->address + pc - block->address;
526 return (block->container->tag == SymTagBlock) ?
527 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
530 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
531 struct symt_function* func,
532 enum SymTagEnum point,
533 const struct location* loc,
536 struct symt_hierarchy_point*sym;
539 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
541 sym->symt.tag = point;
542 sym->parent = &func->symt;
544 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
545 p = vector_add(&func->vchildren, &module->pool);
551 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
554 struct line_info* dli;
557 /* We aren't adding any more locals or line numbers to this function.
558 * Free any spare memory that we might have allocated.
560 assert(func->symt.tag == SymTagFunction);
562 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
563 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
565 len = vector_length(&func->vlines);
568 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
569 dli = vector_at(&func->vlines, len); dli->is_last = 1;
574 struct symt_thunk* symt_new_thunk(struct module* module,
575 struct symt_compiland* compiland,
576 const char* name, THUNK_ORDINAL ord,
577 unsigned long addr, unsigned long size)
579 struct symt_thunk* sym;
581 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
582 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
584 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
586 sym->symt.tag = SymTagThunk;
587 sym->hash_elt.name = pool_strdup(&module->pool, name);
588 sym->container = &compiland->symt;
592 symt_add_module_ht(module, (struct symt_ht*)sym);
596 p = vector_add(&compiland->vchildren, &module->pool);
603 struct symt_data* symt_new_constant(struct module* module,
604 struct symt_compiland* compiland,
605 const char* name, struct symt* type,
608 struct symt_data* sym;
610 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
611 debugstr_w(module->module.ModuleName), name);
613 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
615 sym->symt.tag = SymTagData;
616 sym->hash_elt.name = pool_strdup(&module->pool, name);
617 sym->kind = DataIsConstant;
618 sym->container = compiland ? &compiland->symt : NULL;
621 symt_add_module_ht(module, (struct symt_ht*)sym);
625 p = vector_add(&compiland->vchildren, &module->pool);
632 struct symt_hierarchy_point* symt_new_label(struct module* module,
633 struct symt_compiland* compiland,
634 const char* name, unsigned long address)
636 struct symt_hierarchy_point* sym;
638 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
639 debugstr_w(module->module.ModuleName), name);
641 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
643 sym->symt.tag = SymTagLabel;
644 sym->hash_elt.name = pool_strdup(&module->pool, name);
645 sym->loc.kind = loc_absolute;
646 sym->loc.offset = address;
647 sym->parent = compiland ? &compiland->symt : NULL;
648 symt_add_module_ht(module, (struct symt_ht*)sym);
652 p = vector_add(&compiland->vchildren, &module->pool);
659 /* expect sym_info->MaxNameLen to be set before being called */
660 static void symt_fill_sym_info(struct module_pair* pair,
661 const struct symt_function* func,
662 const struct symt* sym, SYMBOL_INFO* sym_info)
667 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
668 sym_info->TypeIndex = 0;
669 sym_info->info = symt_ptr2index(pair->effective, sym);
670 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
671 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
672 (!sym_info->TypeIndex ||
673 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
674 TI_GET_LENGTH, &size)))
676 sym_info->Size = (DWORD)size;
677 sym_info->ModBase = pair->requested->module.BaseOfImage;
685 const struct symt_data* data = (const struct symt_data*)sym;
689 sym_info->Flags |= SYMFLAG_PARAMETER;
693 struct location loc = data->u.var;
695 if (loc.kind >= loc_user)
698 struct module_format* modfmt;
700 for (i = 0; i < DFI_LAST; i++)
702 modfmt = pair->effective->format_info[i];
703 if (modfmt && modfmt->loc_compute)
705 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
713 /* for now we report error cases as a negative register number */
714 sym_info->Flags |= SYMFLAG_LOCAL;
717 sym_info->Flags |= SYMFLAG_REGISTER;
718 sym_info->Register = loc.reg;
719 sym_info->Address = 0;
722 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
723 /* FIXME: it's i386 dependent !!! */
724 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
725 sym_info->Address = loc.offset;
728 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
729 sym_info->Value = loc.offset;
732 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
738 case DataIsFileStatic:
739 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
740 sym_info->Register = 0;
743 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
744 switch (data->u.value.n1.n2.vt)
746 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
747 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
748 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
749 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
750 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
751 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
752 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
753 case VT_EMPTY: sym_info->Value = 0; break;
755 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
761 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
765 case SymTagPublicSymbol:
766 sym_info->Flags |= SYMFLAG_EXPORT;
767 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
770 sym_info->Flags |= SYMFLAG_FUNCTION;
771 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
774 sym_info->Flags |= SYMFLAG_THUNK;
775 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
778 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
779 sym_info->Register = 0;
782 sym_info->Scope = 0; /* FIXME */
783 sym_info->Tag = sym->tag;
784 name = symt_get_name(sym);
785 if (sym_info->MaxNameLen)
787 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
788 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
789 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
791 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
792 memcpy(sym_info->Name, name, sym_info->NameLen);
793 sym_info->Name[sym_info->NameLen] = '\0';
796 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
797 sym, sym_info->Name, sym_info->Size,
798 wine_dbgstr_longlong(sym_info->Address));
803 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
805 SYMBOL_INFO* sym_info;
809 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
812 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
813 const struct symt_function* func, const struct symt* sym)
815 symt_fill_sym_info(pair, func, sym, se->sym_info);
816 if (se->index && se->sym_info->info != se->index) return FALSE;
817 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
818 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
819 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
822 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
823 const struct sym_enum* se)
826 struct symt_ht* sym = NULL;
827 struct hash_table_iter hti;
829 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
830 while ((ptr = hash_table_iter_up(&hti)))
832 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
833 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
835 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
836 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
837 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
843 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
845 unsigned low = 0, mid = high / 2;
849 symt_get_info(module, &elt->symt, TI_GET_ADDRESS, &addr);
852 switch (cmp_sorttab_addr(module, mid, addr))
855 case -1: low = mid + 1; break;
856 case 1: high = mid; break;
858 mid = low + (high - low) / 2;
859 } while (low < high);
863 /***********************************************************************
866 * Rebuild sorted list of symbols for a module.
868 static BOOL resort_symbols(struct module* module)
870 if (!(module->module.NumSyms = module->num_symbols))
873 /* FIXME: what's the optimal value here ??? */
874 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
876 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
877 struct symt_ht* tmp[30];
879 delta = module->num_symbols - module->num_sorttab;
880 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
881 symt_cmp_addr_module = module;
882 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
884 for (i = delta - 1; i >= 0; i--)
886 prev_ins_idx = ins_idx;
887 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
888 memmove(&module->addr_sorttab[ins_idx + i + 1],
889 &module->addr_sorttab[ins_idx],
890 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
891 module->addr_sorttab[ins_idx + i] = tmp[i];
896 symt_cmp_addr_module = module;
897 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
899 module->num_sorttab = module->num_symbols;
900 return module->sortlist_valid = TRUE;
903 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
907 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
910 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
911 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
912 *size = 0x1000; /* arbitrary value */
915 /* assume addr is in module */
916 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
919 ULONG64 ref_addr, ref_size;
921 if (!module->sortlist_valid || !module->addr_sorttab)
923 if (!resort_symbols(module)) return NULL;
927 * Binary search to find closest symbol.
930 high = module->num_sorttab;
932 symt_get_info(module, &module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
933 if (addr < ref_addr) return NULL;
936 symt_get_info(module, &module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
937 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
938 if (addr >= ref_addr + ref_size) return NULL;
941 while (high > low + 1)
943 mid = (high + low) / 2;
944 if (cmp_sorttab_addr(module, mid, addr) < 0)
949 if (low != high && high != module->num_sorttab &&
950 cmp_sorttab_addr(module, high, addr) <= 0)
953 /* If found symbol is a public symbol, check if there are any other entries that
954 * might also have the same address, but would get better information
956 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
958 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
960 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
961 !cmp_sorttab_addr(module, low - 1, ref_addr))
963 else if (low < module->num_sorttab - 1 &&
964 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
965 !cmp_sorttab_addr(module, low + 1, ref_addr))
968 /* finally check that we fit into the found symbol */
969 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
970 if (addr < ref_addr) return NULL;
971 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
972 if (addr >= ref_addr + ref_size) return NULL;
974 return module->addr_sorttab[low];
977 static BOOL symt_enum_locals_helper(struct module_pair* pair,
978 regex_t* preg, const struct sym_enum* se,
979 struct symt_function* func, const struct vector* v)
981 struct symt* lsym = NULL;
982 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
985 for (i=0; i<vector_length(v); i++)
987 lsym = *(struct symt**)vector_at(v, i);
992 struct symt_block* block = (struct symt_block*)lsym;
993 if (pc < block->address || block->address + block->size <= pc)
995 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
1000 if (match_regexp(preg, symt_get_name(lsym)))
1002 if (send_symbol(se, pair, func, lsym)) return FALSE;
1006 case SymTagFuncDebugStart:
1007 case SymTagFuncDebugEnd:
1011 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1018 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
1019 const struct sym_enum* se)
1021 struct module_pair pair;
1022 struct symt_ht* sym;
1023 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
1025 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1026 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1029 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1030 if (!module_get_debug(&pair)) return FALSE;
1031 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1033 if (sym->symt.tag == SymTagFunction)
1038 compile_regex(mask ? mask : "*", -1, &preg,
1039 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1040 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1041 &((struct symt_function*)sym)->vchildren);
1048 /******************************************************************
1051 * Helper for transforming an ANSI symbol info into a UNICODE one.
1052 * Assume that MaxNameLen is the same for both version (A & W).
1054 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1056 siw->SizeOfStruct = si->SizeOfStruct;
1057 siw->TypeIndex = si->TypeIndex;
1058 siw->Reserved[0] = si->Reserved[0];
1059 siw->Reserved[1] = si->Reserved[1];
1060 siw->Index = si->info; /* FIXME: see dbghelp.h */
1061 siw->Size = si->Size;
1062 siw->ModBase = si->ModBase;
1063 siw->Flags = si->Flags;
1064 siw->Value = si->Value;
1065 siw->Address = si->Address;
1066 siw->Register = si->Register;
1067 siw->Scope = si->Scope;
1069 siw->NameLen = si->NameLen;
1070 siw->MaxNameLen = si->MaxNameLen;
1071 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1074 /******************************************************************
1077 * Core routine for most of the enumeration of symbols
1079 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1080 const struct sym_enum* se)
1082 struct module_pair pair;
1084 regex_t mod_regex, sym_regex;
1086 pair.pcs = process_find_by_handle(hProcess);
1087 if (!pair.pcs) return FALSE;
1090 /* do local variables ? */
1091 if (!Mask || !(bang = strchr(Mask, '!')))
1092 return symt_enum_locals(pair.pcs, Mask, se);
1094 if (bang == Mask) return FALSE;
1096 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1097 compile_regex(bang + 1, -1, &sym_regex,
1098 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1100 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1102 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1104 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1105 symt_enum_module(&pair, &sym_regex, se))
1109 /* not found in PE modules, retry on the ELF ones
1111 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1113 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1115 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1116 !module_get_containee(pair.pcs, pair.requested) &&
1117 module_get_debug(&pair))
1119 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1120 symt_enum_module(&pair, &sym_regex, se))
1125 regfree(&mod_regex);
1126 regfree(&sym_regex);
1129 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1130 if (!module_get_debug(&pair))
1133 /* we always ignore module name from Mask when BaseOfDll is defined */
1134 if (Mask && (bang = strchr(Mask, '!')))
1136 if (bang == Mask) return FALSE;
1140 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1141 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1142 symt_enum_module(&pair, &sym_regex, se);
1143 regfree(&sym_regex);
1148 /******************************************************************
1149 * SymEnumSymbols (DBGHELP.@)
1151 * cases BaseOfDll = 0
1152 * !foo fails always (despite what MSDN states)
1153 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1154 * no ! in Mask, lookup in local Context
1155 * cases BaseOfDll != 0
1156 * !foo fails always (despite what MSDN states)
1157 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1159 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1160 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1165 TRACE("(%p %s %s %p %p)\n",
1166 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1167 EnumSymbolsCallback, UserContext);
1169 se.cb = EnumSymbolsCallback;
1170 se.user = UserContext;
1174 se.sym_info = (PSYMBOL_INFO)se.buffer;
1176 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1181 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1183 PSYMBOL_INFOW sym_info;
1184 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1188 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1190 struct sym_enumW* sew = ctx;
1192 copy_symbolW(sew->sym_info, si);
1194 return (sew->cb)(sew->sym_info, size, sew->ctx);
1197 /******************************************************************
1198 * SymEnumSymbolsW (DBGHELP.@)
1201 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1202 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1205 struct sym_enumW sew;
1209 sew.ctx = UserContext;
1210 sew.cb = EnumSymbolsCallback;
1211 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1215 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1216 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1217 if (!maskA) return FALSE;
1218 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1220 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1221 HeapFree(GetProcessHeap(), 0, maskA);
1226 struct sym_enumerate
1229 PSYM_ENUMSYMBOLS_CALLBACK cb;
1232 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1234 struct sym_enumerate* se = ctx;
1235 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1238 /***********************************************************************
1239 * SymEnumerateSymbols (DBGHELP.@)
1241 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1242 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1245 struct sym_enumerate se;
1247 se.ctx = UserContext;
1248 se.cb = EnumSymbolsCallback;
1250 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1253 struct sym_enumerate64
1256 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1259 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1261 struct sym_enumerate64* se = ctx;
1262 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1265 /***********************************************************************
1266 * SymEnumerateSymbols64 (DBGHELP.@)
1268 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1269 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1272 struct sym_enumerate64 se;
1274 se.ctx = UserContext;
1275 se.cb = EnumSymbolsCallback;
1277 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1280 /******************************************************************
1281 * SymFromAddr (DBGHELP.@)
1284 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1285 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1287 struct module_pair pair;
1288 struct symt_ht* sym;
1290 pair.pcs = process_find_by_handle(hProcess);
1291 if (!pair.pcs) return FALSE;
1292 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1293 if (!module_get_debug(&pair)) return FALSE;
1294 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1296 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1297 *Displacement = Address - Symbol->Address;
1301 /******************************************************************
1302 * SymFromAddrW (DBGHELP.@)
1305 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1306 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1312 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1313 si = HeapAlloc(GetProcessHeap(), 0, len);
1314 if (!si) return FALSE;
1316 si->SizeOfStruct = sizeof(*si);
1317 si->MaxNameLen = Symbol->MaxNameLen;
1318 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1320 copy_symbolW(Symbol, si);
1322 HeapFree(GetProcessHeap(), 0, si);
1326 /******************************************************************
1327 * SymGetSymFromAddr (DBGHELP.@)
1330 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1331 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1333 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1334 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1336 DWORD64 Displacement64;
1338 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1339 si->SizeOfStruct = sizeof(*si);
1340 si->MaxNameLen = MAX_SYM_NAME;
1341 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1345 *Displacement = Displacement64;
1346 Symbol->Address = si->Address;
1347 Symbol->Size = si->Size;
1348 Symbol->Flags = si->Flags;
1349 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1350 lstrcpynA(Symbol->Name, si->Name, len);
1354 /******************************************************************
1355 * SymGetSymFromAddr64 (DBGHELP.@)
1358 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1359 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1361 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1362 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1364 DWORD64 Displacement64;
1366 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1367 si->SizeOfStruct = sizeof(*si);
1368 si->MaxNameLen = MAX_SYM_NAME;
1369 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1373 *Displacement = Displacement64;
1374 Symbol->Address = si->Address;
1375 Symbol->Size = si->Size;
1376 Symbol->Flags = si->Flags;
1377 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1378 lstrcpynA(Symbol->Name, si->Name, len);
1382 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1383 SYMBOL_INFO* symbol)
1385 struct hash_table_iter hti;
1387 struct symt_ht* sym = NULL;
1388 struct module_pair pair;
1391 if (!(pair.requested = module)) return FALSE;
1392 if (!module_get_debug(&pair)) return FALSE;
1394 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1395 while ((ptr = hash_table_iter_up(&hti)))
1397 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1399 if (!strcmp(sym->hash_elt.name, name))
1401 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1408 /******************************************************************
1409 * SymFromName (DBGHELP.@)
1412 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1414 struct process* pcs = process_find_by_handle(hProcess);
1415 struct module* module;
1418 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1419 if (!pcs) return FALSE;
1420 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1421 name = strchr(Name, '!');
1425 assert(name - Name < sizeof(tmp));
1426 memcpy(tmp, Name, name - Name);
1427 tmp[name - Name] = '\0';
1428 module = module_find_by_nameA(pcs, tmp);
1429 return find_name(pcs, module, name + 1, Symbol);
1431 for (module = pcs->lmodules; module; module = module->next)
1433 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1436 /* not found in PE modules, retry on the ELF ones
1438 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1440 for (module = pcs->lmodules; module; module = module->next)
1442 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1443 !module_get_containee(pcs, module) &&
1444 find_name(pcs, module, Name, Symbol))
1451 /***********************************************************************
1452 * SymGetSymFromName64 (DBGHELP.@)
1454 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1456 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1457 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1460 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1461 si->SizeOfStruct = sizeof(*si);
1462 si->MaxNameLen = MAX_SYM_NAME;
1463 if (!SymFromName(hProcess, Name, si)) return FALSE;
1465 Symbol->Address = si->Address;
1466 Symbol->Size = si->Size;
1467 Symbol->Flags = si->Flags;
1468 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1469 lstrcpynA(Symbol->Name, si->Name, len);
1473 /***********************************************************************
1474 * SymGetSymFromName (DBGHELP.@)
1476 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1478 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1479 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1482 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1483 si->SizeOfStruct = sizeof(*si);
1484 si->MaxNameLen = MAX_SYM_NAME;
1485 if (!SymFromName(hProcess, Name, si)) return FALSE;
1487 Symbol->Address = si->Address;
1488 Symbol->Size = si->Size;
1489 Symbol->Flags = si->Flags;
1490 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1491 lstrcpynA(Symbol->Name, si->Name, len);
1495 /******************************************************************
1496 * sym_fill_func_line_info
1498 * fills information about a file
1500 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1501 DWORD64 addr, IMAGEHLP_LINE64* line)
1503 struct line_info* dli = NULL;
1507 assert(func->symt.tag == SymTagFunction);
1509 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1511 dli = vector_at(&func->vlines, i);
1512 if (!dli->is_source_file)
1514 if (found || dli->u.pc_offset > addr) continue;
1515 line->LineNumber = dli->line_number;
1516 line->Address = dli->u.pc_offset;
1523 line->FileName = (char*)source_get(module, dli->u.source_file);
1530 /***********************************************************************
1531 * SymGetSymNext64 (DBGHELP.@)
1533 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1536 * get module from Symbol.Address
1537 * get index in module.addr_sorttab of Symbol.Address
1539 * if out of module bounds, move to next module in process address space
1541 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1542 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1546 /***********************************************************************
1547 * SymGetSymNext (DBGHELP.@)
1549 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1551 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1552 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1556 /***********************************************************************
1557 * SymGetSymPrev64 (DBGHELP.@)
1559 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1561 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1562 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1566 /***********************************************************************
1567 * SymGetSymPrev (DBGHELP.@)
1569 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1571 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1572 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1576 /******************************************************************
1577 * copy_line_64_from_32 (internal)
1580 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1583 l64->Key = l32->Key;
1584 l64->LineNumber = l32->LineNumber;
1585 l64->FileName = l32->FileName;
1586 l64->Address = l32->Address;
1589 /******************************************************************
1590 * copy_line_W64_from_32 (internal)
1593 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1597 l64w->Key = l64->Key;
1598 l64w->LineNumber = l64->LineNumber;
1599 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1600 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1601 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1602 l64w->Address = l64->Address;
1605 /******************************************************************
1606 * copy_line_32_from_64 (internal)
1609 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1612 l32->Key = l64->Key;
1613 l32->LineNumber = l64->LineNumber;
1614 l32->FileName = l64->FileName;
1615 l32->Address = l64->Address;
1618 /******************************************************************
1619 * SymGetLineFromAddr (DBGHELP.@)
1622 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1623 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1625 IMAGEHLP_LINE64 il64;
1627 il64.SizeOfStruct = sizeof(il64);
1628 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1630 copy_line_32_from_64(Line, &il64);
1634 /******************************************************************
1635 * SymGetLineFromAddr64 (DBGHELP.@)
1638 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1639 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1641 struct module_pair pair;
1642 struct symt_ht* symt;
1644 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1646 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1648 pair.pcs = process_find_by_handle(hProcess);
1649 if (!pair.pcs) return FALSE;
1650 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1651 if (!module_get_debug(&pair)) return FALSE;
1652 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1654 if (symt->symt.tag != SymTagFunction) return FALSE;
1655 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1656 dwAddr, Line)) return FALSE;
1657 *pdwDisplacement = dwAddr - Line->Address;
1661 /******************************************************************
1662 * SymGetLineFromAddrW64 (DBGHELP.@)
1665 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1666 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1668 IMAGEHLP_LINE64 il64;
1670 il64.SizeOfStruct = sizeof(il64);
1671 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1673 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1677 /******************************************************************
1678 * SymGetLinePrev64 (DBGHELP.@)
1681 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1683 struct module_pair pair;
1684 struct line_info* li;
1685 BOOL in_search = FALSE;
1687 TRACE("(%p %p)\n", hProcess, Line);
1689 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1691 pair.pcs = process_find_by_handle(hProcess);
1692 if (!pair.pcs) return FALSE;
1693 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1694 if (!module_get_debug(&pair)) return FALSE;
1696 if (Line->Key == 0) return FALSE;
1698 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1699 * element we have to go back until we find the prev one to get the real
1700 * source file name for the DLIT_OFFSET element just before
1701 * the first DLIT_SOURCEFILE
1703 while (!li->is_first)
1706 if (!li->is_source_file)
1708 Line->LineNumber = li->line_number;
1709 Line->Address = li->u.pc_offset;
1711 if (!in_search) return TRUE;
1717 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1723 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1727 /******************************************************************
1728 * SymGetLinePrev (DBGHELP.@)
1731 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1733 IMAGEHLP_LINE64 line64;
1735 line64.SizeOfStruct = sizeof(line64);
1736 copy_line_64_from_32(&line64, Line);
1737 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1738 copy_line_32_from_64(Line, &line64);
1742 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1744 struct line_info* li;
1746 if (line->Key == 0) return FALSE;
1748 while (!li->is_last)
1751 if (!li->is_source_file)
1753 line->LineNumber = li->line_number;
1754 line->Address = li->u.pc_offset;
1758 line->FileName = (char*)source_get(module, li->u.source_file);
1763 /******************************************************************
1764 * SymGetLineNext64 (DBGHELP.@)
1767 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1769 struct module_pair pair;
1771 TRACE("(%p %p)\n", hProcess, Line);
1773 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1774 pair.pcs = process_find_by_handle(hProcess);
1775 if (!pair.pcs) return FALSE;
1776 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1777 if (!module_get_debug(&pair)) return FALSE;
1779 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1780 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1784 /******************************************************************
1785 * SymGetLineNext (DBGHELP.@)
1788 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1790 IMAGEHLP_LINE64 line64;
1792 line64.SizeOfStruct = sizeof(line64);
1793 copy_line_64_from_32(&line64, Line);
1794 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1795 copy_line_32_from_64(Line, &line64);
1799 /***********************************************************************
1800 * SymUnDName (DBGHELP.@)
1802 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1804 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1805 UNDNAME_COMPLETE) != 0;
1808 /***********************************************************************
1809 * SymUnDName64 (DBGHELP.@)
1811 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1813 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1814 UNDNAME_COMPLETE) != 0;
1817 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1818 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1820 /***********************************************************************
1821 * UnDecorateSymbolName (DBGHELP.@)
1823 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1824 DWORD UndecoratedLength, DWORD Flags)
1826 /* undocumented from msvcrt */
1827 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1828 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1830 TRACE("(%s, %p, %d, 0x%08x)\n",
1831 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1835 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1836 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1837 if (!p_undname) return 0;
1840 if (!UnDecoratedName) return 0;
1841 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1842 und_alloc, und_free, Flags))
1844 return strlen(UnDecoratedName);
1847 /******************************************************************
1848 * SymMatchString (DBGHELP.@)
1851 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1856 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1858 compile_regex(re, -1, &preg, _case);
1859 ret = match_regexp(&preg, string);
1864 /******************************************************************
1865 * SymSearch (DBGHELP.@)
1867 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1868 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1869 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1870 PVOID UserContext, DWORD Options)
1874 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1875 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1876 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1877 UserContext, Options);
1879 if (Options != SYMSEARCH_GLOBALSONLY)
1881 FIXME("Unsupported searching with options (%x)\n", Options);
1882 SetLastError(ERROR_INVALID_PARAMETER);
1886 se.cb = EnumSymbolsCallback;
1887 se.user = UserContext;
1891 se.sym_info = (PSYMBOL_INFO)se.buffer;
1893 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1896 /******************************************************************
1897 * SymSearchW (DBGHELP.@)
1899 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1900 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1901 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1902 PVOID UserContext, DWORD Options)
1904 struct sym_enumW sew;
1908 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1909 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1910 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1911 UserContext, Options);
1913 sew.ctx = UserContext;
1914 sew.cb = EnumSymbolsCallback;
1915 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1919 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1920 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1921 if (!maskA) return FALSE;
1922 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1924 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1925 sym_enumW, &sew, Options);
1926 HeapFree(GetProcessHeap(), 0, maskA);
1931 /******************************************************************
1932 * SymAddSymbol (DBGHELP.@)
1935 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1936 DWORD64 addr, DWORD size, DWORD flags)
1938 WCHAR nameW[MAX_SYM_NAME];
1940 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1941 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1944 /******************************************************************
1945 * SymAddSymbolW (DBGHELP.@)
1948 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1949 DWORD64 addr, DWORD size, DWORD flags)
1951 struct module_pair pair;
1953 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1955 pair.pcs = process_find_by_handle(hProcess);
1956 if (!pair.pcs) return FALSE;
1957 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1958 if (!module_get_debug(&pair)) return FALSE;
1960 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1964 /******************************************************************
1965 * SymSetScopeFromAddr (DBGHELP.@)
1967 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1969 struct process* pcs;
1971 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1973 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1977 /******************************************************************
1978 * SymEnumLines (DBGHELP.@)
1981 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1982 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1984 struct module_pair pair;
1985 struct hash_table_iter hti;
1986 struct symt_ht* sym;
1988 struct line_info* dli;
1993 if (!cb) return FALSE;
1994 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1996 pair.pcs = process_find_by_handle(hProcess);
1997 if (!pair.pcs) return FALSE;
1998 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1999 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2000 if (!module_get_debug(&pair)) return FALSE;
2001 if (!compile_file_regex(&re, srcfile)) return FALSE;
2003 sci.SizeOfStruct = sizeof(sci);
2006 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2007 while ((ptr = hash_table_iter_up(&hti)))
2011 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2012 if (sym->symt.tag != SymTagFunction) continue;
2014 sci.FileName[0] = '\0';
2015 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2017 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2018 if (dli->is_source_file)
2020 file = source_get(pair.effective, dli->u.source_file);
2021 if (!match_regexp(&re, file)) file = "";
2022 strcpy(sci.FileName, file);
2024 else if (sci.FileName[0])
2027 sci.Obj[0] = '\0'; /* FIXME */
2028 sci.LineNumber = dli->line_number;
2029 sci.Address = dli->u.pc_offset;
2030 if (!cb(&sci, user)) break;
2038 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2039 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2041 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2042 dwLineNumber, plDisplacement, Line);
2046 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2047 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2049 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2050 dwLineNumber, lpDisplacement, Line);
2054 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2055 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2057 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2058 dwLineNumber, plDisplacement, Line);