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,
308 BOOL in_code, BOOL is_func)
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 sym->in_code = in_code;
326 sym->is_function = is_func;
327 symt_add_module_ht(module, (struct symt_ht*)sym);
330 p = vector_add(&compiland->vchildren, &module->pool);
337 struct symt_data* symt_new_global_variable(struct module* module,
338 struct symt_compiland* compiland,
339 const char* name, unsigned is_static,
340 unsigned long addr, unsigned long size,
343 struct symt_data* sym;
347 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
348 debugstr_w(module->module.ModuleName), name, addr, type);
349 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
351 sym->symt.tag = SymTagData;
352 sym->hash_elt.name = pool_strdup(&module->pool, name);
353 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
354 sym->container = compiland ? &compiland->symt : NULL;
356 sym->u.var.offset = addr;
357 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
360 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
361 debugstr_w(module->module.ModuleName), name,
362 wine_dbgstr_longlong(tsz), size);
364 symt_add_module_ht(module, (struct symt_ht*)sym);
367 p = vector_add(&compiland->vchildren, &module->pool);
374 struct symt_function* symt_new_function(struct module* module,
375 struct symt_compiland* compiland,
377 unsigned long addr, unsigned long size,
378 struct symt* sig_type)
380 struct symt_function* sym;
383 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
384 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
386 assert(!sig_type || sig_type->tag == SymTagFunctionType);
387 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
389 sym->symt.tag = SymTagFunction;
390 sym->hash_elt.name = pool_strdup(&module->pool, name);
391 sym->container = &compiland->symt;
393 sym->type = sig_type;
395 vector_init(&sym->vlines, sizeof(struct line_info), 64);
396 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
397 symt_add_module_ht(module, (struct symt_ht*)sym);
400 p = vector_add(&compiland->vchildren, &module->pool);
407 void symt_add_func_line(struct module* module, struct symt_function* func,
408 unsigned source_idx, int line_num, unsigned long offset)
410 struct line_info* dli;
411 BOOL last_matches = FALSE;
414 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
416 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
417 func, func->hash_elt.name, offset,
418 source_get(module, source_idx), line_num);
420 assert(func->symt.tag == SymTagFunction);
422 for (i=vector_length(&func->vlines)-1; i>=0; i--)
424 dli = vector_at(&func->vlines, i);
425 if (dli->is_source_file)
427 last_matches = (source_idx == dli->u.source_file);
434 /* we shouldn't have line changes on first line of function */
435 dli = vector_add(&func->vlines, &module->pool);
436 dli->is_source_file = 1;
437 dli->is_first = dli->is_last = 0;
438 dli->line_number = 0;
439 dli->u.source_file = source_idx;
441 dli = vector_add(&func->vlines, &module->pool);
442 dli->is_source_file = 0;
443 dli->is_first = dli->is_last = 0;
444 dli->line_number = line_num;
445 dli->u.pc_offset = func->address + offset;
448 /******************************************************************
449 * symt_add_func_local
451 * Adds a new local/parameter to a given function:
452 * In any cases, dt tells whether it's a local variable or a parameter
453 * If regno it's not 0:
454 * - then variable is stored in a register
455 * - otherwise, value is referenced by register + offset
456 * Otherwise, the variable is stored on the stack:
457 * - offset is then the offset from the frame register
459 struct symt_data* symt_add_func_local(struct module* module,
460 struct symt_function* func,
462 const struct location* loc,
463 struct symt_block* block,
464 struct symt* type, const char* name)
466 struct symt_data* locsym;
469 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
470 debugstr_w(module->module.ModuleName), func->hash_elt.name,
474 assert(func->symt.tag == SymTagFunction);
475 assert(dt == DataIsParam || dt == DataIsLocal);
477 locsym = pool_alloc(&module->pool, sizeof(*locsym));
478 locsym->symt.tag = SymTagData;
479 locsym->hash_elt.name = pool_strdup(&module->pool, name);
480 locsym->hash_elt.next = NULL;
482 locsym->container = &block->symt;
484 locsym->u.var = *loc;
486 p = vector_add(&block->vchildren, &module->pool);
488 p = vector_add(&func->vchildren, &module->pool);
494 struct symt_block* symt_open_func_block(struct module* module,
495 struct symt_function* func,
496 struct symt_block* parent_block,
497 unsigned pc, unsigned len)
499 struct symt_block* block;
503 assert(func->symt.tag == SymTagFunction);
505 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
506 block = pool_alloc(&module->pool, sizeof(*block));
507 block->symt.tag = SymTagBlock;
508 block->address = func->address + pc;
510 block->container = parent_block ? &parent_block->symt : &func->symt;
511 vector_init(&block->vchildren, sizeof(struct symt*), 4);
513 p = vector_add(&parent_block->vchildren, &module->pool);
515 p = vector_add(&func->vchildren, &module->pool);
521 struct symt_block* symt_close_func_block(struct module* module,
522 const struct symt_function* func,
523 struct symt_block* block, unsigned pc)
526 assert(func->symt.tag == SymTagFunction);
528 if (pc) block->size = func->address + pc - block->address;
529 return (block->container->tag == SymTagBlock) ?
530 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
533 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
534 struct symt_function* func,
535 enum SymTagEnum point,
536 const struct location* loc,
539 struct symt_hierarchy_point*sym;
542 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
544 sym->symt.tag = point;
545 sym->parent = &func->symt;
547 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
548 p = vector_add(&func->vchildren, &module->pool);
554 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
557 struct line_info* dli;
560 /* We aren't adding any more locals or line numbers to this function.
561 * Free any spare memory that we might have allocated.
563 assert(func->symt.tag == SymTagFunction);
565 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
566 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
568 len = vector_length(&func->vlines);
571 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
572 dli = vector_at(&func->vlines, len); dli->is_last = 1;
577 struct symt_thunk* symt_new_thunk(struct module* module,
578 struct symt_compiland* compiland,
579 const char* name, THUNK_ORDINAL ord,
580 unsigned long addr, unsigned long size)
582 struct symt_thunk* sym;
584 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
585 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
587 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
589 sym->symt.tag = SymTagThunk;
590 sym->hash_elt.name = pool_strdup(&module->pool, name);
591 sym->container = &compiland->symt;
595 symt_add_module_ht(module, (struct symt_ht*)sym);
599 p = vector_add(&compiland->vchildren, &module->pool);
606 struct symt_data* symt_new_constant(struct module* module,
607 struct symt_compiland* compiland,
608 const char* name, struct symt* type,
611 struct symt_data* sym;
613 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
614 debugstr_w(module->module.ModuleName), name);
616 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
618 sym->symt.tag = SymTagData;
619 sym->hash_elt.name = pool_strdup(&module->pool, name);
620 sym->kind = DataIsConstant;
621 sym->container = compiland ? &compiland->symt : NULL;
624 symt_add_module_ht(module, (struct symt_ht*)sym);
628 p = vector_add(&compiland->vchildren, &module->pool);
635 struct symt_hierarchy_point* symt_new_label(struct module* module,
636 struct symt_compiland* compiland,
637 const char* name, unsigned long address)
639 struct symt_hierarchy_point* sym;
641 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
642 debugstr_w(module->module.ModuleName), name);
644 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
646 sym->symt.tag = SymTagLabel;
647 sym->hash_elt.name = pool_strdup(&module->pool, name);
648 sym->loc.kind = loc_absolute;
649 sym->loc.offset = address;
650 sym->parent = compiland ? &compiland->symt : NULL;
651 symt_add_module_ht(module, (struct symt_ht*)sym);
655 p = vector_add(&compiland->vchildren, &module->pool);
662 /* expect sym_info->MaxNameLen to be set before being called */
663 static void symt_fill_sym_info(struct module_pair* pair,
664 const struct symt_function* func,
665 const struct symt* sym, SYMBOL_INFO* sym_info)
670 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
671 sym_info->TypeIndex = 0;
672 sym_info->info = symt_ptr2index(pair->effective, sym);
673 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
674 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
675 (!sym_info->TypeIndex ||
676 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
677 TI_GET_LENGTH, &size)))
679 sym_info->Size = (DWORD)size;
680 sym_info->ModBase = pair->requested->module.BaseOfImage;
688 const struct symt_data* data = (const struct symt_data*)sym;
692 sym_info->Flags |= SYMFLAG_PARAMETER;
696 struct location loc = data->u.var;
698 if (loc.kind >= loc_user)
699 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
704 /* for now we report error cases as a negative register number */
705 sym_info->Flags |= SYMFLAG_LOCAL;
708 sym_info->Flags |= SYMFLAG_REGISTER;
709 sym_info->Register = loc.reg;
710 sym_info->Address = 0;
713 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
714 /* FIXME: it's i386 dependent !!! */
715 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
716 sym_info->Address = loc.offset;
719 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
725 case DataIsFileStatic:
726 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
727 sym_info->Register = 0;
730 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
731 switch (data->u.value.n1.n2.vt)
733 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
734 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
735 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
736 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
737 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
738 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
739 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
740 case VT_EMPTY: sym_info->Value = 0; break;
742 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
748 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
752 case SymTagPublicSymbol:
753 sym_info->Flags |= SYMFLAG_EXPORT;
754 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
757 sym_info->Flags |= SYMFLAG_FUNCTION;
758 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
761 sym_info->Flags |= SYMFLAG_THUNK;
762 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
765 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
766 sym_info->Register = 0;
769 sym_info->Scope = 0; /* FIXME */
770 sym_info->Tag = sym->tag;
771 name = symt_get_name(sym);
772 if (sym_info->MaxNameLen)
774 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
775 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
776 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
778 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
779 memcpy(sym_info->Name, name, sym_info->NameLen);
780 sym_info->Name[sym_info->NameLen] = '\0';
783 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
784 sym, sym_info->Name, sym_info->Size,
785 wine_dbgstr_longlong(sym_info->Address));
790 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
792 SYMBOL_INFO* sym_info;
796 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
799 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
800 const struct symt_function* func, const struct symt* sym)
802 symt_fill_sym_info(pair, func, sym, se->sym_info);
803 if (se->index && se->sym_info->info != se->index) return FALSE;
804 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
805 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
806 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
809 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
810 const struct sym_enum* se)
813 struct symt_ht* sym = NULL;
814 struct hash_table_iter hti;
816 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
817 while ((ptr = hash_table_iter_up(&hti)))
819 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
820 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
822 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
823 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
824 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
830 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
832 unsigned low = 0, mid = high / 2;
836 symt_get_info(module, &elt->symt, TI_GET_ADDRESS, &addr);
839 switch (cmp_sorttab_addr(module, mid, addr))
842 case -1: low = mid + 1; break;
843 case 1: high = mid; break;
845 mid = low + (high - low) / 2;
846 } while (low < high);
850 /***********************************************************************
853 * Rebuild sorted list of symbols for a module.
855 static BOOL resort_symbols(struct module* module)
857 if (!(module->module.NumSyms = module->num_symbols))
860 /* FIXME: what's the optimal value here ??? */
861 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
863 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
864 struct symt_ht* tmp[30];
866 delta = module->num_symbols - module->num_sorttab;
867 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
868 symt_cmp_addr_module = module;
869 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
871 for (i = delta - 1; i >= 0; i--)
873 prev_ins_idx = ins_idx;
874 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
875 memmove(&module->addr_sorttab[ins_idx + i + 1],
876 &module->addr_sorttab[ins_idx],
877 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
878 module->addr_sorttab[ins_idx + i] = tmp[i];
883 symt_cmp_addr_module = module;
884 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
886 module->num_sorttab = module->num_symbols;
887 return module->sortlist_valid = TRUE;
890 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
894 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
897 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
898 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
899 *size = 0x1000; /* arbitrary value */
902 /* assume addr is in module */
903 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
906 ULONG64 ref_addr, ref_size;
908 if (!module->sortlist_valid || !module->addr_sorttab)
910 if (!resort_symbols(module)) return NULL;
914 * Binary search to find closest symbol.
917 high = module->num_sorttab;
919 symt_get_info(module, &module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
920 if (addr < ref_addr) return NULL;
923 symt_get_info(module, &module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
924 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
925 if (addr >= ref_addr + ref_size) return NULL;
928 while (high > low + 1)
930 mid = (high + low) / 2;
931 if (cmp_sorttab_addr(module, mid, addr) < 0)
936 if (low != high && high != module->num_sorttab &&
937 cmp_sorttab_addr(module, high, addr) <= 0)
940 /* If found symbol is a public symbol, check if there are any other entries that
941 * might also have the same address, but would get better information
943 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
945 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
947 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
948 !cmp_sorttab_addr(module, low - 1, ref_addr))
950 else if (low < module->num_sorttab - 1 &&
951 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
952 !cmp_sorttab_addr(module, low + 1, ref_addr))
955 /* finally check that we fit into the found symbol */
956 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
957 if (addr < ref_addr) return NULL;
958 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
959 if (addr >= ref_addr + ref_size) return NULL;
961 return module->addr_sorttab[low];
964 static BOOL symt_enum_locals_helper(struct module_pair* pair,
965 regex_t* preg, const struct sym_enum* se,
966 struct symt_function* func, const struct vector* v)
968 struct symt* lsym = NULL;
969 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
972 for (i=0; i<vector_length(v); i++)
974 lsym = *(struct symt**)vector_at(v, i);
979 struct symt_block* block = (struct symt_block*)lsym;
980 if (pc < block->address || block->address + block->size <= pc)
982 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
987 if (match_regexp(preg, symt_get_name(lsym)))
989 if (send_symbol(se, pair, func, lsym)) return FALSE;
993 case SymTagFuncDebugStart:
994 case SymTagFuncDebugEnd:
998 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1005 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
1006 const struct sym_enum* se)
1008 struct module_pair pair;
1009 struct symt_ht* sym;
1010 DWORD pc = pcs->ctx_frame.InstructionOffset;
1012 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1013 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1016 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1017 if (!module_get_debug(&pair)) return FALSE;
1018 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1020 if (sym->symt.tag == SymTagFunction)
1025 compile_regex(mask ? mask : "*", -1, &preg,
1026 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1027 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1028 &((struct symt_function*)sym)->vchildren);
1033 return send_symbol(se, &pair, NULL, &sym->symt);
1036 /******************************************************************
1039 * Helper for transforming an ANSI symbol info into a UNICODE one.
1040 * Assume that MaxNameLen is the same for both version (A & W).
1042 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1044 siw->SizeOfStruct = si->SizeOfStruct;
1045 siw->TypeIndex = si->TypeIndex;
1046 siw->Reserved[0] = si->Reserved[0];
1047 siw->Reserved[1] = si->Reserved[1];
1048 siw->Index = si->info; /* FIXME: see dbghelp.h */
1049 siw->Size = si->Size;
1050 siw->ModBase = si->ModBase;
1051 siw->Flags = si->Flags;
1052 siw->Value = si->Value;
1053 siw->Address = si->Address;
1054 siw->Register = si->Register;
1055 siw->Scope = si->Scope;
1057 siw->NameLen = si->NameLen;
1058 siw->MaxNameLen = si->MaxNameLen;
1059 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1062 /******************************************************************
1065 * Core routine for most of the enumeration of symbols
1067 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1068 const struct sym_enum* se)
1070 struct module_pair pair;
1072 regex_t mod_regex, sym_regex;
1074 pair.pcs = process_find_by_handle(hProcess);
1075 if (!pair.pcs) return FALSE;
1078 /* do local variables ? */
1079 if (!Mask || !(bang = strchr(Mask, '!')))
1080 return symt_enum_locals(pair.pcs, Mask, se);
1082 if (bang == Mask) return FALSE;
1084 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1085 compile_regex(bang + 1, -1, &sym_regex,
1086 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1088 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1090 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1092 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1093 symt_enum_module(&pair, &sym_regex, se))
1097 /* not found in PE modules, retry on the ELF ones
1099 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1101 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1103 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1104 !module_get_containee(pair.pcs, pair.requested) &&
1105 module_get_debug(&pair))
1107 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1108 symt_enum_module(&pair, &sym_regex, se))
1113 regfree(&mod_regex);
1114 regfree(&sym_regex);
1117 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1118 if (!module_get_debug(&pair))
1121 /* we always ignore module name from Mask when BaseOfDll is defined */
1122 if (Mask && (bang = strchr(Mask, '!')))
1124 if (bang == Mask) return FALSE;
1128 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1129 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1130 symt_enum_module(&pair, &sym_regex, se);
1131 regfree(&sym_regex);
1136 /******************************************************************
1137 * SymEnumSymbols (DBGHELP.@)
1139 * cases BaseOfDll = 0
1140 * !foo fails always (despite what MSDN states)
1141 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1142 * no ! in Mask, lookup in local Context
1143 * cases BaseOfDll != 0
1144 * !foo fails always (despite what MSDN states)
1145 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1147 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1148 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1153 TRACE("(%p %s %s %p %p)\n",
1154 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1155 EnumSymbolsCallback, UserContext);
1157 se.cb = EnumSymbolsCallback;
1158 se.user = UserContext;
1162 se.sym_info = (PSYMBOL_INFO)se.buffer;
1164 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1169 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1171 PSYMBOL_INFOW sym_info;
1172 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1176 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1178 struct sym_enumW* sew = ctx;
1180 copy_symbolW(sew->sym_info, si);
1182 return (sew->cb)(sew->sym_info, size, sew->ctx);
1185 /******************************************************************
1186 * SymEnumSymbolsW (DBGHELP.@)
1189 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1190 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1193 struct sym_enumW sew;
1197 sew.ctx = UserContext;
1198 sew.cb = EnumSymbolsCallback;
1199 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1203 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1204 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1205 if (!maskA) return FALSE;
1206 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1208 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1209 HeapFree(GetProcessHeap(), 0, maskA);
1214 struct sym_enumerate
1217 PSYM_ENUMSYMBOLS_CALLBACK cb;
1220 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1222 struct sym_enumerate* se = ctx;
1223 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1226 /***********************************************************************
1227 * SymEnumerateSymbols (DBGHELP.@)
1229 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1230 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1233 struct sym_enumerate se;
1235 se.ctx = UserContext;
1236 se.cb = EnumSymbolsCallback;
1238 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1241 struct sym_enumerate64
1244 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1247 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1249 struct sym_enumerate64* se = ctx;
1250 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1253 /***********************************************************************
1254 * SymEnumerateSymbols64 (DBGHELP.@)
1256 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1257 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1260 struct sym_enumerate64 se;
1262 se.ctx = UserContext;
1263 se.cb = EnumSymbolsCallback;
1265 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1268 /******************************************************************
1269 * SymFromAddr (DBGHELP.@)
1272 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1273 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1275 struct module_pair pair;
1276 struct symt_ht* sym;
1278 pair.pcs = process_find_by_handle(hProcess);
1279 if (!pair.pcs) return FALSE;
1280 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1281 if (!module_get_debug(&pair)) return FALSE;
1282 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1284 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1285 *Displacement = Address - Symbol->Address;
1289 /******************************************************************
1290 * SymFromAddrW (DBGHELP.@)
1293 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1294 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1300 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1301 si = HeapAlloc(GetProcessHeap(), 0, len);
1302 if (!si) return FALSE;
1304 si->SizeOfStruct = sizeof(*si);
1305 si->MaxNameLen = Symbol->MaxNameLen;
1306 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1308 copy_symbolW(Symbol, si);
1310 HeapFree(GetProcessHeap(), 0, si);
1314 /******************************************************************
1315 * SymGetSymFromAddr (DBGHELP.@)
1318 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1319 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1321 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1322 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1324 DWORD64 Displacement64;
1326 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1327 si->SizeOfStruct = sizeof(*si);
1328 si->MaxNameLen = MAX_SYM_NAME;
1329 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1333 *Displacement = Displacement64;
1334 Symbol->Address = si->Address;
1335 Symbol->Size = si->Size;
1336 Symbol->Flags = si->Flags;
1337 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1338 lstrcpynA(Symbol->Name, si->Name, len);
1342 /******************************************************************
1343 * SymGetSymFromAddr64 (DBGHELP.@)
1346 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1347 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1349 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1350 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1352 DWORD64 Displacement64;
1354 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1355 si->SizeOfStruct = sizeof(*si);
1356 si->MaxNameLen = MAX_SYM_NAME;
1357 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1361 *Displacement = Displacement64;
1362 Symbol->Address = si->Address;
1363 Symbol->Size = si->Size;
1364 Symbol->Flags = si->Flags;
1365 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1366 lstrcpynA(Symbol->Name, si->Name, len);
1370 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1371 SYMBOL_INFO* symbol)
1373 struct hash_table_iter hti;
1375 struct symt_ht* sym = NULL;
1376 struct module_pair pair;
1379 if (!(pair.requested = module)) return FALSE;
1380 if (!module_get_debug(&pair)) return FALSE;
1382 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1383 while ((ptr = hash_table_iter_up(&hti)))
1385 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1387 if (!strcmp(sym->hash_elt.name, name))
1389 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1396 /******************************************************************
1397 * SymFromName (DBGHELP.@)
1400 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1402 struct process* pcs = process_find_by_handle(hProcess);
1403 struct module* module;
1406 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1407 if (!pcs) return FALSE;
1408 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1409 name = strchr(Name, '!');
1413 assert(name - Name < sizeof(tmp));
1414 memcpy(tmp, Name, name - Name);
1415 tmp[name - Name] = '\0';
1416 module = module_find_by_nameA(pcs, tmp);
1417 return find_name(pcs, module, name + 1, Symbol);
1419 for (module = pcs->lmodules; module; module = module->next)
1421 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1424 /* not found in PE modules, retry on the ELF ones
1426 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1428 for (module = pcs->lmodules; module; module = module->next)
1430 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1431 !module_get_containee(pcs, module) &&
1432 find_name(pcs, module, Name, Symbol))
1439 /***********************************************************************
1440 * SymGetSymFromName64 (DBGHELP.@)
1442 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1444 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1445 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1448 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1449 si->SizeOfStruct = sizeof(*si);
1450 si->MaxNameLen = MAX_SYM_NAME;
1451 if (!SymFromName(hProcess, Name, si)) return FALSE;
1453 Symbol->Address = si->Address;
1454 Symbol->Size = si->Size;
1455 Symbol->Flags = si->Flags;
1456 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1457 lstrcpynA(Symbol->Name, si->Name, len);
1461 /***********************************************************************
1462 * SymGetSymFromName (DBGHELP.@)
1464 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1466 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1467 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1470 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1471 si->SizeOfStruct = sizeof(*si);
1472 si->MaxNameLen = MAX_SYM_NAME;
1473 if (!SymFromName(hProcess, Name, si)) return FALSE;
1475 Symbol->Address = si->Address;
1476 Symbol->Size = si->Size;
1477 Symbol->Flags = si->Flags;
1478 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1479 lstrcpynA(Symbol->Name, si->Name, len);
1483 /******************************************************************
1484 * sym_fill_func_line_info
1486 * fills information about a file
1488 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1489 DWORD64 addr, IMAGEHLP_LINE64* line)
1491 struct line_info* dli = NULL;
1495 assert(func->symt.tag == SymTagFunction);
1497 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1499 dli = vector_at(&func->vlines, i);
1500 if (!dli->is_source_file)
1502 if (found || dli->u.pc_offset > addr) continue;
1503 line->LineNumber = dli->line_number;
1504 line->Address = dli->u.pc_offset;
1511 line->FileName = (char*)source_get(module, dli->u.source_file);
1518 /***********************************************************************
1519 * SymGetSymNext64 (DBGHELP.@)
1521 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1524 * get module from Symbol.Address
1525 * get index in module.addr_sorttab of Symbol.Address
1527 * if out of module bounds, move to next module in process address space
1529 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1530 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1534 /***********************************************************************
1535 * SymGetSymNext (DBGHELP.@)
1537 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1539 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1540 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1544 /***********************************************************************
1545 * SymGetSymPrev64 (DBGHELP.@)
1547 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1549 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1550 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1554 /***********************************************************************
1555 * SymGetSymPrev (DBGHELP.@)
1557 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1559 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1560 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1564 /******************************************************************
1565 * copy_line_64_from_32 (internal)
1568 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1571 l64->Key = l32->Key;
1572 l64->LineNumber = l32->LineNumber;
1573 l64->FileName = l32->FileName;
1574 l64->Address = l32->Address;
1577 /******************************************************************
1578 * copy_line_W64_from_32 (internal)
1581 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1585 l64w->Key = l64->Key;
1586 l64w->LineNumber = l64->LineNumber;
1587 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1588 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1589 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1590 l64w->Address = l64->Address;
1593 /******************************************************************
1594 * copy_line_32_from_64 (internal)
1597 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1600 l32->Key = l64->Key;
1601 l32->LineNumber = l64->LineNumber;
1602 l32->FileName = l64->FileName;
1603 l32->Address = l64->Address;
1606 /******************************************************************
1607 * SymGetLineFromAddr (DBGHELP.@)
1610 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1611 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1613 IMAGEHLP_LINE64 il64;
1615 il64.SizeOfStruct = sizeof(il64);
1616 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1618 copy_line_32_from_64(Line, &il64);
1622 /******************************************************************
1623 * SymGetLineFromAddr64 (DBGHELP.@)
1626 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1627 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1629 struct module_pair pair;
1630 struct symt_ht* symt;
1632 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1634 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1636 pair.pcs = process_find_by_handle(hProcess);
1637 if (!pair.pcs) return FALSE;
1638 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1639 if (!module_get_debug(&pair)) return FALSE;
1640 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1642 if (symt->symt.tag != SymTagFunction) return FALSE;
1643 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1644 dwAddr, Line)) return FALSE;
1645 *pdwDisplacement = dwAddr - Line->Address;
1649 /******************************************************************
1650 * SymGetLineFromAddrW64 (DBGHELP.@)
1653 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1654 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1656 IMAGEHLP_LINE64 il64;
1658 il64.SizeOfStruct = sizeof(il64);
1659 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1661 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1665 /******************************************************************
1666 * SymGetLinePrev64 (DBGHELP.@)
1669 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1671 struct module_pair pair;
1672 struct line_info* li;
1673 BOOL in_search = FALSE;
1675 TRACE("(%p %p)\n", hProcess, Line);
1677 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1679 pair.pcs = process_find_by_handle(hProcess);
1680 if (!pair.pcs) return FALSE;
1681 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1682 if (!module_get_debug(&pair)) return FALSE;
1684 if (Line->Key == 0) return FALSE;
1686 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1687 * element we have to go back until we find the prev one to get the real
1688 * source file name for the DLIT_OFFSET element just before
1689 * the first DLIT_SOURCEFILE
1691 while (!li->is_first)
1694 if (!li->is_source_file)
1696 Line->LineNumber = li->line_number;
1697 Line->Address = li->u.pc_offset;
1699 if (!in_search) return TRUE;
1705 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1711 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1715 /******************************************************************
1716 * SymGetLinePrev (DBGHELP.@)
1719 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1721 IMAGEHLP_LINE64 line64;
1723 line64.SizeOfStruct = sizeof(line64);
1724 copy_line_64_from_32(&line64, Line);
1725 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1726 copy_line_32_from_64(Line, &line64);
1730 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1732 struct line_info* li;
1734 if (line->Key == 0) return FALSE;
1736 while (!li->is_last)
1739 if (!li->is_source_file)
1741 line->LineNumber = li->line_number;
1742 line->Address = li->u.pc_offset;
1746 line->FileName = (char*)source_get(module, li->u.source_file);
1751 /******************************************************************
1752 * SymGetLineNext64 (DBGHELP.@)
1755 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1757 struct module_pair pair;
1759 TRACE("(%p %p)\n", hProcess, Line);
1761 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1762 pair.pcs = process_find_by_handle(hProcess);
1763 if (!pair.pcs) return FALSE;
1764 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1765 if (!module_get_debug(&pair)) return FALSE;
1767 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1768 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1772 /******************************************************************
1773 * SymGetLineNext (DBGHELP.@)
1776 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1778 IMAGEHLP_LINE64 line64;
1780 line64.SizeOfStruct = sizeof(line64);
1781 copy_line_64_from_32(&line64, Line);
1782 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1783 copy_line_32_from_64(Line, &line64);
1787 /***********************************************************************
1788 * SymFunctionTableAccess (DBGHELP.@)
1790 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1792 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1796 /***********************************************************************
1797 * SymFunctionTableAccess64 (DBGHELP.@)
1799 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1801 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1805 /***********************************************************************
1806 * SymUnDName (DBGHELP.@)
1808 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1810 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1811 UNDNAME_COMPLETE) != 0;
1814 /***********************************************************************
1815 * SymUnDName64 (DBGHELP.@)
1817 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1819 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1820 UNDNAME_COMPLETE) != 0;
1823 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1824 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1826 /***********************************************************************
1827 * UnDecorateSymbolName (DBGHELP.@)
1829 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1830 DWORD UndecoratedLength, DWORD Flags)
1832 /* undocumented from msvcrt */
1833 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1834 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1836 TRACE("(%s, %p, %d, 0x%08x)\n",
1837 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1841 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1842 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1843 if (!p_undname) return 0;
1846 if (!UnDecoratedName) return 0;
1847 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1848 und_alloc, und_free, Flags))
1850 return strlen(UnDecoratedName);
1853 /******************************************************************
1854 * SymMatchString (DBGHELP.@)
1857 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1862 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1864 compile_regex(re, -1, &preg, _case);
1865 ret = match_regexp(&preg, string);
1870 /******************************************************************
1871 * SymSearch (DBGHELP.@)
1873 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1874 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1875 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1876 PVOID UserContext, DWORD Options)
1880 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1881 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1882 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1883 UserContext, Options);
1885 if (Options != SYMSEARCH_GLOBALSONLY)
1887 FIXME("Unsupported searching with options (%x)\n", Options);
1888 SetLastError(ERROR_INVALID_PARAMETER);
1892 se.cb = EnumSymbolsCallback;
1893 se.user = UserContext;
1897 se.sym_info = (PSYMBOL_INFO)se.buffer;
1899 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1902 /******************************************************************
1903 * SymSearchW (DBGHELP.@)
1905 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1906 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1907 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1908 PVOID UserContext, DWORD Options)
1910 struct sym_enumW sew;
1914 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1915 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1916 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1917 UserContext, Options);
1919 sew.ctx = UserContext;
1920 sew.cb = EnumSymbolsCallback;
1921 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1925 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1926 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1927 if (!maskA) return FALSE;
1928 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1930 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1931 sym_enumW, &sew, Options);
1932 HeapFree(GetProcessHeap(), 0, maskA);
1937 /******************************************************************
1938 * SymAddSymbol (DBGHELP.@)
1941 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1942 DWORD64 addr, DWORD size, DWORD flags)
1944 WCHAR nameW[MAX_SYM_NAME];
1946 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1947 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1950 /******************************************************************
1951 * SymAddSymbolW (DBGHELP.@)
1954 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1955 DWORD64 addr, DWORD size, DWORD flags)
1957 struct module_pair pair;
1959 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1961 pair.pcs = process_find_by_handle(hProcess);
1962 if (!pair.pcs) return FALSE;
1963 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1964 if (!module_get_debug(&pair)) return FALSE;
1966 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1970 /******************************************************************
1971 * SymSetScopeFromAddr (DBGHELP.@)
1973 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1975 struct process* pcs;
1977 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1979 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1983 /******************************************************************
1984 * SymEnumLines (DBGHELP.@)
1987 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1988 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1990 struct module_pair pair;
1991 struct hash_table_iter hti;
1992 struct symt_ht* sym;
1994 struct line_info* dli;
1999 if (!cb) return FALSE;
2000 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2002 pair.pcs = process_find_by_handle(hProcess);
2003 if (!pair.pcs) return FALSE;
2004 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2005 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2006 if (!module_get_debug(&pair)) return FALSE;
2007 if (!compile_file_regex(&re, srcfile)) return FALSE;
2009 sci.SizeOfStruct = sizeof(sci);
2012 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2013 while ((ptr = hash_table_iter_up(&hti)))
2017 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2018 if (sym->symt.tag != SymTagFunction) continue;
2020 sci.FileName[0] = '\0';
2021 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2023 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2024 if (dli->is_source_file)
2026 file = source_get(pair.effective, dli->u.source_file);
2027 if (!match_regexp(&re, file)) file = "";
2028 strcpy(sci.FileName, file);
2030 else if (sci.FileName[0])
2033 sci.Obj[0] = '\0'; /* FIXME */
2034 sci.LineNumber = dli->line_number;
2035 sci.Address = dli->u.pc_offset;
2036 if (!cb(&sci, user)) break;