2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include <sys/types.h>
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
44 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
46 if (a1 > a2) return 1;
47 if (a1 < a2) return -1;
51 static inline int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
55 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
56 return cmp_addr(ref, addr);
59 int symt_cmp_addr(const void* p1, const void* p2)
61 const struct symt* sym1 = *(const struct symt* const *)p1;
62 const struct symt* sym2 = *(const struct symt* const *)p2;
65 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
66 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
67 return cmp_addr(a1, a2);
70 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
75 if (sz <= module->sorttab_size) return TRUE;
76 if (module->addr_sorttab)
78 size = module->sorttab_size * 2;
79 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
80 size * sizeof(struct symt_ht*));
85 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
87 if (!new) return FALSE;
88 module->sorttab_size = size;
89 module->addr_sorttab = new;
93 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
97 hash_table_add(&module->ht_symbols, &ht->hash_elt);
98 /* Don't store in sorttab a symbol without address, they are of
99 * no use here (e.g. constant values)
101 if (symt_get_info(&ht->symt, TI_GET_ADDRESS, &addr) &&
102 symt_grow_sorttab(module, module->num_symbols + 1))
104 module->addr_sorttab[module->num_symbols++] = ht;
105 module->sortlist_valid = FALSE;
111 /* transforms a dbghelp's regular expression into a POSIX one
112 * Here are the valid dbghelp reg ex characters:
113 * * 0 or more characters
114 * ? a single character
116 * # 0 or more of preceding char
117 * + 1 or more of preceding char
118 * escapes \ on #, ?, [, ], *, +. don't work on -
120 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
123 BOOL in_escape = FALSE;
124 unsigned flags = REG_NOSUB;
126 if (numchar == -1) numchar = strlen( str );
128 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
131 while (*str && numchar--)
133 /* FIXME: this shouldn't be valid on '-' */
142 case '\\': in_escape = TRUE; break;
143 case '*': *p++ = '.'; *p++ = '*'; break;
144 case '?': *p++ = '.'; break;
145 case '#': *p++ = '*'; break;
146 /* escape some valid characters in dbghelp reg exp:s */
147 case '$': *p++ = '\\'; *p++ = '$'; break;
148 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
149 default: *p++ = *str; break;
160 if (_case) flags |= REG_ICASE;
161 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
162 HeapFree(GetProcessHeap(), 0, mask);
165 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
170 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
172 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
198 ret = !regcomp(re, mask, REG_NOSUB);
199 HeapFree(GetProcessHeap(), 0, mask);
202 FIXME("Couldn't compile %s\n", mask);
203 SetLastError(ERROR_INVALID_PARAMETER);
208 static int match_regexp( const regex_t *re, const char *str )
210 return !regexec( re, str, 0, NULL, 0 );
213 #else /* HAVE_REGEX_H */
215 /* if we don't have regexp support, fall back to a simple string comparison */
223 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
225 if (numchar == -1) numchar = strlen( str );
227 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
228 memcpy( re->str, str, numchar );
229 re->str[numchar] = 0;
233 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
235 if (!srcfile || !*srcfile) re->str = NULL;
236 else compile_regex( srcfile, -1, re, FALSE );
240 static int match_regexp( const regex_t *re, const char *str )
242 if (!re->str) return 1;
243 if (re->icase) return !lstrcmpiA( re->str, str );
244 return !strcmp( re->str, str );
247 static void regfree( regex_t *re )
249 HeapFree( GetProcessHeap(), 0, re->str );
252 #endif /* HAVE_REGEX_H */
254 struct symt_compiland* symt_new_compiland(struct module* module,
255 unsigned long address, unsigned src_idx)
257 struct symt_compiland* sym;
259 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
260 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
261 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
263 sym->symt.tag = SymTagCompiland;
264 sym->address = address;
265 sym->source = src_idx;
266 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
271 struct symt_public* symt_new_public(struct module* module,
272 struct symt_compiland* compiland,
274 unsigned long address, unsigned size,
275 BOOL in_code, BOOL is_func)
277 struct symt_public* sym;
280 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
281 debugstr_w(module->module.ModuleName), name, address);
282 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
283 symt_find_nearest(module, address) != NULL)
285 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
287 sym->symt.tag = SymTagPublicSymbol;
288 sym->hash_elt.name = pool_strdup(&module->pool, name);
289 sym->container = compiland ? &compiland->symt : NULL;
290 sym->address = address;
292 sym->in_code = in_code;
293 sym->is_function = is_func;
294 symt_add_module_ht(module, (struct symt_ht*)sym);
297 p = vector_add(&compiland->vchildren, &module->pool);
304 struct symt_data* symt_new_global_variable(struct module* module,
305 struct symt_compiland* compiland,
306 const char* name, unsigned is_static,
307 unsigned long addr, unsigned long size,
310 struct symt_data* sym;
314 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
315 debugstr_w(module->module.ModuleName), name, addr, type);
316 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
318 sym->symt.tag = SymTagData;
319 sym->hash_elt.name = pool_strdup(&module->pool, name);
320 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
321 sym->container = compiland ? &compiland->symt : NULL;
323 sym->u.var.offset = addr;
324 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
327 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
328 debugstr_w(module->module.ModuleName), name,
329 wine_dbgstr_longlong(tsz), size);
331 symt_add_module_ht(module, (struct symt_ht*)sym);
334 p = vector_add(&compiland->vchildren, &module->pool);
341 struct symt_function* symt_new_function(struct module* module,
342 struct symt_compiland* compiland,
344 unsigned long addr, unsigned long size,
345 struct symt* sig_type)
347 struct symt_function* sym;
350 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
351 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
353 assert(!sig_type || sig_type->tag == SymTagFunctionType);
354 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
356 sym->symt.tag = SymTagFunction;
357 sym->hash_elt.name = pool_strdup(&module->pool, name);
358 sym->container = &compiland->symt;
360 sym->type = sig_type;
362 vector_init(&sym->vlines, sizeof(struct line_info), 64);
363 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
364 symt_add_module_ht(module, (struct symt_ht*)sym);
367 p = vector_add(&compiland->vchildren, &module->pool);
374 void symt_add_func_line(struct module* module, struct symt_function* func,
375 unsigned source_idx, int line_num, unsigned long offset)
377 struct line_info* dli;
378 BOOL last_matches = FALSE;
381 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
383 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
384 func, func->hash_elt.name, offset,
385 source_get(module, source_idx), line_num);
387 assert(func->symt.tag == SymTagFunction);
389 for (i=vector_length(&func->vlines)-1; i>=0; i--)
391 dli = vector_at(&func->vlines, i);
392 if (dli->is_source_file)
394 last_matches = (source_idx == dli->u.source_file);
401 /* we shouldn't have line changes on first line of function */
402 dli = vector_add(&func->vlines, &module->pool);
403 dli->is_source_file = 1;
404 dli->is_first = dli->is_last = 0;
405 dli->line_number = 0;
406 dli->u.source_file = source_idx;
408 dli = vector_add(&func->vlines, &module->pool);
409 dli->is_source_file = 0;
410 dli->is_first = dli->is_last = 0;
411 dli->line_number = line_num;
412 dli->u.pc_offset = func->address + offset;
415 /******************************************************************
416 * symt_add_func_local
418 * Adds a new local/parameter to a given function:
419 * In any cases, dt tells whether it's a local variable or a parameter
420 * If regno it's not 0:
421 * - then variable is stored in a register
422 * - otherwise, value is referenced by register + offset
423 * Otherwise, the variable is stored on the stack:
424 * - offset is then the offset from the frame register
426 struct symt_data* symt_add_func_local(struct module* module,
427 struct symt_function* func,
429 const struct location* loc,
430 struct symt_block* block,
431 struct symt* type, const char* name)
433 struct symt_data* locsym;
436 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
437 debugstr_w(module->module.ModuleName), func->hash_elt.name,
441 assert(func->symt.tag == SymTagFunction);
442 assert(dt == DataIsParam || dt == DataIsLocal);
444 locsym = pool_alloc(&module->pool, sizeof(*locsym));
445 locsym->symt.tag = SymTagData;
446 locsym->hash_elt.name = pool_strdup(&module->pool, name);
447 locsym->hash_elt.next = NULL;
449 locsym->container = &block->symt;
451 locsym->u.var = *loc;
453 p = vector_add(&block->vchildren, &module->pool);
455 p = vector_add(&func->vchildren, &module->pool);
461 struct symt_block* symt_open_func_block(struct module* module,
462 struct symt_function* func,
463 struct symt_block* parent_block,
464 unsigned pc, unsigned len)
466 struct symt_block* block;
470 assert(func->symt.tag == SymTagFunction);
472 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
473 block = pool_alloc(&module->pool, sizeof(*block));
474 block->symt.tag = SymTagBlock;
475 block->address = func->address + pc;
477 block->container = parent_block ? &parent_block->symt : &func->symt;
478 vector_init(&block->vchildren, sizeof(struct symt*), 4);
480 p = vector_add(&parent_block->vchildren, &module->pool);
482 p = vector_add(&func->vchildren, &module->pool);
488 struct symt_block* symt_close_func_block(struct module* module,
489 const struct symt_function* func,
490 struct symt_block* block, unsigned pc)
493 assert(func->symt.tag == SymTagFunction);
495 if (pc) block->size = func->address + pc - block->address;
496 return (block->container->tag == SymTagBlock) ?
497 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
500 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
501 struct symt_function* func,
502 enum SymTagEnum point,
503 const struct location* loc,
506 struct symt_hierarchy_point*sym;
509 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
511 sym->symt.tag = point;
512 sym->parent = &func->symt;
514 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
515 p = vector_add(&func->vchildren, &module->pool);
521 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
524 struct line_info* dli;
527 /* We aren't adding any more locals or line numbers to this function.
528 * Free any spare memory that we might have allocated.
530 assert(func->symt.tag == SymTagFunction);
532 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
533 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
535 len = vector_length(&func->vlines);
538 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
539 dli = vector_at(&func->vlines, len); dli->is_last = 1;
544 struct symt_thunk* symt_new_thunk(struct module* module,
545 struct symt_compiland* compiland,
546 const char* name, THUNK_ORDINAL ord,
547 unsigned long addr, unsigned long size)
549 struct symt_thunk* sym;
551 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
552 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
554 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
556 sym->symt.tag = SymTagThunk;
557 sym->hash_elt.name = pool_strdup(&module->pool, name);
558 sym->container = &compiland->symt;
562 symt_add_module_ht(module, (struct symt_ht*)sym);
566 p = vector_add(&compiland->vchildren, &module->pool);
573 struct symt_data* symt_new_constant(struct module* module,
574 struct symt_compiland* compiland,
575 const char* name, struct symt* type,
578 struct symt_data* sym;
580 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
581 debugstr_w(module->module.ModuleName), name);
583 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
585 sym->symt.tag = SymTagData;
586 sym->hash_elt.name = pool_strdup(&module->pool, name);
587 sym->kind = DataIsConstant;
588 sym->container = compiland ? &compiland->symt : NULL;
591 symt_add_module_ht(module, (struct symt_ht*)sym);
595 p = vector_add(&compiland->vchildren, &module->pool);
602 struct symt_hierarchy_point* symt_new_label(struct module* module,
603 struct symt_compiland* compiland,
604 const char* name, unsigned long address)
606 struct symt_hierarchy_point* sym;
608 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
609 debugstr_w(module->module.ModuleName), name);
611 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
613 sym->symt.tag = SymTagLabel;
614 sym->hash_elt.name = pool_strdup(&module->pool, name);
615 sym->loc.kind = loc_absolute;
616 sym->loc.offset = address;
617 sym->parent = compiland ? &compiland->symt : NULL;
618 symt_add_module_ht(module, (struct symt_ht*)sym);
622 p = vector_add(&compiland->vchildren, &module->pool);
629 /* expect sym_info->MaxNameLen to be set before being called */
630 static void symt_fill_sym_info(const struct module_pair* pair,
631 const struct symt_function* func,
632 const struct symt* sym, SYMBOL_INFO* sym_info)
637 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
638 sym_info->TypeIndex = 0;
639 sym_info->info = (DWORD)sym;
640 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
641 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
642 (!sym_info->TypeIndex ||
643 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
645 sym_info->Size = (DWORD)size;
646 sym_info->ModBase = pair->requested->module.BaseOfImage;
654 const struct symt_data* data = (const struct symt_data*)sym;
658 sym_info->Flags |= SYMFLAG_PARAMETER;
662 struct location loc = data->u.var;
664 if (loc.kind >= loc_user)
665 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
670 /* for now we report error cases as a negative register number */
671 sym_info->Flags |= SYMFLAG_LOCAL;
674 sym_info->Flags |= SYMFLAG_REGISTER;
675 sym_info->Register = loc.reg;
676 sym_info->Address = 0;
679 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
680 /* FIXME: it's i386 dependent !!! */
681 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
682 sym_info->Address = loc.offset;
685 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
691 case DataIsFileStatic:
692 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
693 sym_info->Register = 0;
696 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
697 switch (data->u.value.n1.n2.vt)
699 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
700 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
701 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
702 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
703 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
704 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
705 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
706 case VT_EMPTY: sym_info->Value = 0; break;
708 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
714 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
718 case SymTagPublicSymbol:
719 sym_info->Flags |= SYMFLAG_EXPORT;
720 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
723 sym_info->Flags |= SYMFLAG_FUNCTION;
724 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
727 sym_info->Flags |= SYMFLAG_THUNK;
728 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
731 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
732 sym_info->Register = 0;
735 sym_info->Scope = 0; /* FIXME */
736 sym_info->Tag = sym->tag;
737 name = symt_get_name(sym);
738 if (sym_info->MaxNameLen)
740 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
741 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
742 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
744 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
745 memcpy(sym_info->Name, name, sym_info->NameLen);
746 sym_info->Name[sym_info->NameLen] = '\0';
749 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
750 sym, sym_info->Name, sym_info->Size,
751 wine_dbgstr_longlong(sym_info->Address));
756 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
758 SYMBOL_INFO* sym_info;
762 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
765 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
766 const struct symt_function* func, const struct symt* sym)
768 symt_fill_sym_info(pair, func, sym, se->sym_info);
769 if (se->index && se->sym_info->info != se->index) return FALSE;
770 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
771 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
772 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
775 static BOOL symt_enum_module(const struct module_pair* pair, const regex_t* regex,
776 const struct sym_enum* se)
779 struct symt_ht* sym = NULL;
780 struct hash_table_iter hti;
782 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
783 while ((ptr = hash_table_iter_up(&hti)))
785 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
786 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
788 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
789 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
790 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
796 static inline unsigned where_to_insert(const struct module* module, unsigned high, const struct symt_ht* elt)
798 unsigned low = 0, mid = high / 2;
802 symt_get_info(&elt->symt, TI_GET_ADDRESS, &addr);
805 switch (cmp_sorttab_addr(module, mid, addr))
808 case -1: low = mid + 1; break;
809 case 1: high = mid; break;
811 mid = low + (high - low) / 2;
812 } while (low < high);
816 /***********************************************************************
819 * Rebuild sorted list of symbols for a module.
821 static BOOL resort_symbols(struct module* module)
823 if (!(module->module.NumSyms = module->num_symbols))
826 /* FIXME: what's the optimal value here ??? */
827 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
829 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
830 struct symt_ht* tmp[30];
832 delta = module->num_symbols - module->num_sorttab;
833 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
834 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
836 for (i = delta - 1; i >= 0; i--)
838 prev_ins_idx = ins_idx;
839 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
840 memmove(&module->addr_sorttab[ins_idx + i + 1],
841 &module->addr_sorttab[ins_idx],
842 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
843 module->addr_sorttab[ins_idx + i] = tmp[i];
847 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
848 module->num_sorttab = module->num_symbols;
849 return module->sortlist_valid = TRUE;
852 static void symt_get_length(const struct symt* symt, ULONG64* size)
856 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
859 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
860 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
861 *size = 0x1000; /* arbitrary value */
864 /* assume addr is in module */
865 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
868 ULONG64 ref_addr, ref_size;
870 if (!module->sortlist_valid || !module->addr_sorttab)
872 if (!resort_symbols(module)) return NULL;
876 * Binary search to find closest symbol.
879 high = module->num_sorttab;
881 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
882 if (addr < ref_addr) return NULL;
885 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
886 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
887 if (addr >= ref_addr + ref_size) return NULL;
890 while (high > low + 1)
892 mid = (high + low) / 2;
893 if (cmp_sorttab_addr(module, mid, addr) < 0)
898 if (low != high && high != module->num_sorttab &&
899 cmp_sorttab_addr(module, high, addr) <= 0)
902 /* If found symbol is a public symbol, check if there are any other entries that
903 * might also have the same address, but would get better information
905 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
907 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
909 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
910 !cmp_sorttab_addr(module, low - 1, ref_addr))
912 else if (low < module->num_sorttab - 1 &&
913 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
914 !cmp_sorttab_addr(module, low + 1, ref_addr))
917 /* finally check that we fit into the found symbol */
918 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
919 if (addr < ref_addr) return NULL;
920 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
921 if (addr >= ref_addr + ref_size) return NULL;
923 return module->addr_sorttab[low];
926 static BOOL symt_enum_locals_helper(struct module_pair* pair,
927 regex_t* preg, const struct sym_enum* se,
928 struct symt_function* func, const struct vector* v)
930 struct symt* lsym = NULL;
931 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
934 for (i=0; i<vector_length(v); i++)
936 lsym = *(struct symt**)vector_at(v, i);
941 struct symt_block* block = (struct symt_block*)lsym;
942 if (pc < block->address || block->address + block->size <= pc)
944 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
949 if (match_regexp(preg, symt_get_name(lsym)))
951 if (send_symbol(se, pair, func, lsym)) return FALSE;
955 case SymTagFuncDebugStart:
956 case SymTagFuncDebugEnd:
960 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
967 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
968 const struct sym_enum* se)
970 struct module_pair pair;
972 DWORD pc = pcs->ctx_frame.InstructionOffset;
974 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
975 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
978 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
979 if (!module_get_debug(&pair)) return FALSE;
980 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
982 if (sym->symt.tag == SymTagFunction)
987 compile_regex(mask ? mask : "*", -1, &preg,
988 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
989 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
990 &((struct symt_function*)sym)->vchildren);
995 return send_symbol(se, &pair, NULL, &sym->symt);
998 /******************************************************************
1001 * Helper for transforming an ANSI symbol info into a UNICODE one.
1002 * Assume that MaxNameLen is the same for both version (A & W).
1004 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1006 siw->SizeOfStruct = si->SizeOfStruct;
1007 siw->TypeIndex = si->TypeIndex;
1008 siw->Reserved[0] = si->Reserved[0];
1009 siw->Reserved[1] = si->Reserved[1];
1010 siw->Index = si->info; /* FIXME: see dbghelp.h */
1011 siw->Size = si->Size;
1012 siw->ModBase = si->ModBase;
1013 siw->Flags = si->Flags;
1014 siw->Value = si->Value;
1015 siw->Address = si->Address;
1016 siw->Register = si->Register;
1017 siw->Scope = si->Scope;
1019 siw->NameLen = si->NameLen;
1020 siw->MaxNameLen = si->MaxNameLen;
1021 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1024 /******************************************************************
1027 * Core routine for most of the enumeration of symbols
1029 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1030 const struct sym_enum* se)
1032 struct module_pair pair;
1034 regex_t mod_regex, sym_regex;
1036 pair.pcs = process_find_by_handle(hProcess);
1037 if (!pair.pcs) return FALSE;
1040 /* do local variables ? */
1041 if (!Mask || !(bang = strchr(Mask, '!')))
1042 return symt_enum_locals(pair.pcs, Mask, se);
1044 if (bang == Mask) return FALSE;
1046 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1047 compile_regex(bang + 1, -1, &sym_regex,
1048 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1050 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1052 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1054 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1055 symt_enum_module(&pair, &sym_regex, se))
1059 /* not found in PE modules, retry on the ELF ones
1061 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1063 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1065 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1066 !module_get_containee(pair.pcs, pair.requested) &&
1067 module_get_debug(&pair))
1069 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1070 symt_enum_module(&pair, &sym_regex, se))
1075 regfree(&mod_regex);
1076 regfree(&sym_regex);
1079 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1080 if (!module_get_debug(&pair))
1083 /* we always ignore module name from Mask when BaseOfDll is defined */
1084 if (Mask && (bang = strchr(Mask, '!')))
1086 if (bang == Mask) return FALSE;
1090 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1091 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1092 symt_enum_module(&pair, &sym_regex, se);
1093 regfree(&sym_regex);
1098 /******************************************************************
1099 * SymEnumSymbols (DBGHELP.@)
1101 * cases BaseOfDll = 0
1102 * !foo fails always (despite what MSDN states)
1103 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1104 * no ! in Mask, lookup in local Context
1105 * cases BaseOfDll != 0
1106 * !foo fails always (despite what MSDN states)
1107 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1109 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1110 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1115 TRACE("(%p %s %s %p %p)\n",
1116 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1117 EnumSymbolsCallback, UserContext);
1119 se.cb = EnumSymbolsCallback;
1120 se.user = UserContext;
1124 se.sym_info = (PSYMBOL_INFO)se.buffer;
1126 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1131 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1133 PSYMBOL_INFOW sym_info;
1134 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1138 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1140 struct sym_enumW* sew = ctx;
1142 copy_symbolW(sew->sym_info, si);
1144 return (sew->cb)(sew->sym_info, size, sew->ctx);
1147 /******************************************************************
1148 * SymEnumSymbolsW (DBGHELP.@)
1151 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1152 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1155 struct sym_enumW sew;
1159 sew.ctx = UserContext;
1160 sew.cb = EnumSymbolsCallback;
1161 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1165 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1166 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1167 if (!maskA) return FALSE;
1168 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1170 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1171 HeapFree(GetProcessHeap(), 0, maskA);
1176 struct sym_enumerate
1179 PSYM_ENUMSYMBOLS_CALLBACK cb;
1182 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1184 struct sym_enumerate* se = ctx;
1185 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1188 /***********************************************************************
1189 * SymEnumerateSymbols (DBGHELP.@)
1191 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1192 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1195 struct sym_enumerate se;
1197 se.ctx = UserContext;
1198 se.cb = EnumSymbolsCallback;
1200 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1203 struct sym_enumerate64
1206 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1209 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1211 struct sym_enumerate64* se = ctx;
1212 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1215 /***********************************************************************
1216 * SymEnumerateSymbols64 (DBGHELP.@)
1218 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1219 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1222 struct sym_enumerate64 se;
1224 se.ctx = UserContext;
1225 se.cb = EnumSymbolsCallback;
1227 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1230 /******************************************************************
1231 * SymFromAddr (DBGHELP.@)
1234 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1235 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1237 struct module_pair pair;
1238 struct symt_ht* sym;
1240 pair.pcs = process_find_by_handle(hProcess);
1241 if (!pair.pcs) return FALSE;
1242 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1243 if (!module_get_debug(&pair)) return FALSE;
1244 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1246 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1247 *Displacement = Address - Symbol->Address;
1251 /******************************************************************
1252 * SymFromAddrW (DBGHELP.@)
1255 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1256 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1262 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1263 si = HeapAlloc(GetProcessHeap(), 0, len);
1264 if (!si) return FALSE;
1266 si->SizeOfStruct = sizeof(*si);
1267 si->MaxNameLen = Symbol->MaxNameLen;
1268 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1270 copy_symbolW(Symbol, si);
1272 HeapFree(GetProcessHeap(), 0, si);
1276 /******************************************************************
1277 * SymGetSymFromAddr (DBGHELP.@)
1280 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1281 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1283 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1284 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1286 DWORD64 Displacement64;
1288 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1289 si->SizeOfStruct = sizeof(*si);
1290 si->MaxNameLen = MAX_SYM_NAME;
1291 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1295 *Displacement = Displacement64;
1296 Symbol->Address = si->Address;
1297 Symbol->Size = si->Size;
1298 Symbol->Flags = si->Flags;
1299 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1300 lstrcpynA(Symbol->Name, si->Name, len);
1304 /******************************************************************
1305 * SymGetSymFromAddr64 (DBGHELP.@)
1308 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1309 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1311 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1312 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1314 DWORD64 Displacement64;
1316 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1317 si->SizeOfStruct = sizeof(*si);
1318 si->MaxNameLen = MAX_SYM_NAME;
1319 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1323 *Displacement = Displacement64;
1324 Symbol->Address = si->Address;
1325 Symbol->Size = si->Size;
1326 Symbol->Flags = si->Flags;
1327 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1328 lstrcpynA(Symbol->Name, si->Name, len);
1332 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1333 SYMBOL_INFO* symbol)
1335 struct hash_table_iter hti;
1337 struct symt_ht* sym = NULL;
1338 struct module_pair pair;
1341 if (!(pair.requested = module)) return FALSE;
1342 if (!module_get_debug(&pair)) return FALSE;
1344 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1345 while ((ptr = hash_table_iter_up(&hti)))
1347 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1349 if (!strcmp(sym->hash_elt.name, name))
1351 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1358 /******************************************************************
1359 * SymFromName (DBGHELP.@)
1362 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1364 struct process* pcs = process_find_by_handle(hProcess);
1365 struct module* module;
1368 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1369 if (!pcs) return FALSE;
1370 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1371 name = strchr(Name, '!');
1375 assert(name - Name < sizeof(tmp));
1376 memcpy(tmp, Name, name - Name);
1377 tmp[name - Name] = '\0';
1378 module = module_find_by_nameA(pcs, tmp);
1379 return find_name(pcs, module, name + 1, Symbol);
1381 for (module = pcs->lmodules; module; module = module->next)
1383 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1386 /* not found in PE modules, retry on the ELF ones
1388 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1390 for (module = pcs->lmodules; module; module = module->next)
1392 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1393 !module_get_containee(pcs, module) &&
1394 find_name(pcs, module, Name, Symbol))
1401 /***********************************************************************
1402 * SymGetSymFromName64 (DBGHELP.@)
1404 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1406 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1407 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1410 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1411 si->SizeOfStruct = sizeof(*si);
1412 si->MaxNameLen = MAX_SYM_NAME;
1413 if (!SymFromName(hProcess, Name, si)) return FALSE;
1415 Symbol->Address = si->Address;
1416 Symbol->Size = si->Size;
1417 Symbol->Flags = si->Flags;
1418 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1419 lstrcpynA(Symbol->Name, si->Name, len);
1423 /***********************************************************************
1424 * SymGetSymFromName (DBGHELP.@)
1426 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1428 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1429 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1432 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1433 si->SizeOfStruct = sizeof(*si);
1434 si->MaxNameLen = MAX_SYM_NAME;
1435 if (!SymFromName(hProcess, Name, si)) return FALSE;
1437 Symbol->Address = si->Address;
1438 Symbol->Size = si->Size;
1439 Symbol->Flags = si->Flags;
1440 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1441 lstrcpynA(Symbol->Name, si->Name, len);
1445 /******************************************************************
1446 * sym_fill_func_line_info
1448 * fills information about a file
1450 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1451 DWORD addr, IMAGEHLP_LINE* line)
1453 struct line_info* dli = NULL;
1457 assert(func->symt.tag == SymTagFunction);
1459 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1461 dli = vector_at(&func->vlines, i);
1462 if (!dli->is_source_file)
1464 if (found || dli->u.pc_offset > addr) continue;
1465 line->LineNumber = dli->line_number;
1466 line->Address = dli->u.pc_offset;
1473 line->FileName = (char*)source_get(module, dli->u.source_file);
1480 /***********************************************************************
1481 * SymGetSymNext64 (DBGHELP.@)
1483 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1486 * get module from Symbol.Address
1487 * get index in module.addr_sorttab of Symbol.Address
1489 * if out of module bounds, move to next module in process address space
1491 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1492 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1496 /***********************************************************************
1497 * SymGetSymNext (DBGHELP.@)
1499 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1501 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1502 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1506 /***********************************************************************
1507 * SymGetSymPrev64 (DBGHELP.@)
1509 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1511 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1512 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1516 /***********************************************************************
1517 * SymGetSymPrev (DBGHELP.@)
1519 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1521 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1522 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1526 /******************************************************************
1527 * SymGetLineFromAddr (DBGHELP.@)
1530 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1531 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1533 struct module_pair pair;
1534 struct symt_ht* symt;
1536 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1538 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1540 pair.pcs = process_find_by_handle(hProcess);
1541 if (!pair.pcs) return FALSE;
1542 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1543 if (!module_get_debug(&pair)) return FALSE;
1544 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1546 if (symt->symt.tag != SymTagFunction) return FALSE;
1547 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1548 dwAddr, Line)) return FALSE;
1549 *pdwDisplacement = dwAddr - Line->Address;
1553 /******************************************************************
1554 * copy_line_64_from_32 (internal)
1557 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1560 l64->Key = l32->Key;
1561 l64->LineNumber = l32->LineNumber;
1562 l64->FileName = l32->FileName;
1563 l64->Address = l32->Address;
1566 /******************************************************************
1567 * copy_line_W64_from_32 (internal)
1570 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1574 l64->Key = l32->Key;
1575 l64->LineNumber = l32->LineNumber;
1576 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1577 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1578 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1579 l64->Address = l32->Address;
1582 /******************************************************************
1583 * copy_line_32_from_64 (internal)
1586 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1589 l32->Key = l64->Key;
1590 l32->LineNumber = l64->LineNumber;
1591 l32->FileName = l64->FileName;
1592 l32->Address = l64->Address;
1595 /******************************************************************
1596 * SymGetLineFromAddr64 (DBGHELP.@)
1599 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1600 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1602 IMAGEHLP_LINE line32;
1604 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1605 if (!validate_addr64(dwAddr)) return FALSE;
1606 line32.SizeOfStruct = sizeof(line32);
1607 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1609 copy_line_64_from_32(Line, &line32);
1613 /******************************************************************
1614 * SymGetLineFromAddrW64 (DBGHELP.@)
1617 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1618 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1620 struct process* pcs = process_find_by_handle(hProcess);
1621 IMAGEHLP_LINE line32;
1623 if (!pcs) return FALSE;
1624 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1625 if (!validate_addr64(dwAddr)) return FALSE;
1626 line32.SizeOfStruct = sizeof(line32);
1627 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1629 copy_line_W64_from_32(pcs, Line, &line32);
1633 /******************************************************************
1634 * SymGetLinePrev (DBGHELP.@)
1637 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1639 struct module_pair pair;
1640 struct line_info* li;
1641 BOOL in_search = FALSE;
1643 TRACE("(%p %p)\n", hProcess, Line);
1645 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1647 pair.pcs = process_find_by_handle(hProcess);
1648 if (!pair.pcs) return FALSE;
1649 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1650 if (!module_get_debug(&pair)) return FALSE;
1652 if (Line->Key == 0) return FALSE;
1654 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1655 * element we have to go back until we find the prev one to get the real
1656 * source file name for the DLIT_OFFSET element just before
1657 * the first DLIT_SOURCEFILE
1659 while (!li->is_first)
1662 if (!li->is_source_file)
1664 Line->LineNumber = li->line_number;
1665 Line->Address = li->u.pc_offset;
1667 if (!in_search) return TRUE;
1673 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1679 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1683 /******************************************************************
1684 * SymGetLinePrev64 (DBGHELP.@)
1687 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1689 IMAGEHLP_LINE line32;
1691 line32.SizeOfStruct = sizeof(line32);
1692 copy_line_32_from_64(&line32, Line);
1693 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1694 copy_line_64_from_32(Line, &line32);
1698 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1700 struct line_info* li;
1702 if (line->Key == 0) return FALSE;
1704 while (!li->is_last)
1707 if (!li->is_source_file)
1709 line->LineNumber = li->line_number;
1710 line->Address = li->u.pc_offset;
1714 line->FileName = (char*)source_get(module, li->u.source_file);
1719 /******************************************************************
1720 * SymGetLineNext (DBGHELP.@)
1723 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1725 struct module_pair pair;
1727 TRACE("(%p %p)\n", hProcess, Line);
1729 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1730 pair.pcs = process_find_by_handle(hProcess);
1731 if (!pair.pcs) return FALSE;
1732 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1733 if (!module_get_debug(&pair)) return FALSE;
1735 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1736 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1740 /******************************************************************
1741 * SymGetLineNext64 (DBGHELP.@)
1744 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1746 IMAGEHLP_LINE line32;
1748 line32.SizeOfStruct = sizeof(line32);
1749 copy_line_32_from_64(&line32, Line);
1750 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1751 copy_line_64_from_32(Line, &line32);
1755 /***********************************************************************
1756 * SymFunctionTableAccess (DBGHELP.@)
1758 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1760 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1764 /***********************************************************************
1765 * SymFunctionTableAccess64 (DBGHELP.@)
1767 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1769 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1773 /***********************************************************************
1774 * SymUnDName (DBGHELP.@)
1776 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1778 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1779 UNDNAME_COMPLETE) != 0;
1782 /***********************************************************************
1783 * SymUnDName64 (DBGHELP.@)
1785 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1787 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1788 UNDNAME_COMPLETE) != 0;
1791 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1792 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1794 /***********************************************************************
1795 * UnDecorateSymbolName (DBGHELP.@)
1797 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1798 DWORD UndecoratedLength, DWORD Flags)
1800 /* undocumented from msvcrt */
1801 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1802 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1804 TRACE("(%s, %p, %d, 0x%08x)\n",
1805 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1809 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1810 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1811 if (!p_undname) return 0;
1814 if (!UnDecoratedName) return 0;
1815 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1816 und_alloc, und_free, Flags))
1818 return strlen(UnDecoratedName);
1821 /******************************************************************
1822 * SymMatchString (DBGHELP.@)
1825 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1830 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1832 compile_regex(re, -1, &preg, _case);
1833 ret = match_regexp(&preg, string);
1838 /******************************************************************
1839 * SymSearch (DBGHELP.@)
1841 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1842 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1843 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1844 PVOID UserContext, DWORD Options)
1848 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1849 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1850 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1851 UserContext, Options);
1853 if (Options != SYMSEARCH_GLOBALSONLY)
1855 FIXME("Unsupported searching with options (%x)\n", Options);
1856 SetLastError(ERROR_INVALID_PARAMETER);
1860 se.cb = EnumSymbolsCallback;
1861 se.user = UserContext;
1865 se.sym_info = (PSYMBOL_INFO)se.buffer;
1867 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1870 /******************************************************************
1871 * SymSearchW (DBGHELP.@)
1873 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1874 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1875 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1876 PVOID UserContext, DWORD Options)
1878 struct sym_enumW sew;
1882 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1883 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1884 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1885 UserContext, Options);
1887 sew.ctx = UserContext;
1888 sew.cb = EnumSymbolsCallback;
1889 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1893 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1894 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1895 if (!maskA) return FALSE;
1896 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1898 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1899 sym_enumW, &sew, Options);
1900 HeapFree(GetProcessHeap(), 0, maskA);
1905 /******************************************************************
1906 * SymAddSymbol (DBGHELP.@)
1909 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1910 DWORD64 addr, DWORD size, DWORD flags)
1912 WCHAR nameW[MAX_SYM_NAME];
1914 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1915 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1918 /******************************************************************
1919 * SymAddSymbolW (DBGHELP.@)
1922 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1923 DWORD64 addr, DWORD size, DWORD flags)
1925 struct module_pair pair;
1927 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1929 pair.pcs = process_find_by_handle(hProcess);
1930 if (!pair.pcs) return FALSE;
1931 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1932 if (!module_get_debug(&pair)) return FALSE;
1934 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1938 /******************************************************************
1939 * SymSetScopeFromAddr (DBGHELP.@)
1941 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1943 struct process* pcs;
1945 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1947 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1951 /******************************************************************
1952 * SymEnumLines (DBGHELP.@)
1955 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1956 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1958 struct module_pair pair;
1959 struct hash_table_iter hti;
1960 struct symt_ht* sym;
1962 struct line_info* dli;
1967 if (!cb) return FALSE;
1968 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1970 pair.pcs = process_find_by_handle(hProcess);
1971 if (!pair.pcs) return FALSE;
1972 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1973 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1974 if (!module_get_debug(&pair)) return FALSE;
1975 if (!compile_file_regex(&re, srcfile)) return FALSE;
1977 sci.SizeOfStruct = sizeof(sci);
1980 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1981 while ((ptr = hash_table_iter_up(&hti)))
1985 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1986 if (sym->symt.tag != SymTagFunction) continue;
1988 sci.FileName[0] = '\0';
1989 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1991 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1992 if (dli->is_source_file)
1994 file = source_get(pair.effective, dli->u.source_file);
1995 if (!match_regexp(&re, file)) file = "";
1996 strcpy(sci.FileName, file);
1998 else if (sci.FileName[0])
2001 sci.Obj[0] = '\0'; /* FIXME */
2002 sci.LineNumber = dli->line_number;
2003 sci.Address = dli->u.pc_offset;
2004 if (!cb(&sci, user)) break;