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)
54 symt_get_address(&module->addr_sorttab[idx]->symt, &ref);
55 return cmp_addr(ref, addr);
58 int symt_cmp_addr(const void* p1, const void* p2)
60 const struct symt* sym1 = *(const struct symt* const *)p1;
61 const struct symt* sym2 = *(const struct symt* const *)p2;
64 symt_get_address(sym1, &a1);
65 symt_get_address(sym2, &a2);
66 return cmp_addr(a1, a2);
69 DWORD symt_ptr2index(struct module* module, const struct symt* sym)
72 const struct symt** c;
73 int len = vector_length(&module->vsymt), i;
75 /* FIXME: this is inefficient */
76 for (i = 0; i < len; i++)
78 if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
82 c = vector_add(&module->vsymt, &module->pool);
90 struct symt* symt_index2ptr(struct module* module, DWORD id)
93 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
94 return *(struct symt**)vector_at(&module->vsymt, id);
96 return (struct symt*)id;
100 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
102 struct symt_ht** new;
105 if (sz <= module->sorttab_size) return TRUE;
106 if (module->addr_sorttab)
108 size = module->sorttab_size * 2;
109 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
110 size * sizeof(struct symt_ht*));
115 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
117 if (!new) return FALSE;
118 module->sorttab_size = size;
119 module->addr_sorttab = new;
123 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
127 hash_table_add(&module->ht_symbols, &ht->hash_elt);
128 /* Don't store in sorttab a symbol without address, they are of
129 * no use here (e.g. constant values)
131 if (symt_get_address(&ht->symt, &addr) &&
132 symt_grow_sorttab(module, module->num_symbols + 1))
134 module->addr_sorttab[module->num_symbols++] = ht;
135 module->sortlist_valid = FALSE;
141 /* transforms a dbghelp's regular expression into a POSIX one
142 * Here are the valid dbghelp reg ex characters:
143 * * 0 or more characters
144 * ? a single character
146 * # 0 or more of preceding char
147 * + 1 or more of preceding char
148 * escapes \ on #, ?, [, ], *, +. don't work on -
150 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
153 BOOL in_escape = FALSE;
154 unsigned flags = REG_NOSUB;
156 if (numchar == -1) numchar = strlen( str );
158 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
161 while (*str && numchar--)
163 /* FIXME: this shouldn't be valid on '-' */
172 case '\\': in_escape = TRUE; break;
173 case '*': *p++ = '.'; *p++ = '*'; break;
174 case '?': *p++ = '.'; break;
175 case '#': *p++ = '*'; break;
176 /* escape some valid characters in dbghelp reg exp:s */
177 case '$': *p++ = '\\'; *p++ = '$'; break;
178 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
179 default: *p++ = *str; break;
190 if (_case) flags |= REG_ICASE;
191 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
192 HeapFree(GetProcessHeap(), 0, mask);
195 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
200 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
202 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
232 ret = !regcomp(re, mask, REG_NOSUB);
233 HeapFree(GetProcessHeap(), 0, mask);
236 FIXME("Couldn't compile %s\n", mask);
237 SetLastError(ERROR_INVALID_PARAMETER);
242 static int match_regexp( const regex_t *re, const char *str )
244 return !regexec( re, str, 0, NULL, 0 );
247 #else /* HAVE_REGEX_H */
249 /* if we don't have regexp support, fall back to a simple string comparison */
257 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
259 if (numchar == -1) numchar = strlen( str );
261 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
262 memcpy( re->str, str, numchar );
263 re->str[numchar] = 0;
267 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
269 if (!srcfile || !*srcfile) re->str = NULL;
270 else compile_regex( srcfile, -1, re, FALSE );
274 static int match_regexp( const regex_t *re, const char *str )
276 if (!re->str) return 1;
277 if (re->icase) return !lstrcmpiA( re->str, str );
278 return !strcmp( re->str, str );
281 static void regfree( regex_t *re )
283 HeapFree( GetProcessHeap(), 0, re->str );
286 #endif /* HAVE_REGEX_H */
288 struct symt_compiland* symt_new_compiland(struct module* module,
289 unsigned long address, unsigned src_idx)
291 struct symt_compiland* sym;
293 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
294 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
295 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
297 sym->symt.tag = SymTagCompiland;
298 sym->address = address;
299 sym->source = src_idx;
300 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
305 struct symt_public* symt_new_public(struct module* module,
306 struct symt_compiland* compiland,
308 unsigned long address, unsigned size)
310 struct symt_public* sym;
313 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
314 debugstr_w(module->module.ModuleName), name, address);
315 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
316 symt_find_nearest(module, address) != NULL)
318 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
320 sym->symt.tag = SymTagPublicSymbol;
321 sym->hash_elt.name = pool_strdup(&module->pool, name);
322 sym->container = compiland ? &compiland->symt : NULL;
323 sym->address = address;
325 symt_add_module_ht(module, (struct symt_ht*)sym);
328 p = vector_add(&compiland->vchildren, &module->pool);
335 struct symt_data* symt_new_global_variable(struct module* module,
336 struct symt_compiland* compiland,
337 const char* name, unsigned is_static,
338 struct location loc, unsigned long size,
341 struct symt_data* sym;
345 TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
346 debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
347 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
349 sym->symt.tag = SymTagData;
350 sym->hash_elt.name = pool_strdup(&module->pool, name);
351 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
352 sym->container = compiland ? &compiland->symt : NULL;
355 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
358 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
359 debugstr_w(module->module.ModuleName), name,
360 wine_dbgstr_longlong(tsz), size);
362 symt_add_module_ht(module, (struct symt_ht*)sym);
365 p = vector_add(&compiland->vchildren, &module->pool);
372 struct symt_function* symt_new_function(struct module* module,
373 struct symt_compiland* compiland,
375 unsigned long addr, unsigned long size,
376 struct symt* sig_type)
378 struct symt_function* sym;
381 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
382 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
384 assert(!sig_type || sig_type->tag == SymTagFunctionType);
385 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
387 sym->symt.tag = SymTagFunction;
388 sym->hash_elt.name = pool_strdup(&module->pool, name);
389 sym->container = &compiland->symt;
391 sym->type = sig_type;
393 vector_init(&sym->vlines, sizeof(struct line_info), 64);
394 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
395 symt_add_module_ht(module, (struct symt_ht*)sym);
398 p = vector_add(&compiland->vchildren, &module->pool);
405 void symt_add_func_line(struct module* module, struct symt_function* func,
406 unsigned source_idx, int line_num, unsigned long offset)
408 struct line_info* dli;
409 BOOL last_matches = FALSE;
412 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
414 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
415 func, func->hash_elt.name, offset,
416 source_get(module, source_idx), line_num);
418 assert(func->symt.tag == SymTagFunction);
420 for (i=vector_length(&func->vlines)-1; i>=0; i--)
422 dli = vector_at(&func->vlines, i);
423 if (dli->is_source_file)
425 last_matches = (source_idx == dli->u.source_file);
432 /* we shouldn't have line changes on first line of function */
433 dli = vector_add(&func->vlines, &module->pool);
434 dli->is_source_file = 1;
435 dli->is_first = dli->is_last = 0;
436 dli->line_number = 0;
437 dli->u.source_file = source_idx;
439 dli = vector_add(&func->vlines, &module->pool);
440 dli->is_source_file = 0;
441 dli->is_first = dli->is_last = 0;
442 dli->line_number = line_num;
443 dli->u.pc_offset = func->address + offset;
446 /******************************************************************
447 * symt_add_func_local
449 * Adds a new local/parameter to a given function:
450 * In any cases, dt tells whether it's a local variable or a parameter
451 * If regno it's not 0:
452 * - then variable is stored in a register
453 * - otherwise, value is referenced by register + offset
454 * Otherwise, the variable is stored on the stack:
455 * - offset is then the offset from the frame register
457 struct symt_data* symt_add_func_local(struct module* module,
458 struct symt_function* func,
460 const struct location* loc,
461 struct symt_block* block,
462 struct symt* type, const char* name)
464 struct symt_data* locsym;
467 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
468 debugstr_w(module->module.ModuleName), func->hash_elt.name,
472 assert(func->symt.tag == SymTagFunction);
473 assert(dt == DataIsParam || dt == DataIsLocal);
475 locsym = pool_alloc(&module->pool, sizeof(*locsym));
476 locsym->symt.tag = SymTagData;
477 locsym->hash_elt.name = pool_strdup(&module->pool, name);
478 locsym->hash_elt.next = NULL;
480 locsym->container = block ? &block->symt : &func->symt;
482 locsym->u.var = *loc;
484 p = vector_add(&block->vchildren, &module->pool);
486 p = vector_add(&func->vchildren, &module->pool);
492 struct symt_block* symt_open_func_block(struct module* module,
493 struct symt_function* func,
494 struct symt_block* parent_block,
495 unsigned pc, unsigned len)
497 struct symt_block* block;
501 assert(func->symt.tag == SymTagFunction);
503 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
504 block = pool_alloc(&module->pool, sizeof(*block));
505 block->symt.tag = SymTagBlock;
506 block->address = func->address + pc;
508 block->container = parent_block ? &parent_block->symt : &func->symt;
509 vector_init(&block->vchildren, sizeof(struct symt*), 4);
511 p = vector_add(&parent_block->vchildren, &module->pool);
513 p = vector_add(&func->vchildren, &module->pool);
519 struct symt_block* symt_close_func_block(struct module* module,
520 const struct symt_function* func,
521 struct symt_block* block, unsigned pc)
524 assert(func->symt.tag == SymTagFunction);
526 if (pc) block->size = func->address + pc - block->address;
527 return (block->container->tag == SymTagBlock) ?
528 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
531 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
532 struct symt_function* func,
533 enum SymTagEnum point,
534 const struct location* loc,
537 struct symt_hierarchy_point*sym;
540 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
542 sym->symt.tag = point;
543 sym->parent = &func->symt;
545 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
546 p = vector_add(&func->vchildren, &module->pool);
552 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
555 struct line_info* dli;
558 /* We aren't adding any more locals or line numbers to this function.
559 * Free any spare memory that we might have allocated.
561 assert(func->symt.tag == SymTagFunction);
563 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
564 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
566 len = vector_length(&func->vlines);
569 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
570 dli = vector_at(&func->vlines, len); dli->is_last = 1;
575 struct symt_thunk* symt_new_thunk(struct module* module,
576 struct symt_compiland* compiland,
577 const char* name, THUNK_ORDINAL ord,
578 unsigned long addr, unsigned long size)
580 struct symt_thunk* sym;
582 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
583 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
585 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
587 sym->symt.tag = SymTagThunk;
588 sym->hash_elt.name = pool_strdup(&module->pool, name);
589 sym->container = &compiland->symt;
593 symt_add_module_ht(module, (struct symt_ht*)sym);
597 p = vector_add(&compiland->vchildren, &module->pool);
604 struct symt_data* symt_new_constant(struct module* module,
605 struct symt_compiland* compiland,
606 const char* name, struct symt* type,
609 struct symt_data* sym;
611 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
612 debugstr_w(module->module.ModuleName), name);
614 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
616 sym->symt.tag = SymTagData;
617 sym->hash_elt.name = pool_strdup(&module->pool, name);
618 sym->kind = DataIsConstant;
619 sym->container = compiland ? &compiland->symt : NULL;
622 symt_add_module_ht(module, (struct symt_ht*)sym);
626 p = vector_add(&compiland->vchildren, &module->pool);
633 struct symt_hierarchy_point* symt_new_label(struct module* module,
634 struct symt_compiland* compiland,
635 const char* name, unsigned long address)
637 struct symt_hierarchy_point* sym;
639 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
640 debugstr_w(module->module.ModuleName), name);
642 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
644 sym->symt.tag = SymTagLabel;
645 sym->hash_elt.name = pool_strdup(&module->pool, name);
646 sym->loc.kind = loc_absolute;
647 sym->loc.offset = address;
648 sym->parent = compiland ? &compiland->symt : NULL;
649 symt_add_module_ht(module, (struct symt_ht*)sym);
653 p = vector_add(&compiland->vchildren, &module->pool);
660 /* expect sym_info->MaxNameLen to be set before being called */
661 static void symt_fill_sym_info(struct module_pair* pair,
662 const struct symt_function* func,
663 const struct symt* sym, SYMBOL_INFO* sym_info)
668 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
669 sym_info->TypeIndex = 0;
670 sym_info->info = symt_ptr2index(pair->effective, sym);
671 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
672 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
673 (!sym_info->TypeIndex ||
674 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
675 TI_GET_LENGTH, &size)))
677 sym_info->Size = (DWORD)size;
678 sym_info->ModBase = pair->requested->module.BaseOfImage;
686 const struct symt_data* data = (const struct symt_data*)sym;
690 sym_info->Flags |= SYMFLAG_PARAMETER;
694 struct location loc = data->u.var;
696 if (loc.kind >= loc_user)
699 struct module_format* modfmt;
701 for (i = 0; i < DFI_LAST; i++)
703 modfmt = pair->effective->format_info[i];
704 if (modfmt && modfmt->loc_compute)
706 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
714 /* for now we report error cases as a negative register number */
715 sym_info->Flags |= SYMFLAG_LOCAL;
718 sym_info->Flags |= SYMFLAG_REGISTER;
719 sym_info->Register = loc.reg;
720 sym_info->Address = 0;
723 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
724 sym_info->Register = loc.reg;
725 if (loc.reg == CV_REG_NONE || (int)loc.reg < 0 /* error */)
726 FIXME("suspicious register value %x\n", loc.reg);
727 sym_info->Address = loc.offset;
730 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
731 sym_info->Value = loc.offset;
734 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
740 case DataIsFileStatic:
741 switch (data->u.var.kind)
744 sym_info->Flags |= SYMFLAG_TLSREL;
747 symt_get_address(sym, &sym_info->Address);
748 sym_info->Register = 0;
751 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
756 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
757 switch (data->u.value.n1.n2.vt)
759 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
760 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
761 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
762 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
763 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
764 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
765 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
766 case VT_EMPTY: sym_info->Value = 0; break;
768 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
774 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
778 case SymTagPublicSymbol:
779 sym_info->Flags |= SYMFLAG_EXPORT;
780 symt_get_address(sym, &sym_info->Address);
783 sym_info->Flags |= SYMFLAG_FUNCTION;
784 symt_get_address(sym, &sym_info->Address);
787 sym_info->Flags |= SYMFLAG_THUNK;
788 symt_get_address(sym, &sym_info->Address);
791 symt_get_address(sym, &sym_info->Address);
792 sym_info->Register = 0;
795 sym_info->Scope = 0; /* FIXME */
796 sym_info->Tag = sym->tag;
797 name = symt_get_name(sym);
798 if (sym_info->MaxNameLen)
800 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
801 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
802 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
804 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
805 memcpy(sym_info->Name, name, sym_info->NameLen);
806 sym_info->Name[sym_info->NameLen] = '\0';
809 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
810 sym, sym_info->Name, sym_info->Size,
811 wine_dbgstr_longlong(sym_info->Address));
816 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
818 SYMBOL_INFO* sym_info;
822 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
825 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
826 const struct symt_function* func, const struct symt* sym)
828 symt_fill_sym_info(pair, func, sym, se->sym_info);
829 if (se->index && se->sym_info->info != se->index) return FALSE;
830 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
831 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
832 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
835 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
836 const struct sym_enum* se)
839 struct symt_ht* sym = NULL;
840 struct hash_table_iter hti;
842 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
843 while ((ptr = hash_table_iter_up(&hti)))
845 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
846 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
848 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
849 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
850 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
856 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
858 unsigned low = 0, mid = high / 2;
862 symt_get_address(&elt->symt, &addr);
865 switch (cmp_sorttab_addr(module, mid, addr))
868 case -1: low = mid + 1; break;
869 case 1: high = mid; break;
871 mid = low + (high - low) / 2;
872 } while (low < high);
876 /***********************************************************************
879 * Rebuild sorted list of symbols for a module.
881 static BOOL resort_symbols(struct module* module)
885 if (!(module->module.NumSyms = module->num_symbols))
888 /* we know that set from 0 up to num_sorttab is already sorted
889 * so sort the remaining (new) symbols, and merge the two sets
890 * (unless the first set is empty)
892 delta = module->num_symbols - module->num_sorttab;
893 qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
894 if (module->num_sorttab)
896 int i, ins_idx = module->num_sorttab, prev_ins_idx;
897 static struct symt_ht** tmp;
898 static unsigned num_tmp;
902 static struct symt_ht** new;
904 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
906 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
909 module->num_sorttab = 0;
910 return resort_symbols(module);
915 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
916 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
918 for (i = delta - 1; i >= 0; i--)
920 prev_ins_idx = ins_idx;
921 ins_idx = where_to_insert(module, ins_idx, tmp[i]);
922 memmove(&module->addr_sorttab[ins_idx + i + 1],
923 &module->addr_sorttab[ins_idx],
924 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
925 module->addr_sorttab[ins_idx + i] = tmp[i];
928 module->num_sorttab = module->num_symbols;
929 return module->sortlist_valid = TRUE;
932 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
936 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
939 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
940 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
941 *size = 0x1000; /* arbitrary value */
944 /* assume addr is in module */
945 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
948 ULONG64 ref_addr, ref_size;
950 if (!module->sortlist_valid || !module->addr_sorttab)
952 if (!resort_symbols(module)) return NULL;
956 * Binary search to find closest symbol.
959 high = module->num_sorttab;
961 symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
962 if (addr < ref_addr) return NULL;
965 symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
966 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
967 if (addr >= ref_addr + ref_size) return NULL;
970 while (high > low + 1)
972 mid = (high + low) / 2;
973 if (cmp_sorttab_addr(module, mid, addr) < 0)
978 if (low != high && high != module->num_sorttab &&
979 cmp_sorttab_addr(module, high, addr) <= 0)
982 /* If found symbol is a public symbol, check if there are any other entries that
983 * might also have the same address, but would get better information
985 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
987 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
989 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
990 !cmp_sorttab_addr(module, low - 1, ref_addr))
992 else if (low < module->num_sorttab - 1 &&
993 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
994 !cmp_sorttab_addr(module, low + 1, ref_addr))
997 /* finally check that we fit into the found symbol */
998 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
999 if (addr < ref_addr) return NULL;
1000 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
1001 if (addr >= ref_addr + ref_size) return NULL;
1003 return module->addr_sorttab[low];
1006 static BOOL symt_enum_locals_helper(struct module_pair* pair,
1007 regex_t* preg, const struct sym_enum* se,
1008 struct symt_function* func, const struct vector* v)
1010 struct symt* lsym = NULL;
1011 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
1014 for (i=0; i<vector_length(v); i++)
1016 lsym = *(struct symt**)vector_at(v, i);
1021 struct symt_block* block = (struct symt_block*)lsym;
1022 if (pc < block->address || block->address + block->size <= pc)
1024 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
1029 if (match_regexp(preg, symt_get_name(lsym)))
1031 if (send_symbol(se, pair, func, lsym)) return FALSE;
1035 case SymTagFuncDebugStart:
1036 case SymTagFuncDebugEnd:
1040 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1047 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
1048 const struct sym_enum* se)
1050 struct module_pair pair;
1051 struct symt_ht* sym;
1052 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
1054 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1055 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1058 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1059 if (!module_get_debug(&pair)) return FALSE;
1060 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1062 if (sym->symt.tag == SymTagFunction)
1067 compile_regex(mask ? mask : "*", -1, &preg,
1068 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1069 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1070 &((struct symt_function*)sym)->vchildren);
1077 /******************************************************************
1080 * Helper for transforming an ANSI symbol info into a UNICODE one.
1081 * Assume that MaxNameLen is the same for both version (A & W).
1083 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1085 siw->SizeOfStruct = si->SizeOfStruct;
1086 siw->TypeIndex = si->TypeIndex;
1087 siw->Reserved[0] = si->Reserved[0];
1088 siw->Reserved[1] = si->Reserved[1];
1089 siw->Index = si->info; /* FIXME: see dbghelp.h */
1090 siw->Size = si->Size;
1091 siw->ModBase = si->ModBase;
1092 siw->Flags = si->Flags;
1093 siw->Value = si->Value;
1094 siw->Address = si->Address;
1095 siw->Register = si->Register;
1096 siw->Scope = si->Scope;
1098 siw->NameLen = si->NameLen;
1099 siw->MaxNameLen = si->MaxNameLen;
1100 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1103 /******************************************************************
1106 * Core routine for most of the enumeration of symbols
1108 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1109 const struct sym_enum* se)
1111 struct module_pair pair;
1113 regex_t mod_regex, sym_regex;
1115 pair.pcs = process_find_by_handle(hProcess);
1116 if (!pair.pcs) return FALSE;
1119 /* do local variables ? */
1120 if (!Mask || !(bang = strchr(Mask, '!')))
1121 return symt_enum_locals(pair.pcs, Mask, se);
1123 if (bang == Mask) return FALSE;
1125 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1126 compile_regex(bang + 1, -1, &sym_regex,
1127 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1129 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1131 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1133 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1134 symt_enum_module(&pair, &sym_regex, se))
1138 /* not found in PE modules, retry on the ELF ones
1140 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1142 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1144 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1145 !module_get_containee(pair.pcs, pair.requested) &&
1146 module_get_debug(&pair))
1148 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1149 symt_enum_module(&pair, &sym_regex, se))
1154 regfree(&mod_regex);
1155 regfree(&sym_regex);
1158 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1159 if (!module_get_debug(&pair))
1162 /* we always ignore module name from Mask when BaseOfDll is defined */
1163 if (Mask && (bang = strchr(Mask, '!')))
1165 if (bang == Mask) return FALSE;
1169 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1170 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1171 symt_enum_module(&pair, &sym_regex, se);
1172 regfree(&sym_regex);
1177 /******************************************************************
1178 * SymEnumSymbols (DBGHELP.@)
1180 * cases BaseOfDll = 0
1181 * !foo fails always (despite what MSDN states)
1182 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1183 * no ! in Mask, lookup in local Context
1184 * cases BaseOfDll != 0
1185 * !foo fails always (despite what MSDN states)
1186 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1188 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1189 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1194 TRACE("(%p %s %s %p %p)\n",
1195 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1196 EnumSymbolsCallback, UserContext);
1198 se.cb = EnumSymbolsCallback;
1199 se.user = UserContext;
1203 se.sym_info = (PSYMBOL_INFO)se.buffer;
1205 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1210 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1212 PSYMBOL_INFOW sym_info;
1213 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1217 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1219 struct sym_enumW* sew = ctx;
1221 copy_symbolW(sew->sym_info, si);
1223 return (sew->cb)(sew->sym_info, size, sew->ctx);
1226 /******************************************************************
1227 * SymEnumSymbolsW (DBGHELP.@)
1230 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1231 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1234 struct sym_enumW sew;
1238 sew.ctx = UserContext;
1239 sew.cb = EnumSymbolsCallback;
1240 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1244 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1245 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1246 if (!maskA) return FALSE;
1247 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1249 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1250 HeapFree(GetProcessHeap(), 0, maskA);
1255 struct sym_enumerate
1258 PSYM_ENUMSYMBOLS_CALLBACK cb;
1261 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1263 struct sym_enumerate* se = ctx;
1264 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1267 /***********************************************************************
1268 * SymEnumerateSymbols (DBGHELP.@)
1270 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1271 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1274 struct sym_enumerate se;
1276 se.ctx = UserContext;
1277 se.cb = EnumSymbolsCallback;
1279 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1282 struct sym_enumerate64
1285 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1288 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1290 struct sym_enumerate64* se = ctx;
1291 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1294 /***********************************************************************
1295 * SymEnumerateSymbols64 (DBGHELP.@)
1297 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1298 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1301 struct sym_enumerate64 se;
1303 se.ctx = UserContext;
1304 se.cb = EnumSymbolsCallback;
1306 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1309 /******************************************************************
1310 * SymFromAddr (DBGHELP.@)
1313 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1314 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1316 struct module_pair pair;
1317 struct symt_ht* sym;
1319 pair.pcs = process_find_by_handle(hProcess);
1320 if (!pair.pcs) return FALSE;
1321 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1322 if (!module_get_debug(&pair)) return FALSE;
1323 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1325 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1326 *Displacement = Address - Symbol->Address;
1330 /******************************************************************
1331 * SymFromAddrW (DBGHELP.@)
1334 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1335 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1341 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1342 si = HeapAlloc(GetProcessHeap(), 0, len);
1343 if (!si) return FALSE;
1345 si->SizeOfStruct = sizeof(*si);
1346 si->MaxNameLen = Symbol->MaxNameLen;
1347 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1349 copy_symbolW(Symbol, si);
1351 HeapFree(GetProcessHeap(), 0, si);
1355 /******************************************************************
1356 * SymGetSymFromAddr (DBGHELP.@)
1359 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1360 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1362 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1363 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1365 DWORD64 Displacement64;
1367 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1368 si->SizeOfStruct = sizeof(*si);
1369 si->MaxNameLen = MAX_SYM_NAME;
1370 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1374 *Displacement = Displacement64;
1375 Symbol->Address = si->Address;
1376 Symbol->Size = si->Size;
1377 Symbol->Flags = si->Flags;
1378 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1379 lstrcpynA(Symbol->Name, si->Name, len);
1383 /******************************************************************
1384 * SymGetSymFromAddr64 (DBGHELP.@)
1387 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1388 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1390 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1391 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1393 DWORD64 Displacement64;
1395 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1396 si->SizeOfStruct = sizeof(*si);
1397 si->MaxNameLen = MAX_SYM_NAME;
1398 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1402 *Displacement = Displacement64;
1403 Symbol->Address = si->Address;
1404 Symbol->Size = si->Size;
1405 Symbol->Flags = si->Flags;
1406 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1407 lstrcpynA(Symbol->Name, si->Name, len);
1411 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1412 SYMBOL_INFO* symbol)
1414 struct hash_table_iter hti;
1416 struct symt_ht* sym = NULL;
1417 struct module_pair pair;
1420 if (!(pair.requested = module)) return FALSE;
1421 if (!module_get_debug(&pair)) return FALSE;
1423 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1424 while ((ptr = hash_table_iter_up(&hti)))
1426 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1428 if (!strcmp(sym->hash_elt.name, name))
1430 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1437 /******************************************************************
1438 * SymFromName (DBGHELP.@)
1441 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1443 struct process* pcs = process_find_by_handle(hProcess);
1444 struct module* module;
1447 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1448 if (!pcs) return FALSE;
1449 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1450 name = strchr(Name, '!');
1454 assert(name - Name < sizeof(tmp));
1455 memcpy(tmp, Name, name - Name);
1456 tmp[name - Name] = '\0';
1457 module = module_find_by_nameA(pcs, tmp);
1458 return find_name(pcs, module, name + 1, Symbol);
1460 for (module = pcs->lmodules; module; module = module->next)
1462 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1465 /* not found in PE modules, retry on the ELF ones
1467 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1469 for (module = pcs->lmodules; module; module = module->next)
1471 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1472 !module_get_containee(pcs, module) &&
1473 find_name(pcs, module, Name, Symbol))
1480 /***********************************************************************
1481 * SymGetSymFromName64 (DBGHELP.@)
1483 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1485 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1486 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1489 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1490 si->SizeOfStruct = sizeof(*si);
1491 si->MaxNameLen = MAX_SYM_NAME;
1492 if (!SymFromName(hProcess, Name, si)) return FALSE;
1494 Symbol->Address = si->Address;
1495 Symbol->Size = si->Size;
1496 Symbol->Flags = si->Flags;
1497 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1498 lstrcpynA(Symbol->Name, si->Name, len);
1502 /***********************************************************************
1503 * SymGetSymFromName (DBGHELP.@)
1505 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1507 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1508 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1511 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1512 si->SizeOfStruct = sizeof(*si);
1513 si->MaxNameLen = MAX_SYM_NAME;
1514 if (!SymFromName(hProcess, Name, si)) return FALSE;
1516 Symbol->Address = si->Address;
1517 Symbol->Size = si->Size;
1518 Symbol->Flags = si->Flags;
1519 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1520 lstrcpynA(Symbol->Name, si->Name, len);
1524 /******************************************************************
1525 * sym_fill_func_line_info
1527 * fills information about a file
1529 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1530 DWORD64 addr, IMAGEHLP_LINE64* line)
1532 struct line_info* dli = NULL;
1536 assert(func->symt.tag == SymTagFunction);
1538 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1540 dli = vector_at(&func->vlines, i);
1541 if (!dli->is_source_file)
1543 if (found || dli->u.pc_offset > addr) continue;
1544 line->LineNumber = dli->line_number;
1545 line->Address = dli->u.pc_offset;
1552 line->FileName = (char*)source_get(module, dli->u.source_file);
1559 /***********************************************************************
1560 * SymGetSymNext64 (DBGHELP.@)
1562 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1565 * get module from Symbol.Address
1566 * get index in module.addr_sorttab of Symbol.Address
1568 * if out of module bounds, move to next module in process address space
1570 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1571 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1575 /***********************************************************************
1576 * SymGetSymNext (DBGHELP.@)
1578 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1580 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1581 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1585 /***********************************************************************
1586 * SymGetSymPrev64 (DBGHELP.@)
1588 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1590 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1591 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1595 /***********************************************************************
1596 * SymGetSymPrev (DBGHELP.@)
1598 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1600 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1601 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1605 /******************************************************************
1606 * copy_line_64_from_32 (internal)
1609 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1612 l64->Key = l32->Key;
1613 l64->LineNumber = l32->LineNumber;
1614 l64->FileName = l32->FileName;
1615 l64->Address = l32->Address;
1618 /******************************************************************
1619 * copy_line_W64_from_32 (internal)
1622 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1626 l64w->Key = l64->Key;
1627 l64w->LineNumber = l64->LineNumber;
1628 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1629 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1630 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1631 l64w->Address = l64->Address;
1634 /******************************************************************
1635 * copy_line_32_from_64 (internal)
1638 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1641 l32->Key = l64->Key;
1642 l32->LineNumber = l64->LineNumber;
1643 l32->FileName = l64->FileName;
1644 l32->Address = l64->Address;
1647 /******************************************************************
1648 * SymGetLineFromAddr (DBGHELP.@)
1651 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1652 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1654 IMAGEHLP_LINE64 il64;
1656 il64.SizeOfStruct = sizeof(il64);
1657 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1659 copy_line_32_from_64(Line, &il64);
1663 /******************************************************************
1664 * SymGetLineFromAddr64 (DBGHELP.@)
1667 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1668 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1670 struct module_pair pair;
1671 struct symt_ht* symt;
1673 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1675 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1677 pair.pcs = process_find_by_handle(hProcess);
1678 if (!pair.pcs) return FALSE;
1679 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1680 if (!module_get_debug(&pair)) return FALSE;
1681 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1683 if (symt->symt.tag != SymTagFunction) return FALSE;
1684 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1685 dwAddr, Line)) return FALSE;
1686 *pdwDisplacement = dwAddr - Line->Address;
1690 /******************************************************************
1691 * SymGetLineFromAddrW64 (DBGHELP.@)
1694 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1695 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1697 IMAGEHLP_LINE64 il64;
1699 il64.SizeOfStruct = sizeof(il64);
1700 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1702 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1706 /******************************************************************
1707 * SymGetLinePrev64 (DBGHELP.@)
1710 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1712 struct module_pair pair;
1713 struct line_info* li;
1714 BOOL in_search = FALSE;
1716 TRACE("(%p %p)\n", hProcess, Line);
1718 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1720 pair.pcs = process_find_by_handle(hProcess);
1721 if (!pair.pcs) return FALSE;
1722 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1723 if (!module_get_debug(&pair)) return FALSE;
1725 if (Line->Key == 0) return FALSE;
1727 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1728 * element we have to go back until we find the prev one to get the real
1729 * source file name for the DLIT_OFFSET element just before
1730 * the first DLIT_SOURCEFILE
1732 while (!li->is_first)
1735 if (!li->is_source_file)
1737 Line->LineNumber = li->line_number;
1738 Line->Address = li->u.pc_offset;
1740 if (!in_search) return TRUE;
1746 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1752 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1756 /******************************************************************
1757 * SymGetLinePrev (DBGHELP.@)
1760 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1762 IMAGEHLP_LINE64 line64;
1764 line64.SizeOfStruct = sizeof(line64);
1765 copy_line_64_from_32(&line64, Line);
1766 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1767 copy_line_32_from_64(Line, &line64);
1771 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1773 struct line_info* li;
1775 if (line->Key == 0) return FALSE;
1777 while (!li->is_last)
1780 if (!li->is_source_file)
1782 line->LineNumber = li->line_number;
1783 line->Address = li->u.pc_offset;
1787 line->FileName = (char*)source_get(module, li->u.source_file);
1792 /******************************************************************
1793 * SymGetLineNext64 (DBGHELP.@)
1796 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1798 struct module_pair pair;
1800 TRACE("(%p %p)\n", hProcess, Line);
1802 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1803 pair.pcs = process_find_by_handle(hProcess);
1804 if (!pair.pcs) return FALSE;
1805 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1806 if (!module_get_debug(&pair)) return FALSE;
1808 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1809 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1813 /******************************************************************
1814 * SymGetLineNext (DBGHELP.@)
1817 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1819 IMAGEHLP_LINE64 line64;
1821 line64.SizeOfStruct = sizeof(line64);
1822 copy_line_64_from_32(&line64, Line);
1823 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1824 copy_line_32_from_64(Line, &line64);
1828 /***********************************************************************
1829 * SymUnDName (DBGHELP.@)
1831 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1833 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1834 UNDNAME_COMPLETE) != 0;
1837 /***********************************************************************
1838 * SymUnDName64 (DBGHELP.@)
1840 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1842 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1843 UNDNAME_COMPLETE) != 0;
1846 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1847 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1849 /***********************************************************************
1850 * UnDecorateSymbolName (DBGHELP.@)
1852 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1853 DWORD UndecoratedLength, DWORD Flags)
1855 /* undocumented from msvcrt */
1856 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1857 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1859 TRACE("(%s, %p, %d, 0x%08x)\n",
1860 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1864 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1865 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1866 if (!p_undname) return 0;
1869 if (!UnDecoratedName) return 0;
1870 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1871 und_alloc, und_free, Flags))
1873 return strlen(UnDecoratedName);
1876 /******************************************************************
1877 * SymMatchStringA (DBGHELP.@)
1880 BOOL WINAPI SymMatchStringA(PCSTR string, PCSTR re, BOOL _case)
1885 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1887 compile_regex(re, -1, &preg, _case);
1888 ret = match_regexp(&preg, string);
1893 /******************************************************************
1894 * SymMatchStringW (DBGHELP.@)
1896 * FIXME: SymMatchStringA should convert and pass the strings to SymMatchStringW,
1897 * but that needs a unicode RE library.
1899 BOOL WINAPI SymMatchStringW(PCWSTR string, PCWSTR re, BOOL _case)
1905 TRACE("%s %s %c\n", debugstr_w(string), debugstr_w(re), _case ? 'Y' : 'N');
1907 len = WideCharToMultiByte( CP_ACP, 0, string, -1, NULL, 0, NULL, NULL );
1908 s = HeapAlloc( GetProcessHeap(), 0, len );
1909 WideCharToMultiByte( CP_ACP, 0, string, -1, s, len, NULL, NULL );
1911 len = WideCharToMultiByte( CP_ACP, 0, re, -1, NULL, 0, NULL, NULL );
1912 r = HeapAlloc( GetProcessHeap(), 0, len );
1913 WideCharToMultiByte( CP_ACP, 0, re, -1, r, len, NULL, NULL );
1915 ret = SymMatchStringA(s, r, _case);
1917 HeapFree( GetProcessHeap(), 0, r );
1918 HeapFree( GetProcessHeap(), 0, s );
1922 /******************************************************************
1923 * SymSearch (DBGHELP.@)
1925 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1926 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1927 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1928 PVOID UserContext, DWORD Options)
1932 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1933 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1934 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1935 UserContext, Options);
1937 if (Options != SYMSEARCH_GLOBALSONLY)
1939 FIXME("Unsupported searching with options (%x)\n", Options);
1940 SetLastError(ERROR_INVALID_PARAMETER);
1944 se.cb = EnumSymbolsCallback;
1945 se.user = UserContext;
1949 se.sym_info = (PSYMBOL_INFO)se.buffer;
1951 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1954 /******************************************************************
1955 * SymSearchW (DBGHELP.@)
1957 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1958 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1959 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1960 PVOID UserContext, DWORD Options)
1962 struct sym_enumW sew;
1966 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1967 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1968 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1969 UserContext, Options);
1971 sew.ctx = UserContext;
1972 sew.cb = EnumSymbolsCallback;
1973 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1977 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1978 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1979 if (!maskA) return FALSE;
1980 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1982 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1983 sym_enumW, &sew, Options);
1984 HeapFree(GetProcessHeap(), 0, maskA);
1989 /******************************************************************
1990 * SymAddSymbol (DBGHELP.@)
1993 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1994 DWORD64 addr, DWORD size, DWORD flags)
1996 WCHAR nameW[MAX_SYM_NAME];
1998 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1999 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
2002 /******************************************************************
2003 * SymAddSymbolW (DBGHELP.@)
2006 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
2007 DWORD64 addr, DWORD size, DWORD flags)
2009 struct module_pair pair;
2011 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
2013 pair.pcs = process_find_by_handle(hProcess);
2014 if (!pair.pcs) return FALSE;
2015 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
2016 if (!module_get_debug(&pair)) return FALSE;
2018 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2022 /******************************************************************
2023 * SymSetScopeFromAddr (DBGHELP.@)
2025 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
2027 struct process* pcs;
2029 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2031 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2035 /******************************************************************
2036 * SymEnumLines (DBGHELP.@)
2039 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2040 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2042 struct module_pair pair;
2043 struct hash_table_iter hti;
2044 struct symt_ht* sym;
2046 struct line_info* dli;
2051 if (!cb) return FALSE;
2052 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2054 pair.pcs = process_find_by_handle(hProcess);
2055 if (!pair.pcs) return FALSE;
2056 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2057 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2058 if (!module_get_debug(&pair)) return FALSE;
2059 if (!compile_file_regex(&re, srcfile)) return FALSE;
2061 sci.SizeOfStruct = sizeof(sci);
2064 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2065 while ((ptr = hash_table_iter_up(&hti)))
2069 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2070 if (sym->symt.tag != SymTagFunction) continue;
2072 sci.FileName[0] = '\0';
2073 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2075 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2076 if (dli->is_source_file)
2078 file = source_get(pair.effective, dli->u.source_file);
2079 if (!match_regexp(&re, file)) sci.FileName[0] = '\0';
2080 else strcpy(sci.FileName, file);
2082 else if (sci.FileName[0])
2085 sci.Obj[0] = '\0'; /* FIXME */
2086 sci.LineNumber = dli->line_number;
2087 sci.Address = dli->u.pc_offset;
2088 if (!cb(&sci, user)) break;
2096 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2097 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2099 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2100 dwLineNumber, plDisplacement, Line);
2104 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2105 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2107 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2108 dwLineNumber, lpDisplacement, Line);
2112 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2113 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2115 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2116 dwLineNumber, plDisplacement, Line);
2120 /******************************************************************
2121 * SymFromIndex (DBGHELP.@)
2124 BOOL WINAPI SymFromIndex(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFO symbol)
2126 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2127 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2132 /******************************************************************
2133 * SymFromIndexW (DBGHELP.@)
2136 BOOL WINAPI SymFromIndexW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFOW symbol)
2138 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2139 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);