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)
74 if (module->addr_sorttab)
75 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
76 sz * sizeof(struct symt_ht*));
78 new = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(struct symt_ht*));
79 if (!new) return FALSE;
80 module->addr_sorttab = new;
84 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
88 hash_table_add(&module->ht_symbols, &ht->hash_elt);
89 /* Don't store in sorttab a symbol without address, they are of
90 * no use here (e.g. constant values)
92 if (symt_get_info(&ht->symt, TI_GET_ADDRESS, &addr) &&
93 symt_grow_sorttab(module, module->num_symbols + 1))
95 module->addr_sorttab[module->num_symbols++] = ht;
96 module->sortlist_valid = FALSE;
102 /* transforms a dbghelp's regular expression into a POSIX one
103 * Here are the valid dbghelp reg ex characters:
104 * * 0 or more characters
105 * ? a single character
107 * # 0 or more of preceding char
108 * + 1 or more of preceding char
109 * escapes \ on #, ?, [, ], *, +. don't work on -
111 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
114 BOOL in_escape = FALSE;
115 unsigned flags = REG_NOSUB;
117 if (numchar == -1) numchar = strlen( str );
119 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
122 while (*str && numchar--)
124 /* FIXME: this shouldn't be valid on '-' */
133 case '\\': in_escape = TRUE; break;
134 case '*': *p++ = '.'; *p++ = '*'; break;
135 case '?': *p++ = '.'; break;
136 case '#': *p++ = '*'; break;
137 /* escape some valid characters in dbghelp reg exp:s */
138 case '$': *p++ = '\\'; *p++ = '$'; break;
139 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
140 default: *p++ = *str; break;
151 if (_case) flags |= REG_ICASE;
152 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
153 HeapFree(GetProcessHeap(), 0, mask);
156 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
161 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
163 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
189 ret = !regcomp(re, mask, REG_NOSUB);
190 HeapFree(GetProcessHeap(), 0, mask);
193 FIXME("Couldn't compile %s\n", mask);
194 SetLastError(ERROR_INVALID_PARAMETER);
199 static int match_regexp( const regex_t *re, const char *str )
201 return !regexec( re, str, 0, NULL, 0 );
204 #else /* HAVE_REGEX_H */
206 /* if we don't have regexp support, fall back to a simple string comparison */
214 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
216 if (numchar == -1) numchar = strlen( str );
218 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
219 memcpy( re->str, str, numchar );
220 re->str[numchar] = 0;
224 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
226 if (!srcfile || !*srcfile) re->str = NULL;
227 else compile_regex( srcfile, -1, re, FALSE );
231 static int match_regexp( const regex_t *re, const char *str )
233 if (!re->str) return 1;
234 if (re->icase) return !lstrcmpiA( re->str, str );
235 return !strcmp( re->str, str );
238 static void regfree( regex_t *re )
240 HeapFree( GetProcessHeap(), 0, re->str );
243 #endif /* HAVE_REGEX_H */
245 struct symt_compiland* symt_new_compiland(struct module* module,
246 unsigned long address, unsigned src_idx)
248 struct symt_compiland* sym;
250 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
251 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
252 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
254 sym->symt.tag = SymTagCompiland;
255 sym->address = address;
256 sym->source = src_idx;
257 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
262 struct symt_public* symt_new_public(struct module* module,
263 struct symt_compiland* compiland,
265 unsigned long address, unsigned size,
266 BOOL in_code, BOOL is_func)
268 struct symt_public* sym;
271 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
272 debugstr_w(module->module.ModuleName), name, address);
273 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
274 symt_find_nearest(module, address) != NULL)
276 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
278 sym->symt.tag = SymTagPublicSymbol;
279 sym->hash_elt.name = pool_strdup(&module->pool, name);
280 sym->container = compiland ? &compiland->symt : NULL;
281 sym->address = address;
283 sym->in_code = in_code;
284 sym->is_function = is_func;
285 symt_add_module_ht(module, (struct symt_ht*)sym);
288 p = vector_add(&compiland->vchildren, &module->pool);
295 struct symt_data* symt_new_global_variable(struct module* module,
296 struct symt_compiland* compiland,
297 const char* name, unsigned is_static,
298 unsigned long addr, unsigned long size,
301 struct symt_data* sym;
305 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
306 debugstr_w(module->module.ModuleName), name, addr, type);
307 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
309 sym->symt.tag = SymTagData;
310 sym->hash_elt.name = pool_strdup(&module->pool, name);
311 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
312 sym->container = compiland ? &compiland->symt : NULL;
314 sym->u.var.offset = addr;
315 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
318 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
319 debugstr_w(module->module.ModuleName), name,
320 wine_dbgstr_longlong(tsz), size);
322 symt_add_module_ht(module, (struct symt_ht*)sym);
325 p = vector_add(&compiland->vchildren, &module->pool);
332 struct symt_function* symt_new_function(struct module* module,
333 struct symt_compiland* compiland,
335 unsigned long addr, unsigned long size,
336 struct symt* sig_type)
338 struct symt_function* sym;
341 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
342 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
344 assert(!sig_type || sig_type->tag == SymTagFunctionType);
345 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
347 sym->symt.tag = SymTagFunction;
348 sym->hash_elt.name = pool_strdup(&module->pool, name);
349 sym->container = &compiland->symt;
351 sym->type = sig_type;
353 vector_init(&sym->vlines, sizeof(struct line_info), 64);
354 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
355 symt_add_module_ht(module, (struct symt_ht*)sym);
358 p = vector_add(&compiland->vchildren, &module->pool);
365 void symt_add_func_line(struct module* module, struct symt_function* func,
366 unsigned source_idx, int line_num, unsigned long offset)
368 struct line_info* dli;
369 BOOL last_matches = FALSE;
372 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
374 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
375 func, func->hash_elt.name, offset,
376 source_get(module, source_idx), line_num);
378 assert(func->symt.tag == SymTagFunction);
380 for (i=vector_length(&func->vlines)-1; i>=0; i--)
382 dli = vector_at(&func->vlines, i);
383 if (dli->is_source_file)
385 last_matches = (source_idx == dli->u.source_file);
392 /* we shouldn't have line changes on first line of function */
393 dli = vector_add(&func->vlines, &module->pool);
394 dli->is_source_file = 1;
395 dli->is_first = dli->is_last = 0;
396 dli->line_number = 0;
397 dli->u.source_file = source_idx;
399 dli = vector_add(&func->vlines, &module->pool);
400 dli->is_source_file = 0;
401 dli->is_first = dli->is_last = 0;
402 dli->line_number = line_num;
403 dli->u.pc_offset = func->address + offset;
406 /******************************************************************
407 * symt_add_func_local
409 * Adds a new local/parameter to a given function:
410 * In any cases, dt tells whether it's a local variable or a parameter
411 * If regno it's not 0:
412 * - then variable is stored in a register
413 * - otherwise, value is referenced by register + offset
414 * Otherwise, the variable is stored on the stack:
415 * - offset is then the offset from the frame register
417 struct symt_data* symt_add_func_local(struct module* module,
418 struct symt_function* func,
420 const struct location* loc,
421 struct symt_block* block,
422 struct symt* type, const char* name)
424 struct symt_data* locsym;
427 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
428 debugstr_w(module->module.ModuleName), func->hash_elt.name,
432 assert(func->symt.tag == SymTagFunction);
433 assert(dt == DataIsParam || dt == DataIsLocal);
435 locsym = pool_alloc(&module->pool, sizeof(*locsym));
436 locsym->symt.tag = SymTagData;
437 locsym->hash_elt.name = pool_strdup(&module->pool, name);
438 locsym->hash_elt.next = NULL;
440 locsym->container = &block->symt;
442 locsym->u.var = *loc;
444 p = vector_add(&block->vchildren, &module->pool);
446 p = vector_add(&func->vchildren, &module->pool);
452 struct symt_block* symt_open_func_block(struct module* module,
453 struct symt_function* func,
454 struct symt_block* parent_block,
455 unsigned pc, unsigned len)
457 struct symt_block* block;
461 assert(func->symt.tag == SymTagFunction);
463 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
464 block = pool_alloc(&module->pool, sizeof(*block));
465 block->symt.tag = SymTagBlock;
466 block->address = func->address + pc;
468 block->container = parent_block ? &parent_block->symt : &func->symt;
469 vector_init(&block->vchildren, sizeof(struct symt*), 4);
471 p = vector_add(&parent_block->vchildren, &module->pool);
473 p = vector_add(&func->vchildren, &module->pool);
479 struct symt_block* symt_close_func_block(struct module* module,
480 struct symt_function* func,
481 struct symt_block* block, unsigned pc)
484 assert(func->symt.tag == SymTagFunction);
486 if (pc) block->size = func->address + pc - block->address;
487 return (block->container->tag == SymTagBlock) ?
488 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
491 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
492 struct symt_function* func,
493 enum SymTagEnum point,
494 const struct location* loc,
497 struct symt_hierarchy_point*sym;
500 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
502 sym->symt.tag = point;
503 sym->parent = &func->symt;
505 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
506 p = vector_add(&func->vchildren, &module->pool);
512 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
515 struct line_info* dli;
518 /* We aren't adding any more locals or line numbers to this function.
519 * Free any spare memory that we might have allocated.
521 assert(func->symt.tag == SymTagFunction);
523 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
524 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
526 len = vector_length(&func->vlines);
529 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
530 dli = vector_at(&func->vlines, len); dli->is_last = 1;
535 struct symt_thunk* symt_new_thunk(struct module* module,
536 struct symt_compiland* compiland,
537 const char* name, THUNK_ORDINAL ord,
538 unsigned long addr, unsigned long size)
540 struct symt_thunk* sym;
542 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
543 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
545 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
547 sym->symt.tag = SymTagThunk;
548 sym->hash_elt.name = pool_strdup(&module->pool, name);
549 sym->container = &compiland->symt;
553 symt_add_module_ht(module, (struct symt_ht*)sym);
557 p = vector_add(&compiland->vchildren, &module->pool);
564 struct symt_data* symt_new_constant(struct module* module,
565 struct symt_compiland* compiland,
566 const char* name, struct symt* type,
569 struct symt_data* sym;
571 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
572 debugstr_w(module->module.ModuleName), name);
574 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
576 sym->symt.tag = SymTagData;
577 sym->hash_elt.name = pool_strdup(&module->pool, name);
578 sym->kind = DataIsConstant;
579 sym->container = compiland ? &compiland->symt : NULL;
582 symt_add_module_ht(module, (struct symt_ht*)sym);
586 p = vector_add(&compiland->vchildren, &module->pool);
593 struct symt_hierarchy_point* symt_new_label(struct module* module,
594 struct symt_compiland* compiland,
595 const char* name, unsigned long address)
597 struct symt_hierarchy_point* sym;
599 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
600 debugstr_w(module->module.ModuleName), name);
602 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
604 sym->symt.tag = SymTagLabel;
605 sym->hash_elt.name = pool_strdup(&module->pool, name);
606 sym->loc.kind = loc_absolute;
607 sym->loc.offset = address;
608 sym->parent = compiland ? &compiland->symt : NULL;
609 symt_add_module_ht(module, (struct symt_ht*)sym);
613 p = vector_add(&compiland->vchildren, &module->pool);
620 /* expect sym_info->MaxNameLen to be set before being called */
621 static void symt_fill_sym_info(const struct module_pair* pair,
622 const struct symt_function* func,
623 const struct symt* sym, SYMBOL_INFO* sym_info)
628 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
629 sym_info->TypeIndex = 0;
630 sym_info->info = (DWORD)sym;
631 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
632 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
633 (!sym_info->TypeIndex ||
634 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
636 sym_info->Size = (DWORD)size;
637 sym_info->ModBase = pair->requested->module.BaseOfImage;
645 const struct symt_data* data = (const struct symt_data*)sym;
649 sym_info->Flags |= SYMFLAG_PARAMETER;
653 struct location loc = data->u.var;
655 if (loc.kind >= loc_user)
656 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
661 /* for now we report error cases as a negative register number */
662 sym_info->Flags |= SYMFLAG_LOCAL;
665 sym_info->Flags |= SYMFLAG_REGISTER;
666 sym_info->Register = loc.reg;
667 sym_info->Address = 0;
670 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
671 /* FIXME: it's i386 dependent !!! */
672 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
673 sym_info->Address = loc.offset;
676 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
682 case DataIsFileStatic:
683 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
684 sym_info->Register = 0;
687 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
688 switch (data->u.value.n1.n2.vt)
690 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
691 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
692 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
693 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
694 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
695 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
696 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
698 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
704 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
708 case SymTagPublicSymbol:
709 sym_info->Flags |= SYMFLAG_EXPORT;
710 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
713 sym_info->Flags |= SYMFLAG_FUNCTION;
714 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
717 sym_info->Flags |= SYMFLAG_THUNK;
718 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
721 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
722 sym_info->Register = 0;
725 sym_info->Scope = 0; /* FIXME */
726 sym_info->Tag = sym->tag;
727 name = symt_get_name(sym);
728 if (sym_info->MaxNameLen)
730 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
731 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
732 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
734 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
735 memcpy(sym_info->Name, name, sym_info->NameLen);
736 sym_info->Name[sym_info->NameLen] = '\0';
739 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
740 sym, sym_info->Name, sym_info->Size,
741 wine_dbgstr_longlong(sym_info->Address));
746 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
748 SYMBOL_INFO* sym_info;
752 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
755 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
756 const struct symt_function* func, const struct symt* sym)
758 symt_fill_sym_info(pair, func, sym, se->sym_info);
759 if (se->index && se->sym_info->info != se->index) return FALSE;
760 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
761 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
762 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
765 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
766 const struct sym_enum* se)
769 struct symt_ht* sym = NULL;
770 struct hash_table_iter hti;
772 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
773 while ((ptr = hash_table_iter_up(&hti)))
775 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
776 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
778 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
779 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
780 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
786 static inline unsigned where_to_insert(const struct module* module, unsigned high, struct symt_ht* elt)
788 unsigned low = 0, mid = high / 2;
792 symt_get_info(&elt->symt, TI_GET_ADDRESS, &addr);
795 switch (cmp_sorttab_addr(module, mid, addr))
798 case -1: low = mid + 1; break;
799 case 1: high = mid; break;
801 mid = low + (high - low) / 2;
802 } while (low < high);
806 /***********************************************************************
809 * Rebuild sorted list of symbols for a module.
811 static BOOL resort_symbols(struct module* module)
813 if (!(module->module.NumSyms = module->num_symbols))
816 /* FIXME: what's the optimal value here ??? */
817 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
819 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
820 struct symt_ht* tmp[30];
822 delta = module->num_symbols - module->num_sorttab;
823 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
824 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
826 for (i = delta - 1; i >= 0; i--)
828 prev_ins_idx = ins_idx;
829 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
830 memmove(&module->addr_sorttab[ins_idx + i + 1],
831 &module->addr_sorttab[ins_idx],
832 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
833 module->addr_sorttab[ins_idx + i] = tmp[i];
837 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
838 module->num_sorttab = module->num_symbols;
839 return module->sortlist_valid = TRUE;
842 static void symt_get_length(struct symt* symt, ULONG64* size)
846 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
849 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
850 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
851 *size = 0x1000; /* arbitrary value */
854 /* assume addr is in module */
855 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
858 ULONG64 ref_addr, ref_size;
860 if (!module->sortlist_valid || !module->addr_sorttab)
862 if (!resort_symbols(module)) return NULL;
866 * Binary search to find closest symbol.
869 high = module->num_sorttab;
871 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
872 if (addr < ref_addr) return NULL;
875 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
876 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
877 if (addr >= ref_addr + ref_size) return NULL;
880 while (high > low + 1)
882 mid = (high + low) / 2;
883 if (cmp_sorttab_addr(module, mid, addr) < 0)
888 if (low != high && high != module->num_sorttab &&
889 cmp_sorttab_addr(module, high, addr) <= 0)
892 /* If found symbol is a public symbol, check if there are any other entries that
893 * might also have the same address, but would get better information
895 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
897 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
899 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
900 !cmp_sorttab_addr(module, low - 1, ref_addr))
902 else if (low < module->num_sorttab - 1 &&
903 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
904 !cmp_sorttab_addr(module, low + 1, ref_addr))
907 /* finally check that we fit into the found symbol */
908 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
909 if (addr < ref_addr) return NULL;
910 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
911 if (addr >= ref_addr + ref_size) return NULL;
913 return module->addr_sorttab[low];
916 static BOOL symt_enum_locals_helper(struct module_pair* pair,
917 regex_t* preg, const struct sym_enum* se,
918 struct symt_function* func, const struct vector* v)
920 struct symt* lsym = NULL;
921 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
924 for (i=0; i<vector_length(v); i++)
926 lsym = *(struct symt**)vector_at(v, i);
931 struct symt_block* block = (struct symt_block*)lsym;
932 if (pc < block->address || block->address + block->size <= pc)
934 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
939 if (match_regexp(preg, symt_get_name(lsym)))
941 if (send_symbol(se, pair, func, lsym)) return FALSE;
945 case SymTagFuncDebugStart:
946 case SymTagFuncDebugEnd:
950 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
957 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
958 const struct sym_enum* se)
960 struct module_pair pair;
962 DWORD pc = pcs->ctx_frame.InstructionOffset;
964 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
965 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
968 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
969 if (!module_get_debug(&pair)) return FALSE;
970 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
972 if (sym->symt.tag == SymTagFunction)
977 compile_regex(mask ? mask : "*", -1, &preg,
978 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
979 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
980 &((struct symt_function*)sym)->vchildren);
985 return send_symbol(se, &pair, NULL, &sym->symt);
988 /******************************************************************
991 * Helper for transforming an ANSI symbol info into a UNICODE one.
992 * Assume that MaxNameLen is the same for both version (A & W).
994 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
996 siw->SizeOfStruct = si->SizeOfStruct;
997 siw->TypeIndex = si->TypeIndex;
998 siw->Reserved[0] = si->Reserved[0];
999 siw->Reserved[1] = si->Reserved[1];
1000 siw->Index = si->info; /* FIXME: see dbghelp.h */
1001 siw->Size = si->Size;
1002 siw->ModBase = si->ModBase;
1003 siw->Flags = si->Flags;
1004 siw->Value = si->Value;
1005 siw->Address = si->Address;
1006 siw->Register = si->Register;
1007 siw->Scope = si->Scope;
1009 siw->NameLen = si->NameLen;
1010 siw->MaxNameLen = si->MaxNameLen;
1011 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1014 /******************************************************************
1017 * Core routine for most of the enumeration of symbols
1019 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1020 const struct sym_enum* se)
1022 struct module_pair pair;
1024 regex_t mod_regex, sym_regex;
1026 pair.pcs = process_find_by_handle(hProcess);
1029 /* do local variables ? */
1030 if (!Mask || !(bang = strchr(Mask, '!')))
1031 return symt_enum_locals(pair.pcs, Mask, se);
1033 if (bang == Mask) return FALSE;
1035 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1036 compile_regex(bang + 1, -1, &sym_regex,
1037 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1039 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1041 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1043 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1044 symt_enum_module(&pair, &sym_regex, se))
1048 /* not found in PE modules, retry on the ELF ones
1050 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1052 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1054 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1055 !module_get_containee(pair.pcs, pair.requested) &&
1056 module_get_debug(&pair))
1058 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1059 symt_enum_module(&pair, &sym_regex, se))
1064 regfree(&mod_regex);
1065 regfree(&sym_regex);
1068 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1069 if (!module_get_debug(&pair))
1072 /* we always ignore module name from Mask when BaseOfDll is defined */
1073 if (Mask && (bang = strchr(Mask, '!')))
1075 if (bang == Mask) return FALSE;
1079 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1080 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1081 symt_enum_module(&pair, &sym_regex, se);
1082 regfree(&sym_regex);
1087 /******************************************************************
1088 * SymEnumSymbols (DBGHELP.@)
1090 * cases BaseOfDll = 0
1091 * !foo fails always (despite what MSDN states)
1092 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1093 * no ! in Mask, lookup in local Context
1094 * cases BaseOfDll != 0
1095 * !foo fails always (despite what MSDN states)
1096 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1098 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1099 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1104 TRACE("(%p %s %s %p %p)\n",
1105 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1106 EnumSymbolsCallback, UserContext);
1108 se.cb = EnumSymbolsCallback;
1109 se.user = UserContext;
1113 se.sym_info = (PSYMBOL_INFO)se.buffer;
1115 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1120 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1122 PSYMBOL_INFOW sym_info;
1123 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1127 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1129 struct sym_enumW* sew = ctx;
1131 copy_symbolW(sew->sym_info, si);
1133 return (sew->cb)(sew->sym_info, size, sew->ctx);
1136 /******************************************************************
1137 * SymEnumSymbolsW (DBGHELP.@)
1140 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1141 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1144 struct sym_enumW sew;
1148 sew.ctx = UserContext;
1149 sew.cb = EnumSymbolsCallback;
1150 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1154 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1155 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1156 if (!maskA) return FALSE;
1157 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1159 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1160 HeapFree(GetProcessHeap(), 0, maskA);
1165 struct sym_enumerate
1168 PSYM_ENUMSYMBOLS_CALLBACK cb;
1171 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1173 struct sym_enumerate* se = ctx;
1174 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1177 /***********************************************************************
1178 * SymEnumerateSymbols (DBGHELP.@)
1180 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1181 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1184 struct sym_enumerate se;
1186 se.ctx = UserContext;
1187 se.cb = EnumSymbolsCallback;
1189 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1192 struct sym_enumerate64
1195 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1198 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1200 struct sym_enumerate64* se = ctx;
1201 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1204 /***********************************************************************
1205 * SymEnumerateSymbols64 (DBGHELP.@)
1207 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1208 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1211 struct sym_enumerate64 se;
1213 se.ctx = UserContext;
1214 se.cb = EnumSymbolsCallback;
1216 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1219 /******************************************************************
1220 * SymFromAddr (DBGHELP.@)
1223 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1224 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1226 struct module_pair pair;
1227 struct symt_ht* sym;
1229 pair.pcs = process_find_by_handle(hProcess);
1230 if (!pair.pcs) return FALSE;
1231 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1232 if (!module_get_debug(&pair)) return FALSE;
1233 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1235 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1236 *Displacement = Address - Symbol->Address;
1240 /******************************************************************
1241 * SymFromAddrW (DBGHELP.@)
1244 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1245 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1251 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1252 si = HeapAlloc(GetProcessHeap(), 0, len);
1253 if (!si) return FALSE;
1255 si->SizeOfStruct = sizeof(*si);
1256 si->MaxNameLen = Symbol->MaxNameLen;
1257 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1259 copy_symbolW(Symbol, si);
1261 HeapFree(GetProcessHeap(), 0, si);
1265 /******************************************************************
1266 * SymGetSymFromAddr (DBGHELP.@)
1269 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1270 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1272 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1273 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1275 DWORD64 Displacement64;
1277 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1278 si->SizeOfStruct = sizeof(*si);
1279 si->MaxNameLen = MAX_SYM_NAME;
1280 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1284 *Displacement = Displacement64;
1285 Symbol->Address = si->Address;
1286 Symbol->Size = si->Size;
1287 Symbol->Flags = si->Flags;
1288 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1289 lstrcpynA(Symbol->Name, si->Name, len);
1293 /******************************************************************
1294 * SymGetSymFromAddr64 (DBGHELP.@)
1297 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1298 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1300 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1301 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1303 DWORD64 Displacement64;
1305 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1306 si->SizeOfStruct = sizeof(*si);
1307 si->MaxNameLen = MAX_SYM_NAME;
1308 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1312 *Displacement = Displacement64;
1313 Symbol->Address = si->Address;
1314 Symbol->Size = si->Size;
1315 Symbol->Flags = si->Flags;
1316 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1317 lstrcpynA(Symbol->Name, si->Name, len);
1321 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1322 SYMBOL_INFO* symbol)
1324 struct hash_table_iter hti;
1326 struct symt_ht* sym = NULL;
1327 struct module_pair pair;
1330 if (!(pair.requested = module)) return FALSE;
1331 if (!module_get_debug(&pair)) return FALSE;
1333 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1334 while ((ptr = hash_table_iter_up(&hti)))
1336 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1338 if (!strcmp(sym->hash_elt.name, name))
1340 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1347 /******************************************************************
1348 * SymFromName (DBGHELP.@)
1351 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1353 struct process* pcs = process_find_by_handle(hProcess);
1354 struct module* module;
1357 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1358 if (!pcs) return FALSE;
1359 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1360 name = strchr(Name, '!');
1364 assert(name - Name < sizeof(tmp));
1365 memcpy(tmp, Name, name - Name);
1366 tmp[name - Name] = '\0';
1367 module = module_find_by_nameA(pcs, tmp);
1368 return find_name(pcs, module, name + 1, Symbol);
1370 for (module = pcs->lmodules; module; module = module->next)
1372 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1375 /* not found in PE modules, retry on the ELF ones
1377 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1379 for (module = pcs->lmodules; module; module = module->next)
1381 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1382 !module_get_containee(pcs, module) &&
1383 find_name(pcs, module, Name, Symbol))
1390 /***********************************************************************
1391 * SymGetSymFromName64 (DBGHELP.@)
1393 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1395 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1396 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1399 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1400 si->SizeOfStruct = sizeof(*si);
1401 si->MaxNameLen = MAX_SYM_NAME;
1402 if (!SymFromName(hProcess, Name, si)) return FALSE;
1404 Symbol->Address = si->Address;
1405 Symbol->Size = si->Size;
1406 Symbol->Flags = si->Flags;
1407 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1408 lstrcpynA(Symbol->Name, si->Name, len);
1412 /***********************************************************************
1413 * SymGetSymFromName (DBGHELP.@)
1415 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1417 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1418 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1421 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1422 si->SizeOfStruct = sizeof(*si);
1423 si->MaxNameLen = MAX_SYM_NAME;
1424 if (!SymFromName(hProcess, Name, si)) return FALSE;
1426 Symbol->Address = si->Address;
1427 Symbol->Size = si->Size;
1428 Symbol->Flags = si->Flags;
1429 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1430 lstrcpynA(Symbol->Name, si->Name, len);
1434 /******************************************************************
1435 * sym_fill_func_line_info
1437 * fills information about a file
1439 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1440 DWORD addr, IMAGEHLP_LINE* line)
1442 struct line_info* dli = NULL;
1446 assert(func->symt.tag == SymTagFunction);
1448 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1450 dli = vector_at(&func->vlines, i);
1451 if (!dli->is_source_file)
1453 if (found || dli->u.pc_offset > addr) continue;
1454 line->LineNumber = dli->line_number;
1455 line->Address = dli->u.pc_offset;
1462 line->FileName = (char*)source_get(module, dli->u.source_file);
1469 /***********************************************************************
1470 * SymGetSymNext64 (DBGHELP.@)
1472 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1475 * get module from Symbol.Address
1476 * get index in module.addr_sorttab of Symbol.Address
1478 * if out of module bounds, move to next module in process address space
1480 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1481 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1485 /***********************************************************************
1486 * SymGetSymNext (DBGHELP.@)
1488 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1490 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1491 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1495 /***********************************************************************
1496 * SymGetSymPrev64 (DBGHELP.@)
1498 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1500 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1501 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1505 /***********************************************************************
1506 * SymGetSymPrev (DBGHELP.@)
1508 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1510 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1511 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1515 /******************************************************************
1516 * SymGetLineFromAddr (DBGHELP.@)
1519 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1520 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1522 struct module_pair pair;
1523 struct symt_ht* symt;
1525 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1527 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1529 pair.pcs = process_find_by_handle(hProcess);
1530 if (!pair.pcs) return FALSE;
1531 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1532 if (!module_get_debug(&pair)) return FALSE;
1533 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1535 if (symt->symt.tag != SymTagFunction) return FALSE;
1536 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1537 dwAddr, Line)) return FALSE;
1538 *pdwDisplacement = dwAddr - Line->Address;
1542 /******************************************************************
1543 * copy_line_64_from_32 (internal)
1546 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1549 l64->Key = l32->Key;
1550 l64->LineNumber = l32->LineNumber;
1551 l64->FileName = l32->FileName;
1552 l64->Address = l32->Address;
1555 /******************************************************************
1556 * copy_line_W64_from_32 (internal)
1559 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1563 l64->Key = l32->Key;
1564 l64->LineNumber = l32->LineNumber;
1565 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1566 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1567 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1568 l64->Address = l32->Address;
1571 /******************************************************************
1572 * copy_line_32_from_64 (internal)
1575 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1578 l32->Key = l64->Key;
1579 l32->LineNumber = l64->LineNumber;
1580 l32->FileName = l64->FileName;
1581 l32->Address = l64->Address;
1584 /******************************************************************
1585 * SymGetLineFromAddr64 (DBGHELP.@)
1588 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1589 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1591 IMAGEHLP_LINE line32;
1593 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1594 if (!validate_addr64(dwAddr)) return FALSE;
1595 line32.SizeOfStruct = sizeof(line32);
1596 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1598 copy_line_64_from_32(Line, &line32);
1602 /******************************************************************
1603 * SymGetLineFromAddrW64 (DBGHELP.@)
1606 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1607 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1609 struct process* pcs = process_find_by_handle(hProcess);
1610 IMAGEHLP_LINE line32;
1612 if (!pcs) return FALSE;
1613 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1614 if (!validate_addr64(dwAddr)) return FALSE;
1615 line32.SizeOfStruct = sizeof(line32);
1616 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1618 copy_line_W64_from_32(pcs, Line, &line32);
1622 /******************************************************************
1623 * SymGetLinePrev (DBGHELP.@)
1626 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1628 struct module_pair pair;
1629 struct line_info* li;
1630 BOOL in_search = FALSE;
1632 TRACE("(%p %p)\n", hProcess, Line);
1634 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1636 pair.pcs = process_find_by_handle(hProcess);
1637 if (!pair.pcs) return FALSE;
1638 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1639 if (!module_get_debug(&pair)) return FALSE;
1641 if (Line->Key == 0) return FALSE;
1643 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1644 * element we have to go back until we find the prev one to get the real
1645 * source file name for the DLIT_OFFSET element just before
1646 * the first DLIT_SOURCEFILE
1648 while (!li->is_first)
1651 if (!li->is_source_file)
1653 Line->LineNumber = li->line_number;
1654 Line->Address = li->u.pc_offset;
1656 if (!in_search) return TRUE;
1662 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1668 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1672 /******************************************************************
1673 * SymGetLinePrev64 (DBGHELP.@)
1676 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1678 IMAGEHLP_LINE line32;
1680 line32.SizeOfStruct = sizeof(line32);
1681 copy_line_32_from_64(&line32, Line);
1682 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1683 copy_line_64_from_32(Line, &line32);
1687 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1689 struct line_info* li;
1691 if (line->Key == 0) return FALSE;
1693 while (!li->is_last)
1696 if (!li->is_source_file)
1698 line->LineNumber = li->line_number;
1699 line->Address = li->u.pc_offset;
1703 line->FileName = (char*)source_get(module, li->u.source_file);
1708 /******************************************************************
1709 * SymGetLineNext (DBGHELP.@)
1712 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1714 struct module_pair pair;
1716 TRACE("(%p %p)\n", hProcess, Line);
1718 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1719 pair.pcs = process_find_by_handle(hProcess);
1720 if (!pair.pcs) return FALSE;
1721 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1722 if (!module_get_debug(&pair)) return FALSE;
1724 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1725 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1729 /******************************************************************
1730 * SymGetLineNext64 (DBGHELP.@)
1733 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1735 IMAGEHLP_LINE line32;
1737 line32.SizeOfStruct = sizeof(line32);
1738 copy_line_32_from_64(&line32, Line);
1739 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1740 copy_line_64_from_32(Line, &line32);
1744 /***********************************************************************
1745 * SymFunctionTableAccess (DBGHELP.@)
1747 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1749 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1753 /***********************************************************************
1754 * SymFunctionTableAccess64 (DBGHELP.@)
1756 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1758 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1762 /***********************************************************************
1763 * SymUnDName (DBGHELP.@)
1765 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1767 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1768 UNDNAME_COMPLETE) != 0;
1771 /***********************************************************************
1772 * SymUnDName64 (DBGHELP.@)
1774 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1776 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1777 UNDNAME_COMPLETE) != 0;
1780 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1781 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1783 /***********************************************************************
1784 * UnDecorateSymbolName (DBGHELP.@)
1786 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1787 DWORD UndecoratedLength, DWORD Flags)
1789 /* undocumented from msvcrt */
1790 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1791 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1793 TRACE("(%s, %p, %d, 0x%08x)\n",
1794 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1798 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1799 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1800 if (!p_undname) return 0;
1803 if (!UnDecoratedName) return 0;
1804 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1805 und_alloc, und_free, Flags))
1807 return strlen(UnDecoratedName);
1810 /******************************************************************
1811 * SymMatchString (DBGHELP.@)
1814 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1819 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1821 compile_regex(re, -1, &preg, _case);
1822 ret = match_regexp(&preg, string);
1827 /******************************************************************
1828 * SymSearch (DBGHELP.@)
1830 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1831 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1832 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1833 PVOID UserContext, DWORD Options)
1837 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1838 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1839 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1840 UserContext, Options);
1842 if (Options != SYMSEARCH_GLOBALSONLY)
1844 FIXME("Unsupported searching with options (%x)\n", Options);
1845 SetLastError(ERROR_INVALID_PARAMETER);
1849 se.cb = EnumSymbolsCallback;
1850 se.user = UserContext;
1854 se.sym_info = (PSYMBOL_INFO)se.buffer;
1856 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1859 /******************************************************************
1860 * SymSearchW (DBGHELP.@)
1862 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1863 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1864 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1865 PVOID UserContext, DWORD Options)
1867 struct sym_enumW sew;
1871 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1872 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1873 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1874 UserContext, Options);
1876 sew.ctx = UserContext;
1877 sew.cb = EnumSymbolsCallback;
1878 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1882 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1883 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1884 if (!maskA) return FALSE;
1885 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1887 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1888 sym_enumW, &sew, Options);
1889 HeapFree(GetProcessHeap(), 0, maskA);
1894 /******************************************************************
1895 * SymAddSymbol (DBGHELP.@)
1898 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1899 DWORD64 addr, DWORD size, DWORD flags)
1901 WCHAR nameW[MAX_SYM_NAME];
1903 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1904 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1907 /******************************************************************
1908 * SymAddSymbolW (DBGHELP.@)
1911 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1912 DWORD64 addr, DWORD size, DWORD flags)
1914 struct module_pair pair;
1916 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1918 pair.pcs = process_find_by_handle(hProcess);
1919 if (!pair.pcs) return FALSE;
1920 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1921 if (!module_get_debug(&pair)) return FALSE;
1923 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1927 /******************************************************************
1928 * SymSetScopeFromAddr (DBGHELP.@)
1930 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1932 struct process* pcs;
1934 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1936 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1940 /******************************************************************
1941 * SymEnumLines (DBGHELP.@)
1944 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1945 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1947 struct module_pair pair;
1948 struct hash_table_iter hti;
1949 struct symt_ht* sym;
1951 struct line_info* dli;
1956 if (!cb) return FALSE;
1957 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1959 pair.pcs = process_find_by_handle(hProcess);
1960 if (!pair.pcs) return FALSE;
1961 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1962 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1963 if (!module_get_debug(&pair)) return FALSE;
1964 if (!compile_file_regex(&re, srcfile)) return FALSE;
1966 sci.SizeOfStruct = sizeof(sci);
1969 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1970 while ((ptr = hash_table_iter_up(&hti)))
1974 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1975 if (sym->symt.tag != SymTagFunction) continue;
1977 sci.FileName[0] = '\0';
1978 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1980 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1981 if (dli->is_source_file)
1983 file = source_get(pair.effective, dli->u.source_file);
1984 if (!match_regexp(&re, file)) file = "";
1985 strcpy(sci.FileName, file);
1987 else if (sci.FileName[0])
1990 sci.Obj[0] = '\0'; /* FIXME */
1991 sci.LineNumber = dli->line_number;
1992 sci.Address = dli->u.pc_offset;
1993 if (!cb(&sci, user)) break;