2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include <sys/types.h>
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
44 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
46 if (a1 > a2) return 1;
47 if (a1 < a2) return -1;
51 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
55 symt_get_info(module, &module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
56 return cmp_addr(ref, addr);
59 struct module* symt_cmp_addr_module = NULL;
61 int symt_cmp_addr(const void* p1, const void* p2)
63 const struct symt* sym1 = *(const struct symt* const *)p1;
64 const struct symt* sym2 = *(const struct symt* const *)p2;
67 symt_get_info(symt_cmp_addr_module, sym1, TI_GET_ADDRESS, &a1);
68 symt_get_info(symt_cmp_addr_module, sym2, TI_GET_ADDRESS, &a2);
69 return cmp_addr(a1, a2);
72 DWORD symt_ptr2index(struct module* module, const struct symt* sym)
75 const struct symt** c;
76 int len = vector_length(&module->vsymt), i;
78 /* FIXME: this is inefficient */
79 for (i = 0; i < len; i++)
81 if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
85 c = vector_add(&module->vsymt, &module->pool);
93 struct symt* symt_index2ptr(struct module* module, DWORD id)
96 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
97 return *(struct symt**)vector_at(&module->vsymt, id);
99 return (struct symt*)id;
103 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
105 struct symt_ht** new;
108 if (sz <= module->sorttab_size) return TRUE;
109 if (module->addr_sorttab)
111 size = module->sorttab_size * 2;
112 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
113 size * sizeof(struct symt_ht*));
118 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
120 if (!new) return FALSE;
121 module->sorttab_size = size;
122 module->addr_sorttab = new;
126 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
130 hash_table_add(&module->ht_symbols, &ht->hash_elt);
131 /* Don't store in sorttab a symbol without address, they are of
132 * no use here (e.g. constant values)
134 if (symt_get_info(module, &ht->symt, TI_GET_ADDRESS, &addr) &&
135 symt_grow_sorttab(module, module->num_symbols + 1))
137 module->addr_sorttab[module->num_symbols++] = ht;
138 module->sortlist_valid = FALSE;
144 /* transforms a dbghelp's regular expression into a POSIX one
145 * Here are the valid dbghelp reg ex characters:
146 * * 0 or more characters
147 * ? a single character
149 * # 0 or more of preceding char
150 * + 1 or more of preceding char
151 * escapes \ on #, ?, [, ], *, +. don't work on -
153 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
156 BOOL in_escape = FALSE;
157 unsigned flags = REG_NOSUB;
159 if (numchar == -1) numchar = strlen( str );
161 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
164 while (*str && numchar--)
166 /* FIXME: this shouldn't be valid on '-' */
175 case '\\': in_escape = TRUE; break;
176 case '*': *p++ = '.'; *p++ = '*'; break;
177 case '?': *p++ = '.'; break;
178 case '#': *p++ = '*'; break;
179 /* escape some valid characters in dbghelp reg exp:s */
180 case '$': *p++ = '\\'; *p++ = '$'; break;
181 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
182 default: *p++ = *str; break;
193 if (_case) flags |= REG_ICASE;
194 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
195 HeapFree(GetProcessHeap(), 0, mask);
198 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
203 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
205 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
235 ret = !regcomp(re, mask, REG_NOSUB);
236 HeapFree(GetProcessHeap(), 0, mask);
239 FIXME("Couldn't compile %s\n", mask);
240 SetLastError(ERROR_INVALID_PARAMETER);
245 static int match_regexp( const regex_t *re, const char *str )
247 return !regexec( re, str, 0, NULL, 0 );
250 #else /* HAVE_REGEX_H */
252 /* if we don't have regexp support, fall back to a simple string comparison */
260 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
262 if (numchar == -1) numchar = strlen( str );
264 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
265 memcpy( re->str, str, numchar );
266 re->str[numchar] = 0;
270 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
272 if (!srcfile || !*srcfile) re->str = NULL;
273 else compile_regex( srcfile, -1, re, FALSE );
277 static int match_regexp( const regex_t *re, const char *str )
279 if (!re->str) return 1;
280 if (re->icase) return !lstrcmpiA( re->str, str );
281 return !strcmp( re->str, str );
284 static void regfree( regex_t *re )
286 HeapFree( GetProcessHeap(), 0, re->str );
289 #endif /* HAVE_REGEX_H */
291 struct symt_compiland* symt_new_compiland(struct module* module,
292 unsigned long address, unsigned src_idx)
294 struct symt_compiland* sym;
296 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
297 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
298 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
300 sym->symt.tag = SymTagCompiland;
301 sym->address = address;
302 sym->source = src_idx;
303 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
308 struct symt_public* symt_new_public(struct module* module,
309 struct symt_compiland* compiland,
311 unsigned long address, unsigned size)
313 struct symt_public* sym;
316 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
317 debugstr_w(module->module.ModuleName), name, address);
318 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
319 symt_find_nearest(module, address) != NULL)
321 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
323 sym->symt.tag = SymTagPublicSymbol;
324 sym->hash_elt.name = pool_strdup(&module->pool, name);
325 sym->container = compiland ? &compiland->symt : NULL;
326 sym->address = address;
328 symt_add_module_ht(module, (struct symt_ht*)sym);
331 p = vector_add(&compiland->vchildren, &module->pool);
338 struct symt_data* symt_new_global_variable(struct module* module,
339 struct symt_compiland* compiland,
340 const char* name, unsigned is_static,
341 struct location loc, unsigned long size,
344 struct symt_data* sym;
348 TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
349 debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
350 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
352 sym->symt.tag = SymTagData;
353 sym->hash_elt.name = pool_strdup(&module->pool, name);
354 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
355 sym->container = compiland ? &compiland->symt : NULL;
358 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
361 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
362 debugstr_w(module->module.ModuleName), name,
363 wine_dbgstr_longlong(tsz), size);
365 symt_add_module_ht(module, (struct symt_ht*)sym);
368 p = vector_add(&compiland->vchildren, &module->pool);
375 struct symt_function* symt_new_function(struct module* module,
376 struct symt_compiland* compiland,
378 unsigned long addr, unsigned long size,
379 struct symt* sig_type)
381 struct symt_function* sym;
384 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
385 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
387 assert(!sig_type || sig_type->tag == SymTagFunctionType);
388 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
390 sym->symt.tag = SymTagFunction;
391 sym->hash_elt.name = pool_strdup(&module->pool, name);
392 sym->container = &compiland->symt;
394 sym->type = sig_type;
396 vector_init(&sym->vlines, sizeof(struct line_info), 64);
397 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
398 symt_add_module_ht(module, (struct symt_ht*)sym);
401 p = vector_add(&compiland->vchildren, &module->pool);
408 void symt_add_func_line(struct module* module, struct symt_function* func,
409 unsigned source_idx, int line_num, unsigned long offset)
411 struct line_info* dli;
412 BOOL last_matches = FALSE;
415 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
417 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
418 func, func->hash_elt.name, offset,
419 source_get(module, source_idx), line_num);
421 assert(func->symt.tag == SymTagFunction);
423 for (i=vector_length(&func->vlines)-1; i>=0; i--)
425 dli = vector_at(&func->vlines, i);
426 if (dli->is_source_file)
428 last_matches = (source_idx == dli->u.source_file);
435 /* we shouldn't have line changes on first line of function */
436 dli = vector_add(&func->vlines, &module->pool);
437 dli->is_source_file = 1;
438 dli->is_first = dli->is_last = 0;
439 dli->line_number = 0;
440 dli->u.source_file = source_idx;
442 dli = vector_add(&func->vlines, &module->pool);
443 dli->is_source_file = 0;
444 dli->is_first = dli->is_last = 0;
445 dli->line_number = line_num;
446 dli->u.pc_offset = func->address + offset;
449 /******************************************************************
450 * symt_add_func_local
452 * Adds a new local/parameter to a given function:
453 * In any cases, dt tells whether it's a local variable or a parameter
454 * If regno it's not 0:
455 * - then variable is stored in a register
456 * - otherwise, value is referenced by register + offset
457 * Otherwise, the variable is stored on the stack:
458 * - offset is then the offset from the frame register
460 struct symt_data* symt_add_func_local(struct module* module,
461 struct symt_function* func,
463 const struct location* loc,
464 struct symt_block* block,
465 struct symt* type, const char* name)
467 struct symt_data* locsym;
470 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
471 debugstr_w(module->module.ModuleName), func->hash_elt.name,
475 assert(func->symt.tag == SymTagFunction);
476 assert(dt == DataIsParam || dt == DataIsLocal);
478 locsym = pool_alloc(&module->pool, sizeof(*locsym));
479 locsym->symt.tag = SymTagData;
480 locsym->hash_elt.name = pool_strdup(&module->pool, name);
481 locsym->hash_elt.next = NULL;
483 locsym->container = block ? &block->symt : &func->symt;
485 locsym->u.var = *loc;
487 p = vector_add(&block->vchildren, &module->pool);
489 p = vector_add(&func->vchildren, &module->pool);
495 struct symt_block* symt_open_func_block(struct module* module,
496 struct symt_function* func,
497 struct symt_block* parent_block,
498 unsigned pc, unsigned len)
500 struct symt_block* block;
504 assert(func->symt.tag == SymTagFunction);
506 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
507 block = pool_alloc(&module->pool, sizeof(*block));
508 block->symt.tag = SymTagBlock;
509 block->address = func->address + pc;
511 block->container = parent_block ? &parent_block->symt : &func->symt;
512 vector_init(&block->vchildren, sizeof(struct symt*), 4);
514 p = vector_add(&parent_block->vchildren, &module->pool);
516 p = vector_add(&func->vchildren, &module->pool);
522 struct symt_block* symt_close_func_block(struct module* module,
523 const struct symt_function* func,
524 struct symt_block* block, unsigned pc)
527 assert(func->symt.tag == SymTagFunction);
529 if (pc) block->size = func->address + pc - block->address;
530 return (block->container->tag == SymTagBlock) ?
531 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
534 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
535 struct symt_function* func,
536 enum SymTagEnum point,
537 const struct location* loc,
540 struct symt_hierarchy_point*sym;
543 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
545 sym->symt.tag = point;
546 sym->parent = &func->symt;
548 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
549 p = vector_add(&func->vchildren, &module->pool);
555 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
558 struct line_info* dli;
561 /* We aren't adding any more locals or line numbers to this function.
562 * Free any spare memory that we might have allocated.
564 assert(func->symt.tag == SymTagFunction);
566 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
567 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
569 len = vector_length(&func->vlines);
572 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
573 dli = vector_at(&func->vlines, len); dli->is_last = 1;
578 struct symt_thunk* symt_new_thunk(struct module* module,
579 struct symt_compiland* compiland,
580 const char* name, THUNK_ORDINAL ord,
581 unsigned long addr, unsigned long size)
583 struct symt_thunk* sym;
585 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
586 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
588 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
590 sym->symt.tag = SymTagThunk;
591 sym->hash_elt.name = pool_strdup(&module->pool, name);
592 sym->container = &compiland->symt;
596 symt_add_module_ht(module, (struct symt_ht*)sym);
600 p = vector_add(&compiland->vchildren, &module->pool);
607 struct symt_data* symt_new_constant(struct module* module,
608 struct symt_compiland* compiland,
609 const char* name, struct symt* type,
612 struct symt_data* sym;
614 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
615 debugstr_w(module->module.ModuleName), name);
617 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
619 sym->symt.tag = SymTagData;
620 sym->hash_elt.name = pool_strdup(&module->pool, name);
621 sym->kind = DataIsConstant;
622 sym->container = compiland ? &compiland->symt : NULL;
625 symt_add_module_ht(module, (struct symt_ht*)sym);
629 p = vector_add(&compiland->vchildren, &module->pool);
636 struct symt_hierarchy_point* symt_new_label(struct module* module,
637 struct symt_compiland* compiland,
638 const char* name, unsigned long address)
640 struct symt_hierarchy_point* sym;
642 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
643 debugstr_w(module->module.ModuleName), name);
645 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
647 sym->symt.tag = SymTagLabel;
648 sym->hash_elt.name = pool_strdup(&module->pool, name);
649 sym->loc.kind = loc_absolute;
650 sym->loc.offset = address;
651 sym->parent = compiland ? &compiland->symt : NULL;
652 symt_add_module_ht(module, (struct symt_ht*)sym);
656 p = vector_add(&compiland->vchildren, &module->pool);
663 /* expect sym_info->MaxNameLen to be set before being called */
664 static void symt_fill_sym_info(struct module_pair* pair,
665 const struct symt_function* func,
666 const struct symt* sym, SYMBOL_INFO* sym_info)
671 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
672 sym_info->TypeIndex = 0;
673 sym_info->info = symt_ptr2index(pair->effective, sym);
674 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
675 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
676 (!sym_info->TypeIndex ||
677 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
678 TI_GET_LENGTH, &size)))
680 sym_info->Size = (DWORD)size;
681 sym_info->ModBase = pair->requested->module.BaseOfImage;
689 const struct symt_data* data = (const struct symt_data*)sym;
693 sym_info->Flags |= SYMFLAG_PARAMETER;
697 struct location loc = data->u.var;
699 if (loc.kind >= loc_user)
702 struct module_format* modfmt;
704 for (i = 0; i < DFI_LAST; i++)
706 modfmt = pair->effective->format_info[i];
707 if (modfmt && modfmt->loc_compute)
709 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
717 /* for now we report error cases as a negative register number */
718 sym_info->Flags |= SYMFLAG_LOCAL;
721 sym_info->Flags |= SYMFLAG_REGISTER;
722 sym_info->Register = loc.reg;
723 sym_info->Address = 0;
726 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
727 /* FIXME: it's i386 dependent !!! */
728 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
729 sym_info->Address = loc.offset;
732 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
733 sym_info->Value = loc.offset;
736 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
742 case DataIsFileStatic:
743 switch (data->u.var.kind)
746 sym_info->Flags |= SYMFLAG_TLSREL;
749 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
750 sym_info->Register = 0;
753 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
758 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
759 switch (data->u.value.n1.n2.vt)
761 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
762 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
763 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
764 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
765 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
766 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
767 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
768 case VT_EMPTY: sym_info->Value = 0; break;
770 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
776 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
780 case SymTagPublicSymbol:
781 sym_info->Flags |= SYMFLAG_EXPORT;
782 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
785 sym_info->Flags |= SYMFLAG_FUNCTION;
786 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
789 sym_info->Flags |= SYMFLAG_THUNK;
790 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
793 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
794 sym_info->Register = 0;
797 sym_info->Scope = 0; /* FIXME */
798 sym_info->Tag = sym->tag;
799 name = symt_get_name(sym);
800 if (sym_info->MaxNameLen)
802 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
803 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
804 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
806 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
807 memcpy(sym_info->Name, name, sym_info->NameLen);
808 sym_info->Name[sym_info->NameLen] = '\0';
811 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
812 sym, sym_info->Name, sym_info->Size,
813 wine_dbgstr_longlong(sym_info->Address));
818 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
820 SYMBOL_INFO* sym_info;
824 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
827 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
828 const struct symt_function* func, const struct symt* sym)
830 symt_fill_sym_info(pair, func, sym, se->sym_info);
831 if (se->index && se->sym_info->info != se->index) return FALSE;
832 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
833 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
834 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
837 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
838 const struct sym_enum* se)
841 struct symt_ht* sym = NULL;
842 struct hash_table_iter hti;
844 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
845 while ((ptr = hash_table_iter_up(&hti)))
847 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
848 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
850 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
851 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
852 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
858 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
860 unsigned low = 0, mid = high / 2;
864 symt_get_info(module, &elt->symt, TI_GET_ADDRESS, &addr);
867 switch (cmp_sorttab_addr(module, mid, addr))
870 case -1: low = mid + 1; break;
871 case 1: high = mid; break;
873 mid = low + (high - low) / 2;
874 } while (low < high);
878 /***********************************************************************
881 * Rebuild sorted list of symbols for a module.
883 static BOOL resort_symbols(struct module* module)
885 if (!(module->module.NumSyms = module->num_symbols))
888 /* FIXME: what's the optimal value here ??? */
889 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
891 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
892 struct symt_ht* tmp[30];
894 delta = module->num_symbols - module->num_sorttab;
895 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
896 symt_cmp_addr_module = module;
897 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
899 for (i = delta - 1; i >= 0; i--)
901 prev_ins_idx = ins_idx;
902 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
903 memmove(&module->addr_sorttab[ins_idx + i + 1],
904 &module->addr_sorttab[ins_idx],
905 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
906 module->addr_sorttab[ins_idx + i] = tmp[i];
911 symt_cmp_addr_module = module;
912 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
914 module->num_sorttab = module->num_symbols;
915 return module->sortlist_valid = TRUE;
918 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
922 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
925 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
926 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
927 *size = 0x1000; /* arbitrary value */
930 /* assume addr is in module */
931 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
934 ULONG64 ref_addr, ref_size;
936 if (!module->sortlist_valid || !module->addr_sorttab)
938 if (!resort_symbols(module)) return NULL;
942 * Binary search to find closest symbol.
945 high = module->num_sorttab;
947 symt_get_info(module, &module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
948 if (addr < ref_addr) return NULL;
951 symt_get_info(module, &module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
952 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
953 if (addr >= ref_addr + ref_size) return NULL;
956 while (high > low + 1)
958 mid = (high + low) / 2;
959 if (cmp_sorttab_addr(module, mid, addr) < 0)
964 if (low != high && high != module->num_sorttab &&
965 cmp_sorttab_addr(module, high, addr) <= 0)
968 /* If found symbol is a public symbol, check if there are any other entries that
969 * might also have the same address, but would get better information
971 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
973 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
975 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
976 !cmp_sorttab_addr(module, low - 1, ref_addr))
978 else if (low < module->num_sorttab - 1 &&
979 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
980 !cmp_sorttab_addr(module, low + 1, ref_addr))
983 /* finally check that we fit into the found symbol */
984 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
985 if (addr < ref_addr) return NULL;
986 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
987 if (addr >= ref_addr + ref_size) return NULL;
989 return module->addr_sorttab[low];
992 static BOOL symt_enum_locals_helper(struct module_pair* pair,
993 regex_t* preg, const struct sym_enum* se,
994 struct symt_function* func, const struct vector* v)
996 struct symt* lsym = NULL;
997 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
1000 for (i=0; i<vector_length(v); i++)
1002 lsym = *(struct symt**)vector_at(v, i);
1007 struct symt_block* block = (struct symt_block*)lsym;
1008 if (pc < block->address || block->address + block->size <= pc)
1010 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
1015 if (match_regexp(preg, symt_get_name(lsym)))
1017 if (send_symbol(se, pair, func, lsym)) return FALSE;
1021 case SymTagFuncDebugStart:
1022 case SymTagFuncDebugEnd:
1026 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1033 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
1034 const struct sym_enum* se)
1036 struct module_pair pair;
1037 struct symt_ht* sym;
1038 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
1040 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1041 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1044 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1045 if (!module_get_debug(&pair)) return FALSE;
1046 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1048 if (sym->symt.tag == SymTagFunction)
1053 compile_regex(mask ? mask : "*", -1, &preg,
1054 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1055 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1056 &((struct symt_function*)sym)->vchildren);
1063 /******************************************************************
1066 * Helper for transforming an ANSI symbol info into a UNICODE one.
1067 * Assume that MaxNameLen is the same for both version (A & W).
1069 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1071 siw->SizeOfStruct = si->SizeOfStruct;
1072 siw->TypeIndex = si->TypeIndex;
1073 siw->Reserved[0] = si->Reserved[0];
1074 siw->Reserved[1] = si->Reserved[1];
1075 siw->Index = si->info; /* FIXME: see dbghelp.h */
1076 siw->Size = si->Size;
1077 siw->ModBase = si->ModBase;
1078 siw->Flags = si->Flags;
1079 siw->Value = si->Value;
1080 siw->Address = si->Address;
1081 siw->Register = si->Register;
1082 siw->Scope = si->Scope;
1084 siw->NameLen = si->NameLen;
1085 siw->MaxNameLen = si->MaxNameLen;
1086 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1089 /******************************************************************
1092 * Core routine for most of the enumeration of symbols
1094 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1095 const struct sym_enum* se)
1097 struct module_pair pair;
1099 regex_t mod_regex, sym_regex;
1101 pair.pcs = process_find_by_handle(hProcess);
1102 if (!pair.pcs) return FALSE;
1105 /* do local variables ? */
1106 if (!Mask || !(bang = strchr(Mask, '!')))
1107 return symt_enum_locals(pair.pcs, Mask, se);
1109 if (bang == Mask) return FALSE;
1111 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1112 compile_regex(bang + 1, -1, &sym_regex,
1113 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1115 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1117 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1119 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1120 symt_enum_module(&pair, &sym_regex, se))
1124 /* not found in PE modules, retry on the ELF ones
1126 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1128 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1130 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1131 !module_get_containee(pair.pcs, pair.requested) &&
1132 module_get_debug(&pair))
1134 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1135 symt_enum_module(&pair, &sym_regex, se))
1140 regfree(&mod_regex);
1141 regfree(&sym_regex);
1144 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1145 if (!module_get_debug(&pair))
1148 /* we always ignore module name from Mask when BaseOfDll is defined */
1149 if (Mask && (bang = strchr(Mask, '!')))
1151 if (bang == Mask) return FALSE;
1155 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1156 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1157 symt_enum_module(&pair, &sym_regex, se);
1158 regfree(&sym_regex);
1163 /******************************************************************
1164 * SymEnumSymbols (DBGHELP.@)
1166 * cases BaseOfDll = 0
1167 * !foo fails always (despite what MSDN states)
1168 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1169 * no ! in Mask, lookup in local Context
1170 * cases BaseOfDll != 0
1171 * !foo fails always (despite what MSDN states)
1172 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1174 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1175 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1180 TRACE("(%p %s %s %p %p)\n",
1181 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1182 EnumSymbolsCallback, UserContext);
1184 se.cb = EnumSymbolsCallback;
1185 se.user = UserContext;
1189 se.sym_info = (PSYMBOL_INFO)se.buffer;
1191 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1196 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1198 PSYMBOL_INFOW sym_info;
1199 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1203 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1205 struct sym_enumW* sew = ctx;
1207 copy_symbolW(sew->sym_info, si);
1209 return (sew->cb)(sew->sym_info, size, sew->ctx);
1212 /******************************************************************
1213 * SymEnumSymbolsW (DBGHELP.@)
1216 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1217 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1220 struct sym_enumW sew;
1224 sew.ctx = UserContext;
1225 sew.cb = EnumSymbolsCallback;
1226 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1230 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1231 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1232 if (!maskA) return FALSE;
1233 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1235 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1236 HeapFree(GetProcessHeap(), 0, maskA);
1241 struct sym_enumerate
1244 PSYM_ENUMSYMBOLS_CALLBACK cb;
1247 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1249 struct sym_enumerate* se = ctx;
1250 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1253 /***********************************************************************
1254 * SymEnumerateSymbols (DBGHELP.@)
1256 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1257 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1260 struct sym_enumerate se;
1262 se.ctx = UserContext;
1263 se.cb = EnumSymbolsCallback;
1265 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1268 struct sym_enumerate64
1271 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1274 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1276 struct sym_enumerate64* se = ctx;
1277 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1280 /***********************************************************************
1281 * SymEnumerateSymbols64 (DBGHELP.@)
1283 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1284 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1287 struct sym_enumerate64 se;
1289 se.ctx = UserContext;
1290 se.cb = EnumSymbolsCallback;
1292 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1295 /******************************************************************
1296 * SymFromAddr (DBGHELP.@)
1299 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1300 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1302 struct module_pair pair;
1303 struct symt_ht* sym;
1305 pair.pcs = process_find_by_handle(hProcess);
1306 if (!pair.pcs) return FALSE;
1307 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1308 if (!module_get_debug(&pair)) return FALSE;
1309 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1311 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1312 *Displacement = Address - Symbol->Address;
1316 /******************************************************************
1317 * SymFromAddrW (DBGHELP.@)
1320 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1321 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1327 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1328 si = HeapAlloc(GetProcessHeap(), 0, len);
1329 if (!si) return FALSE;
1331 si->SizeOfStruct = sizeof(*si);
1332 si->MaxNameLen = Symbol->MaxNameLen;
1333 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1335 copy_symbolW(Symbol, si);
1337 HeapFree(GetProcessHeap(), 0, si);
1341 /******************************************************************
1342 * SymGetSymFromAddr (DBGHELP.@)
1345 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1346 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1348 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1349 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1351 DWORD64 Displacement64;
1353 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1354 si->SizeOfStruct = sizeof(*si);
1355 si->MaxNameLen = MAX_SYM_NAME;
1356 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1360 *Displacement = Displacement64;
1361 Symbol->Address = si->Address;
1362 Symbol->Size = si->Size;
1363 Symbol->Flags = si->Flags;
1364 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1365 lstrcpynA(Symbol->Name, si->Name, len);
1369 /******************************************************************
1370 * SymGetSymFromAddr64 (DBGHELP.@)
1373 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1374 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1376 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1377 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1379 DWORD64 Displacement64;
1381 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1382 si->SizeOfStruct = sizeof(*si);
1383 si->MaxNameLen = MAX_SYM_NAME;
1384 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1388 *Displacement = Displacement64;
1389 Symbol->Address = si->Address;
1390 Symbol->Size = si->Size;
1391 Symbol->Flags = si->Flags;
1392 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1393 lstrcpynA(Symbol->Name, si->Name, len);
1397 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1398 SYMBOL_INFO* symbol)
1400 struct hash_table_iter hti;
1402 struct symt_ht* sym = NULL;
1403 struct module_pair pair;
1406 if (!(pair.requested = module)) return FALSE;
1407 if (!module_get_debug(&pair)) return FALSE;
1409 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1410 while ((ptr = hash_table_iter_up(&hti)))
1412 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1414 if (!strcmp(sym->hash_elt.name, name))
1416 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1423 /******************************************************************
1424 * SymFromName (DBGHELP.@)
1427 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1429 struct process* pcs = process_find_by_handle(hProcess);
1430 struct module* module;
1433 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1434 if (!pcs) return FALSE;
1435 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1436 name = strchr(Name, '!');
1440 assert(name - Name < sizeof(tmp));
1441 memcpy(tmp, Name, name - Name);
1442 tmp[name - Name] = '\0';
1443 module = module_find_by_nameA(pcs, tmp);
1444 return find_name(pcs, module, name + 1, Symbol);
1446 for (module = pcs->lmodules; module; module = module->next)
1448 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1451 /* not found in PE modules, retry on the ELF ones
1453 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1455 for (module = pcs->lmodules; module; module = module->next)
1457 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1458 !module_get_containee(pcs, module) &&
1459 find_name(pcs, module, Name, Symbol))
1466 /***********************************************************************
1467 * SymGetSymFromName64 (DBGHELP.@)
1469 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1471 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1472 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1475 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1476 si->SizeOfStruct = sizeof(*si);
1477 si->MaxNameLen = MAX_SYM_NAME;
1478 if (!SymFromName(hProcess, Name, si)) return FALSE;
1480 Symbol->Address = si->Address;
1481 Symbol->Size = si->Size;
1482 Symbol->Flags = si->Flags;
1483 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1484 lstrcpynA(Symbol->Name, si->Name, len);
1488 /***********************************************************************
1489 * SymGetSymFromName (DBGHELP.@)
1491 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1493 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1494 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1497 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1498 si->SizeOfStruct = sizeof(*si);
1499 si->MaxNameLen = MAX_SYM_NAME;
1500 if (!SymFromName(hProcess, Name, si)) return FALSE;
1502 Symbol->Address = si->Address;
1503 Symbol->Size = si->Size;
1504 Symbol->Flags = si->Flags;
1505 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1506 lstrcpynA(Symbol->Name, si->Name, len);
1510 /******************************************************************
1511 * sym_fill_func_line_info
1513 * fills information about a file
1515 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1516 DWORD64 addr, IMAGEHLP_LINE64* line)
1518 struct line_info* dli = NULL;
1522 assert(func->symt.tag == SymTagFunction);
1524 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1526 dli = vector_at(&func->vlines, i);
1527 if (!dli->is_source_file)
1529 if (found || dli->u.pc_offset > addr) continue;
1530 line->LineNumber = dli->line_number;
1531 line->Address = dli->u.pc_offset;
1538 line->FileName = (char*)source_get(module, dli->u.source_file);
1545 /***********************************************************************
1546 * SymGetSymNext64 (DBGHELP.@)
1548 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1551 * get module from Symbol.Address
1552 * get index in module.addr_sorttab of Symbol.Address
1554 * if out of module bounds, move to next module in process address space
1556 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1557 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1561 /***********************************************************************
1562 * SymGetSymNext (DBGHELP.@)
1564 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1566 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1567 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1571 /***********************************************************************
1572 * SymGetSymPrev64 (DBGHELP.@)
1574 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1576 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1577 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1581 /***********************************************************************
1582 * SymGetSymPrev (DBGHELP.@)
1584 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1586 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1587 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1591 /******************************************************************
1592 * copy_line_64_from_32 (internal)
1595 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1598 l64->Key = l32->Key;
1599 l64->LineNumber = l32->LineNumber;
1600 l64->FileName = l32->FileName;
1601 l64->Address = l32->Address;
1604 /******************************************************************
1605 * copy_line_W64_from_32 (internal)
1608 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1612 l64w->Key = l64->Key;
1613 l64w->LineNumber = l64->LineNumber;
1614 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1615 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1616 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1617 l64w->Address = l64->Address;
1620 /******************************************************************
1621 * copy_line_32_from_64 (internal)
1624 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1627 l32->Key = l64->Key;
1628 l32->LineNumber = l64->LineNumber;
1629 l32->FileName = l64->FileName;
1630 l32->Address = l64->Address;
1633 /******************************************************************
1634 * SymGetLineFromAddr (DBGHELP.@)
1637 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1638 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1640 IMAGEHLP_LINE64 il64;
1642 il64.SizeOfStruct = sizeof(il64);
1643 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1645 copy_line_32_from_64(Line, &il64);
1649 /******************************************************************
1650 * SymGetLineFromAddr64 (DBGHELP.@)
1653 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1654 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1656 struct module_pair pair;
1657 struct symt_ht* symt;
1659 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1661 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1663 pair.pcs = process_find_by_handle(hProcess);
1664 if (!pair.pcs) return FALSE;
1665 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1666 if (!module_get_debug(&pair)) return FALSE;
1667 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1669 if (symt->symt.tag != SymTagFunction) return FALSE;
1670 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1671 dwAddr, Line)) return FALSE;
1672 *pdwDisplacement = dwAddr - Line->Address;
1676 /******************************************************************
1677 * SymGetLineFromAddrW64 (DBGHELP.@)
1680 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1681 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1683 IMAGEHLP_LINE64 il64;
1685 il64.SizeOfStruct = sizeof(il64);
1686 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1688 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1692 /******************************************************************
1693 * SymGetLinePrev64 (DBGHELP.@)
1696 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1698 struct module_pair pair;
1699 struct line_info* li;
1700 BOOL in_search = FALSE;
1702 TRACE("(%p %p)\n", hProcess, Line);
1704 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1706 pair.pcs = process_find_by_handle(hProcess);
1707 if (!pair.pcs) return FALSE;
1708 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1709 if (!module_get_debug(&pair)) return FALSE;
1711 if (Line->Key == 0) return FALSE;
1713 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1714 * element we have to go back until we find the prev one to get the real
1715 * source file name for the DLIT_OFFSET element just before
1716 * the first DLIT_SOURCEFILE
1718 while (!li->is_first)
1721 if (!li->is_source_file)
1723 Line->LineNumber = li->line_number;
1724 Line->Address = li->u.pc_offset;
1726 if (!in_search) return TRUE;
1732 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1738 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1742 /******************************************************************
1743 * SymGetLinePrev (DBGHELP.@)
1746 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1748 IMAGEHLP_LINE64 line64;
1750 line64.SizeOfStruct = sizeof(line64);
1751 copy_line_64_from_32(&line64, Line);
1752 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1753 copy_line_32_from_64(Line, &line64);
1757 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1759 struct line_info* li;
1761 if (line->Key == 0) return FALSE;
1763 while (!li->is_last)
1766 if (!li->is_source_file)
1768 line->LineNumber = li->line_number;
1769 line->Address = li->u.pc_offset;
1773 line->FileName = (char*)source_get(module, li->u.source_file);
1778 /******************************************************************
1779 * SymGetLineNext64 (DBGHELP.@)
1782 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1784 struct module_pair pair;
1786 TRACE("(%p %p)\n", hProcess, Line);
1788 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1789 pair.pcs = process_find_by_handle(hProcess);
1790 if (!pair.pcs) return FALSE;
1791 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1792 if (!module_get_debug(&pair)) return FALSE;
1794 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1795 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1799 /******************************************************************
1800 * SymGetLineNext (DBGHELP.@)
1803 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1805 IMAGEHLP_LINE64 line64;
1807 line64.SizeOfStruct = sizeof(line64);
1808 copy_line_64_from_32(&line64, Line);
1809 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1810 copy_line_32_from_64(Line, &line64);
1814 /***********************************************************************
1815 * SymUnDName (DBGHELP.@)
1817 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1819 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1820 UNDNAME_COMPLETE) != 0;
1823 /***********************************************************************
1824 * SymUnDName64 (DBGHELP.@)
1826 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1828 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1829 UNDNAME_COMPLETE) != 0;
1832 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1833 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1835 /***********************************************************************
1836 * UnDecorateSymbolName (DBGHELP.@)
1838 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1839 DWORD UndecoratedLength, DWORD Flags)
1841 /* undocumented from msvcrt */
1842 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1843 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1845 TRACE("(%s, %p, %d, 0x%08x)\n",
1846 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1850 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1851 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1852 if (!p_undname) return 0;
1855 if (!UnDecoratedName) return 0;
1856 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1857 und_alloc, und_free, Flags))
1859 return strlen(UnDecoratedName);
1862 /******************************************************************
1863 * SymMatchString (DBGHELP.@)
1866 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1871 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1873 compile_regex(re, -1, &preg, _case);
1874 ret = match_regexp(&preg, string);
1879 /******************************************************************
1880 * SymSearch (DBGHELP.@)
1882 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1883 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1884 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1885 PVOID UserContext, DWORD Options)
1889 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1890 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1891 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1892 UserContext, Options);
1894 if (Options != SYMSEARCH_GLOBALSONLY)
1896 FIXME("Unsupported searching with options (%x)\n", Options);
1897 SetLastError(ERROR_INVALID_PARAMETER);
1901 se.cb = EnumSymbolsCallback;
1902 se.user = UserContext;
1906 se.sym_info = (PSYMBOL_INFO)se.buffer;
1908 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1911 /******************************************************************
1912 * SymSearchW (DBGHELP.@)
1914 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1915 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1916 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1917 PVOID UserContext, DWORD Options)
1919 struct sym_enumW sew;
1923 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1924 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1925 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1926 UserContext, Options);
1928 sew.ctx = UserContext;
1929 sew.cb = EnumSymbolsCallback;
1930 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1934 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1935 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1936 if (!maskA) return FALSE;
1937 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1939 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1940 sym_enumW, &sew, Options);
1941 HeapFree(GetProcessHeap(), 0, maskA);
1946 /******************************************************************
1947 * SymAddSymbol (DBGHELP.@)
1950 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1951 DWORD64 addr, DWORD size, DWORD flags)
1953 WCHAR nameW[MAX_SYM_NAME];
1955 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1956 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1959 /******************************************************************
1960 * SymAddSymbolW (DBGHELP.@)
1963 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1964 DWORD64 addr, DWORD size, DWORD flags)
1966 struct module_pair pair;
1968 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1970 pair.pcs = process_find_by_handle(hProcess);
1971 if (!pair.pcs) return FALSE;
1972 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1973 if (!module_get_debug(&pair)) return FALSE;
1975 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1979 /******************************************************************
1980 * SymSetScopeFromAddr (DBGHELP.@)
1982 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1984 struct process* pcs;
1986 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1988 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1992 /******************************************************************
1993 * SymEnumLines (DBGHELP.@)
1996 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1997 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1999 struct module_pair pair;
2000 struct hash_table_iter hti;
2001 struct symt_ht* sym;
2003 struct line_info* dli;
2008 if (!cb) return FALSE;
2009 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2011 pair.pcs = process_find_by_handle(hProcess);
2012 if (!pair.pcs) return FALSE;
2013 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2014 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2015 if (!module_get_debug(&pair)) return FALSE;
2016 if (!compile_file_regex(&re, srcfile)) return FALSE;
2018 sci.SizeOfStruct = sizeof(sci);
2021 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2022 while ((ptr = hash_table_iter_up(&hti)))
2026 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2027 if (sym->symt.tag != SymTagFunction) continue;
2029 sci.FileName[0] = '\0';
2030 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2032 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2033 if (dli->is_source_file)
2035 file = source_get(pair.effective, dli->u.source_file);
2036 if (!match_regexp(&re, file)) sci.FileName[0] = '\0';
2037 else strcpy(sci.FileName, file);
2039 else if (sci.FileName[0])
2042 sci.Obj[0] = '\0'; /* FIXME */
2043 sci.LineNumber = dli->line_number;
2044 sci.Address = dli->u.pc_offset;
2045 if (!cb(&sci, user)) break;
2053 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2054 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2056 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2057 dwLineNumber, plDisplacement, Line);
2061 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2062 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2064 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2065 dwLineNumber, lpDisplacement, Line);
2069 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2070 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2072 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2073 dwLineNumber, plDisplacement, Line);