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 = (ULONG)data->u.value.n1.n2.n3.byref; break;
707 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
713 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
717 case SymTagPublicSymbol:
718 sym_info->Flags |= SYMFLAG_EXPORT;
719 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
722 sym_info->Flags |= SYMFLAG_FUNCTION;
723 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
726 sym_info->Flags |= SYMFLAG_THUNK;
727 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
730 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
731 sym_info->Register = 0;
734 sym_info->Scope = 0; /* FIXME */
735 sym_info->Tag = sym->tag;
736 name = symt_get_name(sym);
737 if (sym_info->MaxNameLen)
739 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
740 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
741 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
743 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
744 memcpy(sym_info->Name, name, sym_info->NameLen);
745 sym_info->Name[sym_info->NameLen] = '\0';
748 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
749 sym, sym_info->Name, sym_info->Size,
750 wine_dbgstr_longlong(sym_info->Address));
755 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
757 SYMBOL_INFO* sym_info;
761 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
764 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
765 const struct symt_function* func, const struct symt* sym)
767 symt_fill_sym_info(pair, func, sym, se->sym_info);
768 if (se->index && se->sym_info->info != se->index) return FALSE;
769 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
770 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
771 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
774 static BOOL symt_enum_module(const struct module_pair* pair, const regex_t* regex,
775 const struct sym_enum* se)
778 struct symt_ht* sym = NULL;
779 struct hash_table_iter hti;
781 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
782 while ((ptr = hash_table_iter_up(&hti)))
784 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
785 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
787 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
788 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
789 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
795 static inline unsigned where_to_insert(const struct module* module, unsigned high, const struct symt_ht* elt)
797 unsigned low = 0, mid = high / 2;
801 symt_get_info(&elt->symt, TI_GET_ADDRESS, &addr);
804 switch (cmp_sorttab_addr(module, mid, addr))
807 case -1: low = mid + 1; break;
808 case 1: high = mid; break;
810 mid = low + (high - low) / 2;
811 } while (low < high);
815 /***********************************************************************
818 * Rebuild sorted list of symbols for a module.
820 static BOOL resort_symbols(struct module* module)
822 if (!(module->module.NumSyms = module->num_symbols))
825 /* FIXME: what's the optimal value here ??? */
826 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
828 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
829 struct symt_ht* tmp[30];
831 delta = module->num_symbols - module->num_sorttab;
832 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
833 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
835 for (i = delta - 1; i >= 0; i--)
837 prev_ins_idx = ins_idx;
838 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
839 memmove(&module->addr_sorttab[ins_idx + i + 1],
840 &module->addr_sorttab[ins_idx],
841 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
842 module->addr_sorttab[ins_idx + i] = tmp[i];
846 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
847 module->num_sorttab = module->num_symbols;
848 return module->sortlist_valid = TRUE;
851 static void symt_get_length(const struct symt* symt, ULONG64* size)
855 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
858 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
859 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
860 *size = 0x1000; /* arbitrary value */
863 /* assume addr is in module */
864 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
867 ULONG64 ref_addr, ref_size;
869 if (!module->sortlist_valid || !module->addr_sorttab)
871 if (!resort_symbols(module)) return NULL;
875 * Binary search to find closest symbol.
878 high = module->num_sorttab;
880 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
881 if (addr < ref_addr) return NULL;
884 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
885 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
886 if (addr >= ref_addr + ref_size) return NULL;
889 while (high > low + 1)
891 mid = (high + low) / 2;
892 if (cmp_sorttab_addr(module, mid, addr) < 0)
897 if (low != high && high != module->num_sorttab &&
898 cmp_sorttab_addr(module, high, addr) <= 0)
901 /* If found symbol is a public symbol, check if there are any other entries that
902 * might also have the same address, but would get better information
904 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
906 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
908 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
909 !cmp_sorttab_addr(module, low - 1, ref_addr))
911 else if (low < module->num_sorttab - 1 &&
912 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
913 !cmp_sorttab_addr(module, low + 1, ref_addr))
916 /* finally check that we fit into the found symbol */
917 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
918 if (addr < ref_addr) return NULL;
919 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
920 if (addr >= ref_addr + ref_size) return NULL;
922 return module->addr_sorttab[low];
925 static BOOL symt_enum_locals_helper(struct module_pair* pair,
926 regex_t* preg, const struct sym_enum* se,
927 struct symt_function* func, const struct vector* v)
929 struct symt* lsym = NULL;
930 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
933 for (i=0; i<vector_length(v); i++)
935 lsym = *(struct symt**)vector_at(v, i);
940 struct symt_block* block = (struct symt_block*)lsym;
941 if (pc < block->address || block->address + block->size <= pc)
943 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
948 if (match_regexp(preg, symt_get_name(lsym)))
950 if (send_symbol(se, pair, func, lsym)) return FALSE;
954 case SymTagFuncDebugStart:
955 case SymTagFuncDebugEnd:
959 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
966 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
967 const struct sym_enum* se)
969 struct module_pair pair;
971 DWORD pc = pcs->ctx_frame.InstructionOffset;
973 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
974 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
977 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
978 if (!module_get_debug(&pair)) return FALSE;
979 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
981 if (sym->symt.tag == SymTagFunction)
986 compile_regex(mask ? mask : "*", -1, &preg,
987 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
988 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
989 &((struct symt_function*)sym)->vchildren);
994 return send_symbol(se, &pair, NULL, &sym->symt);
997 /******************************************************************
1000 * Helper for transforming an ANSI symbol info into a UNICODE one.
1001 * Assume that MaxNameLen is the same for both version (A & W).
1003 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1005 siw->SizeOfStruct = si->SizeOfStruct;
1006 siw->TypeIndex = si->TypeIndex;
1007 siw->Reserved[0] = si->Reserved[0];
1008 siw->Reserved[1] = si->Reserved[1];
1009 siw->Index = si->info; /* FIXME: see dbghelp.h */
1010 siw->Size = si->Size;
1011 siw->ModBase = si->ModBase;
1012 siw->Flags = si->Flags;
1013 siw->Value = si->Value;
1014 siw->Address = si->Address;
1015 siw->Register = si->Register;
1016 siw->Scope = si->Scope;
1018 siw->NameLen = si->NameLen;
1019 siw->MaxNameLen = si->MaxNameLen;
1020 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1023 /******************************************************************
1026 * Core routine for most of the enumeration of symbols
1028 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1029 const struct sym_enum* se)
1031 struct module_pair pair;
1033 regex_t mod_regex, sym_regex;
1035 pair.pcs = process_find_by_handle(hProcess);
1038 /* do local variables ? */
1039 if (!Mask || !(bang = strchr(Mask, '!')))
1040 return symt_enum_locals(pair.pcs, Mask, se);
1042 if (bang == Mask) return FALSE;
1044 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1045 compile_regex(bang + 1, -1, &sym_regex,
1046 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1048 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1050 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1052 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1053 symt_enum_module(&pair, &sym_regex, se))
1057 /* not found in PE modules, retry on the ELF ones
1059 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1061 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1063 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1064 !module_get_containee(pair.pcs, pair.requested) &&
1065 module_get_debug(&pair))
1067 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1068 symt_enum_module(&pair, &sym_regex, se))
1073 regfree(&mod_regex);
1074 regfree(&sym_regex);
1077 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1078 if (!module_get_debug(&pair))
1081 /* we always ignore module name from Mask when BaseOfDll is defined */
1082 if (Mask && (bang = strchr(Mask, '!')))
1084 if (bang == Mask) return FALSE;
1088 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1089 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1090 symt_enum_module(&pair, &sym_regex, se);
1091 regfree(&sym_regex);
1096 /******************************************************************
1097 * SymEnumSymbols (DBGHELP.@)
1099 * cases BaseOfDll = 0
1100 * !foo fails always (despite what MSDN states)
1101 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1102 * no ! in Mask, lookup in local Context
1103 * cases BaseOfDll != 0
1104 * !foo fails always (despite what MSDN states)
1105 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1107 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1108 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1113 TRACE("(%p %s %s %p %p)\n",
1114 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1115 EnumSymbolsCallback, UserContext);
1117 se.cb = EnumSymbolsCallback;
1118 se.user = UserContext;
1122 se.sym_info = (PSYMBOL_INFO)se.buffer;
1124 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1129 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1131 PSYMBOL_INFOW sym_info;
1132 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1136 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1138 struct sym_enumW* sew = ctx;
1140 copy_symbolW(sew->sym_info, si);
1142 return (sew->cb)(sew->sym_info, size, sew->ctx);
1145 /******************************************************************
1146 * SymEnumSymbolsW (DBGHELP.@)
1149 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1150 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1153 struct sym_enumW sew;
1157 sew.ctx = UserContext;
1158 sew.cb = EnumSymbolsCallback;
1159 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1163 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1164 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1165 if (!maskA) return FALSE;
1166 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1168 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1169 HeapFree(GetProcessHeap(), 0, maskA);
1174 struct sym_enumerate
1177 PSYM_ENUMSYMBOLS_CALLBACK cb;
1180 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1182 struct sym_enumerate* se = ctx;
1183 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1186 /***********************************************************************
1187 * SymEnumerateSymbols (DBGHELP.@)
1189 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1190 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1193 struct sym_enumerate se;
1195 se.ctx = UserContext;
1196 se.cb = EnumSymbolsCallback;
1198 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1201 struct sym_enumerate64
1204 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1207 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1209 struct sym_enumerate64* se = ctx;
1210 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1213 /***********************************************************************
1214 * SymEnumerateSymbols64 (DBGHELP.@)
1216 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1217 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1220 struct sym_enumerate64 se;
1222 se.ctx = UserContext;
1223 se.cb = EnumSymbolsCallback;
1225 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1228 /******************************************************************
1229 * SymFromAddr (DBGHELP.@)
1232 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1233 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1235 struct module_pair pair;
1236 struct symt_ht* sym;
1238 pair.pcs = process_find_by_handle(hProcess);
1239 if (!pair.pcs) return FALSE;
1240 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1241 if (!module_get_debug(&pair)) return FALSE;
1242 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1244 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1245 *Displacement = Address - Symbol->Address;
1249 /******************************************************************
1250 * SymFromAddrW (DBGHELP.@)
1253 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1254 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1260 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1261 si = HeapAlloc(GetProcessHeap(), 0, len);
1262 if (!si) return FALSE;
1264 si->SizeOfStruct = sizeof(*si);
1265 si->MaxNameLen = Symbol->MaxNameLen;
1266 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1268 copy_symbolW(Symbol, si);
1270 HeapFree(GetProcessHeap(), 0, si);
1274 /******************************************************************
1275 * SymGetSymFromAddr (DBGHELP.@)
1278 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1279 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1281 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1282 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1284 DWORD64 Displacement64;
1286 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1287 si->SizeOfStruct = sizeof(*si);
1288 si->MaxNameLen = MAX_SYM_NAME;
1289 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1293 *Displacement = Displacement64;
1294 Symbol->Address = si->Address;
1295 Symbol->Size = si->Size;
1296 Symbol->Flags = si->Flags;
1297 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1298 lstrcpynA(Symbol->Name, si->Name, len);
1302 /******************************************************************
1303 * SymGetSymFromAddr64 (DBGHELP.@)
1306 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1307 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1309 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1310 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1312 DWORD64 Displacement64;
1314 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1315 si->SizeOfStruct = sizeof(*si);
1316 si->MaxNameLen = MAX_SYM_NAME;
1317 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1321 *Displacement = Displacement64;
1322 Symbol->Address = si->Address;
1323 Symbol->Size = si->Size;
1324 Symbol->Flags = si->Flags;
1325 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1326 lstrcpynA(Symbol->Name, si->Name, len);
1330 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1331 SYMBOL_INFO* symbol)
1333 struct hash_table_iter hti;
1335 struct symt_ht* sym = NULL;
1336 struct module_pair pair;
1339 if (!(pair.requested = module)) return FALSE;
1340 if (!module_get_debug(&pair)) return FALSE;
1342 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1343 while ((ptr = hash_table_iter_up(&hti)))
1345 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1347 if (!strcmp(sym->hash_elt.name, name))
1349 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1356 /******************************************************************
1357 * SymFromName (DBGHELP.@)
1360 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1362 struct process* pcs = process_find_by_handle(hProcess);
1363 struct module* module;
1366 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1367 if (!pcs) return FALSE;
1368 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1369 name = strchr(Name, '!');
1373 assert(name - Name < sizeof(tmp));
1374 memcpy(tmp, Name, name - Name);
1375 tmp[name - Name] = '\0';
1376 module = module_find_by_nameA(pcs, tmp);
1377 return find_name(pcs, module, name + 1, Symbol);
1379 for (module = pcs->lmodules; module; module = module->next)
1381 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1384 /* not found in PE modules, retry on the ELF ones
1386 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1388 for (module = pcs->lmodules; module; module = module->next)
1390 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1391 !module_get_containee(pcs, module) &&
1392 find_name(pcs, module, Name, Symbol))
1399 /***********************************************************************
1400 * SymGetSymFromName64 (DBGHELP.@)
1402 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1404 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1405 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1408 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1409 si->SizeOfStruct = sizeof(*si);
1410 si->MaxNameLen = MAX_SYM_NAME;
1411 if (!SymFromName(hProcess, Name, si)) return FALSE;
1413 Symbol->Address = si->Address;
1414 Symbol->Size = si->Size;
1415 Symbol->Flags = si->Flags;
1416 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1417 lstrcpynA(Symbol->Name, si->Name, len);
1421 /***********************************************************************
1422 * SymGetSymFromName (DBGHELP.@)
1424 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1426 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1427 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1430 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1431 si->SizeOfStruct = sizeof(*si);
1432 si->MaxNameLen = MAX_SYM_NAME;
1433 if (!SymFromName(hProcess, Name, si)) return FALSE;
1435 Symbol->Address = si->Address;
1436 Symbol->Size = si->Size;
1437 Symbol->Flags = si->Flags;
1438 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1439 lstrcpynA(Symbol->Name, si->Name, len);
1443 /******************************************************************
1444 * sym_fill_func_line_info
1446 * fills information about a file
1448 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1449 DWORD addr, IMAGEHLP_LINE* line)
1451 struct line_info* dli = NULL;
1455 assert(func->symt.tag == SymTagFunction);
1457 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1459 dli = vector_at(&func->vlines, i);
1460 if (!dli->is_source_file)
1462 if (found || dli->u.pc_offset > addr) continue;
1463 line->LineNumber = dli->line_number;
1464 line->Address = dli->u.pc_offset;
1471 line->FileName = (char*)source_get(module, dli->u.source_file);
1478 /***********************************************************************
1479 * SymGetSymNext64 (DBGHELP.@)
1481 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1484 * get module from Symbol.Address
1485 * get index in module.addr_sorttab of Symbol.Address
1487 * if out of module bounds, move to next module in process address space
1489 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1490 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1494 /***********************************************************************
1495 * SymGetSymNext (DBGHELP.@)
1497 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1499 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1500 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1504 /***********************************************************************
1505 * SymGetSymPrev64 (DBGHELP.@)
1507 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1509 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1510 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1514 /***********************************************************************
1515 * SymGetSymPrev (DBGHELP.@)
1517 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1519 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1520 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1524 /******************************************************************
1525 * SymGetLineFromAddr (DBGHELP.@)
1528 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1529 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1531 struct module_pair pair;
1532 struct symt_ht* symt;
1534 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1536 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1538 pair.pcs = process_find_by_handle(hProcess);
1539 if (!pair.pcs) return FALSE;
1540 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1541 if (!module_get_debug(&pair)) return FALSE;
1542 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1544 if (symt->symt.tag != SymTagFunction) return FALSE;
1545 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1546 dwAddr, Line)) return FALSE;
1547 *pdwDisplacement = dwAddr - Line->Address;
1551 /******************************************************************
1552 * copy_line_64_from_32 (internal)
1555 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1558 l64->Key = l32->Key;
1559 l64->LineNumber = l32->LineNumber;
1560 l64->FileName = l32->FileName;
1561 l64->Address = l32->Address;
1564 /******************************************************************
1565 * copy_line_W64_from_32 (internal)
1568 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1572 l64->Key = l32->Key;
1573 l64->LineNumber = l32->LineNumber;
1574 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1575 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1576 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1577 l64->Address = l32->Address;
1580 /******************************************************************
1581 * copy_line_32_from_64 (internal)
1584 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1587 l32->Key = l64->Key;
1588 l32->LineNumber = l64->LineNumber;
1589 l32->FileName = l64->FileName;
1590 l32->Address = l64->Address;
1593 /******************************************************************
1594 * SymGetLineFromAddr64 (DBGHELP.@)
1597 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1598 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1600 IMAGEHLP_LINE line32;
1602 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1603 if (!validate_addr64(dwAddr)) return FALSE;
1604 line32.SizeOfStruct = sizeof(line32);
1605 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1607 copy_line_64_from_32(Line, &line32);
1611 /******************************************************************
1612 * SymGetLineFromAddrW64 (DBGHELP.@)
1615 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1616 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1618 struct process* pcs = process_find_by_handle(hProcess);
1619 IMAGEHLP_LINE line32;
1621 if (!pcs) return FALSE;
1622 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1623 if (!validate_addr64(dwAddr)) return FALSE;
1624 line32.SizeOfStruct = sizeof(line32);
1625 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1627 copy_line_W64_from_32(pcs, Line, &line32);
1631 /******************************************************************
1632 * SymGetLinePrev (DBGHELP.@)
1635 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1637 struct module_pair pair;
1638 struct line_info* li;
1639 BOOL in_search = FALSE;
1641 TRACE("(%p %p)\n", hProcess, Line);
1643 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1645 pair.pcs = process_find_by_handle(hProcess);
1646 if (!pair.pcs) return FALSE;
1647 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1648 if (!module_get_debug(&pair)) return FALSE;
1650 if (Line->Key == 0) return FALSE;
1652 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1653 * element we have to go back until we find the prev one to get the real
1654 * source file name for the DLIT_OFFSET element just before
1655 * the first DLIT_SOURCEFILE
1657 while (!li->is_first)
1660 if (!li->is_source_file)
1662 Line->LineNumber = li->line_number;
1663 Line->Address = li->u.pc_offset;
1665 if (!in_search) return TRUE;
1671 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1677 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1681 /******************************************************************
1682 * SymGetLinePrev64 (DBGHELP.@)
1685 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1687 IMAGEHLP_LINE line32;
1689 line32.SizeOfStruct = sizeof(line32);
1690 copy_line_32_from_64(&line32, Line);
1691 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1692 copy_line_64_from_32(Line, &line32);
1696 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1698 struct line_info* li;
1700 if (line->Key == 0) return FALSE;
1702 while (!li->is_last)
1705 if (!li->is_source_file)
1707 line->LineNumber = li->line_number;
1708 line->Address = li->u.pc_offset;
1712 line->FileName = (char*)source_get(module, li->u.source_file);
1717 /******************************************************************
1718 * SymGetLineNext (DBGHELP.@)
1721 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1723 struct module_pair pair;
1725 TRACE("(%p %p)\n", hProcess, Line);
1727 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1728 pair.pcs = process_find_by_handle(hProcess);
1729 if (!pair.pcs) return FALSE;
1730 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1731 if (!module_get_debug(&pair)) return FALSE;
1733 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1734 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1738 /******************************************************************
1739 * SymGetLineNext64 (DBGHELP.@)
1742 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1744 IMAGEHLP_LINE line32;
1746 line32.SizeOfStruct = sizeof(line32);
1747 copy_line_32_from_64(&line32, Line);
1748 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1749 copy_line_64_from_32(Line, &line32);
1753 /***********************************************************************
1754 * SymFunctionTableAccess (DBGHELP.@)
1756 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1758 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1762 /***********************************************************************
1763 * SymFunctionTableAccess64 (DBGHELP.@)
1765 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1767 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1771 /***********************************************************************
1772 * SymUnDName (DBGHELP.@)
1774 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1776 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1777 UNDNAME_COMPLETE) != 0;
1780 /***********************************************************************
1781 * SymUnDName64 (DBGHELP.@)
1783 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1785 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1786 UNDNAME_COMPLETE) != 0;
1789 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1790 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1792 /***********************************************************************
1793 * UnDecorateSymbolName (DBGHELP.@)
1795 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1796 DWORD UndecoratedLength, DWORD Flags)
1798 /* undocumented from msvcrt */
1799 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1800 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1802 TRACE("(%s, %p, %d, 0x%08x)\n",
1803 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1807 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1808 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1809 if (!p_undname) return 0;
1812 if (!UnDecoratedName) return 0;
1813 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1814 und_alloc, und_free, Flags))
1816 return strlen(UnDecoratedName);
1819 /******************************************************************
1820 * SymMatchString (DBGHELP.@)
1823 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1828 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1830 compile_regex(re, -1, &preg, _case);
1831 ret = match_regexp(&preg, string);
1836 /******************************************************************
1837 * SymSearch (DBGHELP.@)
1839 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1840 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1841 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1842 PVOID UserContext, DWORD Options)
1846 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1847 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1848 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1849 UserContext, Options);
1851 if (Options != SYMSEARCH_GLOBALSONLY)
1853 FIXME("Unsupported searching with options (%x)\n", Options);
1854 SetLastError(ERROR_INVALID_PARAMETER);
1858 se.cb = EnumSymbolsCallback;
1859 se.user = UserContext;
1863 se.sym_info = (PSYMBOL_INFO)se.buffer;
1865 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1868 /******************************************************************
1869 * SymSearchW (DBGHELP.@)
1871 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1872 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1873 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1874 PVOID UserContext, DWORD Options)
1876 struct sym_enumW sew;
1880 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1881 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1882 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1883 UserContext, Options);
1885 sew.ctx = UserContext;
1886 sew.cb = EnumSymbolsCallback;
1887 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1891 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1892 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1893 if (!maskA) return FALSE;
1894 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1896 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1897 sym_enumW, &sew, Options);
1898 HeapFree(GetProcessHeap(), 0, maskA);
1903 /******************************************************************
1904 * SymAddSymbol (DBGHELP.@)
1907 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1908 DWORD64 addr, DWORD size, DWORD flags)
1910 WCHAR nameW[MAX_SYM_NAME];
1912 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1913 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1916 /******************************************************************
1917 * SymAddSymbolW (DBGHELP.@)
1920 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1921 DWORD64 addr, DWORD size, DWORD flags)
1923 struct module_pair pair;
1925 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1927 pair.pcs = process_find_by_handle(hProcess);
1928 if (!pair.pcs) return FALSE;
1929 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1930 if (!module_get_debug(&pair)) return FALSE;
1932 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1936 /******************************************************************
1937 * SymSetScopeFromAddr (DBGHELP.@)
1939 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1941 struct process* pcs;
1943 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1945 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1949 /******************************************************************
1950 * SymEnumLines (DBGHELP.@)
1953 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1954 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1956 struct module_pair pair;
1957 struct hash_table_iter hti;
1958 struct symt_ht* sym;
1960 struct line_info* dli;
1965 if (!cb) return FALSE;
1966 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1968 pair.pcs = process_find_by_handle(hProcess);
1969 if (!pair.pcs) return FALSE;
1970 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1971 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1972 if (!module_get_debug(&pair)) return FALSE;
1973 if (!compile_file_regex(&re, srcfile)) return FALSE;
1975 sci.SizeOfStruct = sizeof(sci);
1978 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1979 while ((ptr = hash_table_iter_up(&hti)))
1983 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1984 if (sym->symt.tag != SymTagFunction) continue;
1986 sci.FileName[0] = '\0';
1987 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1989 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1990 if (dli->is_source_file)
1992 file = source_get(pair.effective, dli->u.source_file);
1993 if (!match_regexp(&re, file)) file = "";
1994 strcpy(sci.FileName, file);
1996 else if (sci.FileName[0])
1999 sci.Obj[0] = '\0'; /* FIXME */
2000 sci.LineNumber = dli->line_number;
2001 sci.Address = dli->u.pc_offset;
2002 if (!cb(&sci, user)) break;