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);
72 /* transforms a dbghelp's regular expression into a POSIX one
73 * Here are the valid dbghelp reg ex characters:
74 * * 0 or more characters
75 * ? a single character
77 * # 0 or more of preceding char
78 * + 1 or more of preceding char
79 * escapes \ on #, ?, [, ], *, +. don't work on -
81 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
84 BOOL in_escape = FALSE;
85 unsigned flags = REG_NOSUB;
87 if (numchar == -1) numchar = strlen( str );
89 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
92 while (*str && numchar--)
94 /* FIXME: this shouldn't be valid on '-' */
103 case '\\': in_escape = TRUE; break;
104 case '*': *p++ = '.'; *p++ = '*'; break;
105 case '?': *p++ = '.'; break;
106 case '#': *p++ = '*'; break;
107 /* escape some valid characters in dbghelp reg exp:s */
108 case '$': *p++ = '\\'; *p++ = '$'; break;
109 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
110 default: *p++ = *str; break;
121 if (_case) flags |= REG_ICASE;
122 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
123 HeapFree(GetProcessHeap(), 0, mask);
126 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
131 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
133 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
159 ret = !regcomp(re, mask, REG_NOSUB);
160 HeapFree(GetProcessHeap(), 0, mask);
163 FIXME("Couldn't compile %s\n", mask);
164 SetLastError(ERROR_INVALID_PARAMETER);
169 static int match_regexp( const regex_t *re, const char *str )
171 return !regexec( re, str, 0, NULL, 0 );
174 #else /* HAVE_REGEX_H */
176 /* if we don't have regexp support, fall back to a simple string comparison */
184 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
186 if (numchar == -1) numchar = strlen( str );
188 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
189 memcpy( re->str, str, numchar );
190 re->str[numchar] = 0;
194 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
196 if (!srcfile || !*srcfile) re->str = NULL;
197 else compile_regex( srcfile, -1, re, FALSE );
201 static int match_regexp( const regex_t *re, const char *str )
203 if (!re->str) return 1;
204 if (re->icase) return !lstrcmpiA( re->str, str );
205 return !strcmp( re->str, str );
208 static void regfree( regex_t *re )
210 HeapFree( GetProcessHeap(), 0, re->str );
213 #endif /* HAVE_REGEX_H */
215 struct symt_compiland* symt_new_compiland(struct module* module,
216 unsigned long address, unsigned src_idx)
218 struct symt_compiland* sym;
220 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
221 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
222 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
224 sym->symt.tag = SymTagCompiland;
225 sym->address = address;
226 sym->source = src_idx;
227 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
232 struct symt_public* symt_new_public(struct module* module,
233 struct symt_compiland* compiland,
235 unsigned long address, unsigned size,
236 BOOL in_code, BOOL is_func)
238 struct symt_public* sym;
241 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
242 debugstr_w(module->module.ModuleName), name, address);
243 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
244 symt_find_nearest(module, address) != NULL)
246 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
248 sym->symt.tag = SymTagPublicSymbol;
249 sym->hash_elt.name = pool_strdup(&module->pool, name);
250 hash_table_add(&module->ht_symbols, &sym->hash_elt);
251 module->sortlist_valid = FALSE;
252 sym->container = compiland ? &compiland->symt : NULL;
253 sym->address = address;
255 sym->in_code = in_code;
256 sym->is_function = is_func;
259 p = vector_add(&compiland->vchildren, &module->pool);
266 struct symt_data* symt_new_global_variable(struct module* module,
267 struct symt_compiland* compiland,
268 const char* name, unsigned is_static,
269 unsigned long addr, unsigned long size,
272 struct symt_data* sym;
276 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
277 debugstr_w(module->module.ModuleName), name, addr, type);
278 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
280 sym->symt.tag = SymTagData;
281 sym->hash_elt.name = pool_strdup(&module->pool, name);
282 hash_table_add(&module->ht_symbols, &sym->hash_elt);
283 module->sortlist_valid = FALSE;
284 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
285 sym->container = compiland ? &compiland->symt : NULL;
287 sym->u.var.offset = addr;
288 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
291 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
292 debugstr_w(module->module.ModuleName), name,
293 wine_dbgstr_longlong(tsz), size);
297 p = vector_add(&compiland->vchildren, &module->pool);
304 struct symt_function* symt_new_function(struct module* module,
305 struct symt_compiland* compiland,
307 unsigned long addr, unsigned long size,
308 struct symt* sig_type)
310 struct symt_function* sym;
313 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
314 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
316 assert(!sig_type || sig_type->tag == SymTagFunctionType);
317 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
319 sym->symt.tag = SymTagFunction;
320 sym->hash_elt.name = pool_strdup(&module->pool, name);
321 hash_table_add(&module->ht_symbols, &sym->hash_elt);
322 module->sortlist_valid = FALSE;
323 sym->container = &compiland->symt;
325 sym->type = sig_type;
327 vector_init(&sym->vlines, sizeof(struct line_info), 64);
328 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
331 p = vector_add(&compiland->vchildren, &module->pool);
338 void symt_add_func_line(struct module* module, struct symt_function* func,
339 unsigned source_idx, int line_num, unsigned long offset)
341 struct line_info* dli;
342 BOOL last_matches = FALSE;
345 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
347 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
348 func, func->hash_elt.name, offset,
349 source_get(module, source_idx), line_num);
351 assert(func->symt.tag == SymTagFunction);
353 for (i=vector_length(&func->vlines)-1; i>=0; i--)
355 dli = vector_at(&func->vlines, i);
356 if (dli->is_source_file)
358 last_matches = (source_idx == dli->u.source_file);
365 /* we shouldn't have line changes on first line of function */
366 dli = vector_add(&func->vlines, &module->pool);
367 dli->is_source_file = 1;
368 dli->is_first = dli->is_last = 0;
369 dli->line_number = 0;
370 dli->u.source_file = source_idx;
372 dli = vector_add(&func->vlines, &module->pool);
373 dli->is_source_file = 0;
374 dli->is_first = dli->is_last = 0;
375 dli->line_number = line_num;
376 dli->u.pc_offset = func->address + offset;
379 /******************************************************************
380 * symt_add_func_local
382 * Adds a new local/parameter to a given function:
383 * In any cases, dt tells whether it's a local variable or a parameter
384 * If regno it's not 0:
385 * - then variable is stored in a register
386 * - otherwise, value is referenced by register + offset
387 * Otherwise, the variable is stored on the stack:
388 * - offset is then the offset from the frame register
390 struct symt_data* symt_add_func_local(struct module* module,
391 struct symt_function* func,
393 const struct location* loc,
394 struct symt_block* block,
395 struct symt* type, const char* name)
397 struct symt_data* locsym;
400 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
401 debugstr_w(module->module.ModuleName), func->hash_elt.name,
405 assert(func->symt.tag == SymTagFunction);
406 assert(dt == DataIsParam || dt == DataIsLocal);
408 locsym = pool_alloc(&module->pool, sizeof(*locsym));
409 locsym->symt.tag = SymTagData;
410 locsym->hash_elt.name = pool_strdup(&module->pool, name);
411 locsym->hash_elt.next = NULL;
413 locsym->container = &block->symt;
415 locsym->u.var = *loc;
417 p = vector_add(&block->vchildren, &module->pool);
419 p = vector_add(&func->vchildren, &module->pool);
425 struct symt_block* symt_open_func_block(struct module* module,
426 struct symt_function* func,
427 struct symt_block* parent_block,
428 unsigned pc, unsigned len)
430 struct symt_block* block;
434 assert(func->symt.tag == SymTagFunction);
436 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
437 block = pool_alloc(&module->pool, sizeof(*block));
438 block->symt.tag = SymTagBlock;
439 block->address = func->address + pc;
441 block->container = parent_block ? &parent_block->symt : &func->symt;
442 vector_init(&block->vchildren, sizeof(struct symt*), 4);
444 p = vector_add(&parent_block->vchildren, &module->pool);
446 p = vector_add(&func->vchildren, &module->pool);
452 struct symt_block* symt_close_func_block(struct module* module,
453 struct symt_function* func,
454 struct symt_block* block, unsigned pc)
457 assert(func->symt.tag == SymTagFunction);
459 if (pc) block->size = func->address + pc - block->address;
460 return (block->container->tag == SymTagBlock) ?
461 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
464 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
465 struct symt_function* func,
466 enum SymTagEnum point,
467 const struct location* loc,
470 struct symt_hierarchy_point*sym;
473 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
475 sym->symt.tag = point;
476 sym->parent = &func->symt;
478 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
479 p = vector_add(&func->vchildren, &module->pool);
485 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
488 struct line_info* dli;
491 /* We aren't adding any more locals or line numbers to this function.
492 * Free any spare memory that we might have allocated.
494 assert(func->symt.tag == SymTagFunction);
496 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
497 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
499 len = vector_length(&func->vlines);
502 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
503 dli = vector_at(&func->vlines, len); dli->is_last = 1;
508 struct symt_thunk* symt_new_thunk(struct module* module,
509 struct symt_compiland* compiland,
510 const char* name, THUNK_ORDINAL ord,
511 unsigned long addr, unsigned long size)
513 struct symt_thunk* sym;
515 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
516 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
518 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
520 sym->symt.tag = SymTagThunk;
521 sym->hash_elt.name = pool_strdup(&module->pool, name);
522 hash_table_add(&module->ht_symbols, &sym->hash_elt);
523 module->sortlist_valid = FALSE;
524 sym->container = &compiland->symt;
531 p = vector_add(&compiland->vchildren, &module->pool);
538 struct symt_data* symt_new_constant(struct module* module,
539 struct symt_compiland* compiland,
540 const char* name, struct symt* type,
543 struct symt_data* sym;
545 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
546 debugstr_w(module->module.ModuleName), name);
548 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
550 sym->symt.tag = SymTagData;
551 sym->hash_elt.name = pool_strdup(&module->pool, name);
552 hash_table_add(&module->ht_symbols, &sym->hash_elt);
553 module->sortlist_valid = FALSE;
554 sym->kind = DataIsConstant;
555 sym->container = compiland ? &compiland->symt : NULL;
561 p = vector_add(&compiland->vchildren, &module->pool);
568 struct symt_hierarchy_point* symt_new_label(struct module* module,
569 struct symt_compiland* compiland,
570 const char* name, unsigned long address)
572 struct symt_hierarchy_point* sym;
574 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
575 debugstr_w(module->module.ModuleName), name);
577 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
579 sym->symt.tag = SymTagLabel;
580 sym->hash_elt.name = pool_strdup(&module->pool, name);
581 hash_table_add(&module->ht_symbols, &sym->hash_elt);
582 module->sortlist_valid = FALSE;
583 sym->loc.kind = loc_absolute;
584 sym->loc.offset = address;
585 sym->parent = compiland ? &compiland->symt : NULL;
589 p = vector_add(&compiland->vchildren, &module->pool);
596 /* expect sym_info->MaxNameLen to be set before being called */
597 static void symt_fill_sym_info(const struct module_pair* pair,
598 const struct symt_function* func,
599 const struct symt* sym, SYMBOL_INFO* sym_info)
604 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
605 sym_info->TypeIndex = 0;
606 sym_info->info = (DWORD)sym;
607 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
608 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
609 (!sym_info->TypeIndex ||
610 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
612 sym_info->Size = (DWORD)size;
613 sym_info->ModBase = pair->requested->module.BaseOfImage;
621 const struct symt_data* data = (const struct symt_data*)sym;
625 sym_info->Flags |= SYMFLAG_PARAMETER;
629 struct location loc = data->u.var;
631 if (loc.kind >= loc_user)
632 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
637 /* for now we report error cases as a negative register number */
638 sym_info->Flags |= SYMFLAG_LOCAL;
641 sym_info->Flags |= SYMFLAG_REGISTER;
642 sym_info->Register = loc.reg;
643 sym_info->Address = 0;
646 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
647 /* FIXME: it's i386 dependent !!! */
648 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
649 sym_info->Address = loc.offset;
652 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
658 case DataIsFileStatic:
659 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
660 sym_info->Register = 0;
663 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
664 switch (data->u.value.n1.n2.vt)
666 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
667 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
668 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
669 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
670 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
671 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
672 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
674 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
680 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
684 case SymTagPublicSymbol:
685 sym_info->Flags |= SYMFLAG_EXPORT;
686 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
689 sym_info->Flags |= SYMFLAG_FUNCTION;
690 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
693 sym_info->Flags |= SYMFLAG_THUNK;
694 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
697 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
698 sym_info->Register = 0;
701 sym_info->Scope = 0; /* FIXME */
702 sym_info->Tag = sym->tag;
703 name = symt_get_name(sym);
704 if (sym_info->MaxNameLen)
706 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
707 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
708 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
710 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
711 memcpy(sym_info->Name, name, sym_info->NameLen);
712 sym_info->Name[sym_info->NameLen] = '\0';
715 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
716 sym, sym_info->Name, sym_info->Size,
717 wine_dbgstr_longlong(sym_info->Address));
722 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
724 SYMBOL_INFO* sym_info;
728 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
731 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
732 const struct symt_function* func, const struct symt* sym)
734 symt_fill_sym_info(pair, func, sym, se->sym_info);
735 if (se->index && se->sym_info->info != se->index) return FALSE;
736 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
737 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
738 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
741 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
742 const struct sym_enum* se)
745 struct symt_ht* sym = NULL;
746 struct hash_table_iter hti;
748 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
749 while ((ptr = hash_table_iter_up(&hti)))
751 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
752 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
754 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
755 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
756 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
762 /***********************************************************************
765 * Rebuild sorted list of symbols for a module.
767 static BOOL resort_symbols(struct module* module)
771 struct hash_table_iter hti;
774 if (!(module->module.NumSyms = module->ht_symbols.num_elts))
777 if (module->addr_sorttab)
778 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
779 module->addr_sorttab,
780 module->module.NumSyms * sizeof(struct symt_ht*));
782 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
783 module->module.NumSyms * sizeof(struct symt_ht*));
784 if (!module->addr_sorttab) return FALSE;
786 module->num_sorttab = 0;
787 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
788 while ((ptr = hash_table_iter_up(&hti)))
790 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
792 /* Don't store in sorttab symbol without address, they are of
793 * no use here (e.g. constant values)
794 * As the number of those symbols is very couple (a couple per module)
795 * we don't bother for the unused spots at the end of addr_sorttab
797 if (symt_get_info(&sym->symt, TI_GET_ADDRESS, &addr))
798 module->addr_sorttab[module->num_sorttab++] = sym;
800 qsort(module->addr_sorttab, module->num_sorttab, sizeof(struct symt_ht*), symt_cmp_addr);
801 return module->sortlist_valid = TRUE;
804 static void symt_get_length(struct symt* symt, ULONG64* size)
808 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
811 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
812 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
813 *size = 0x1000; /* arbitrary value */
816 /* assume addr is in module */
817 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
820 ULONG64 ref_addr, ref_size;
822 if (!module->sortlist_valid || !module->addr_sorttab)
824 if (!resort_symbols(module)) return NULL;
828 * Binary search to find closest symbol.
831 high = module->num_sorttab;
833 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
834 if (addr < ref_addr) return NULL;
837 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
838 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
839 if (addr >= ref_addr + ref_size) return NULL;
842 while (high > low + 1)
844 mid = (high + low) / 2;
845 if (cmp_sorttab_addr(module, mid, addr) < 0)
850 if (low != high && high != module->num_sorttab &&
851 cmp_sorttab_addr(module, high, addr) <= 0)
854 /* If found symbol is a public symbol, check if there are any other entries that
855 * might also have the same address, but would get better information
857 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
859 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
861 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
862 !cmp_sorttab_addr(module, low - 1, ref_addr))
864 else if (low < module->num_sorttab - 1 &&
865 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
866 !cmp_sorttab_addr(module, low + 1, ref_addr))
869 /* finally check that we fit into the found symbol */
870 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
871 if (addr < ref_addr) return NULL;
872 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
873 if (addr >= ref_addr + ref_size) return NULL;
875 return module->addr_sorttab[low];
878 static BOOL symt_enum_locals_helper(struct module_pair* pair,
879 regex_t* preg, const struct sym_enum* se,
880 struct symt_function* func, const struct vector* v)
882 struct symt* lsym = NULL;
883 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
886 for (i=0; i<vector_length(v); i++)
888 lsym = *(struct symt**)vector_at(v, i);
893 struct symt_block* block = (struct symt_block*)lsym;
894 if (pc < block->address || block->address + block->size <= pc)
896 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
901 if (match_regexp(preg, symt_get_name(lsym)))
903 if (send_symbol(se, pair, func, lsym)) return FALSE;
907 case SymTagFuncDebugStart:
908 case SymTagFuncDebugEnd:
912 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
919 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
920 const struct sym_enum* se)
922 struct module_pair pair;
924 DWORD pc = pcs->ctx_frame.InstructionOffset;
926 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
927 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
930 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
931 if (!module_get_debug(&pair)) return FALSE;
932 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
934 if (sym->symt.tag == SymTagFunction)
939 compile_regex(mask ? mask : "*", -1, &preg,
940 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
941 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
942 &((struct symt_function*)sym)->vchildren);
947 return send_symbol(se, &pair, NULL, &sym->symt);
950 /******************************************************************
953 * Helper for transforming an ANSI symbol info into a UNICODE one.
954 * Assume that MaxNameLen is the same for both version (A & W).
956 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
958 siw->SizeOfStruct = si->SizeOfStruct;
959 siw->TypeIndex = si->TypeIndex;
960 siw->Reserved[0] = si->Reserved[0];
961 siw->Reserved[1] = si->Reserved[1];
962 siw->Index = si->info; /* FIXME: see dbghelp.h */
963 siw->Size = si->Size;
964 siw->ModBase = si->ModBase;
965 siw->Flags = si->Flags;
966 siw->Value = si->Value;
967 siw->Address = si->Address;
968 siw->Register = si->Register;
969 siw->Scope = si->Scope;
971 siw->NameLen = si->NameLen;
972 siw->MaxNameLen = si->MaxNameLen;
973 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
976 /******************************************************************
979 * Core routine for most of the enumeration of symbols
981 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
982 const struct sym_enum* se)
984 struct module_pair pair;
986 regex_t mod_regex, sym_regex;
988 pair.pcs = process_find_by_handle(hProcess);
991 /* do local variables ? */
992 if (!Mask || !(bang = strchr(Mask, '!')))
993 return symt_enum_locals(pair.pcs, Mask, se);
995 if (bang == Mask) return FALSE;
997 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
998 compile_regex(bang + 1, -1, &sym_regex,
999 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1001 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1003 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1005 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1006 symt_enum_module(&pair, &sym_regex, se))
1010 /* not found in PE modules, retry on the ELF ones
1012 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1014 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1016 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1017 !module_get_containee(pair.pcs, pair.requested) &&
1018 module_get_debug(&pair))
1020 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1021 symt_enum_module(&pair, &sym_regex, se))
1026 regfree(&mod_regex);
1027 regfree(&sym_regex);
1030 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1031 if (!module_get_debug(&pair))
1034 /* we always ignore module name from Mask when BaseOfDll is defined */
1035 if (Mask && (bang = strchr(Mask, '!')))
1037 if (bang == Mask) return FALSE;
1041 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1042 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1043 symt_enum_module(&pair, &sym_regex, se);
1044 regfree(&sym_regex);
1049 /******************************************************************
1050 * SymEnumSymbols (DBGHELP.@)
1052 * cases BaseOfDll = 0
1053 * !foo fails always (despite what MSDN states)
1054 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1055 * no ! in Mask, lookup in local Context
1056 * cases BaseOfDll != 0
1057 * !foo fails always (despite what MSDN states)
1058 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1060 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1061 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1066 TRACE("(%p %s %s %p %p)\n",
1067 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1068 EnumSymbolsCallback, UserContext);
1070 se.cb = EnumSymbolsCallback;
1071 se.user = UserContext;
1075 se.sym_info = (PSYMBOL_INFO)se.buffer;
1077 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1082 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1084 PSYMBOL_INFOW sym_info;
1085 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1089 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1091 struct sym_enumW* sew = ctx;
1093 copy_symbolW(sew->sym_info, si);
1095 return (sew->cb)(sew->sym_info, size, sew->ctx);
1098 /******************************************************************
1099 * SymEnumSymbolsW (DBGHELP.@)
1102 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1103 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1106 struct sym_enumW sew;
1110 sew.ctx = UserContext;
1111 sew.cb = EnumSymbolsCallback;
1112 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1116 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1117 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1118 if (!maskA) return FALSE;
1119 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1121 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1122 HeapFree(GetProcessHeap(), 0, maskA);
1127 struct sym_enumerate
1130 PSYM_ENUMSYMBOLS_CALLBACK cb;
1133 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1135 struct sym_enumerate* se = ctx;
1136 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1139 /***********************************************************************
1140 * SymEnumerateSymbols (DBGHELP.@)
1142 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1143 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1146 struct sym_enumerate se;
1148 se.ctx = UserContext;
1149 se.cb = EnumSymbolsCallback;
1151 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1154 struct sym_enumerate64
1157 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1160 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1162 struct sym_enumerate64* se = ctx;
1163 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1166 /***********************************************************************
1167 * SymEnumerateSymbols64 (DBGHELP.@)
1169 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1170 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1173 struct sym_enumerate64 se;
1175 se.ctx = UserContext;
1176 se.cb = EnumSymbolsCallback;
1178 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1181 /******************************************************************
1182 * SymFromAddr (DBGHELP.@)
1185 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1186 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1188 struct module_pair pair;
1189 struct symt_ht* sym;
1191 pair.pcs = process_find_by_handle(hProcess);
1192 if (!pair.pcs) return FALSE;
1193 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1194 if (!module_get_debug(&pair)) return FALSE;
1195 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1197 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1198 *Displacement = Address - Symbol->Address;
1202 /******************************************************************
1203 * SymFromAddrW (DBGHELP.@)
1206 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1207 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1213 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1214 si = HeapAlloc(GetProcessHeap(), 0, len);
1215 if (!si) return FALSE;
1217 si->SizeOfStruct = sizeof(*si);
1218 si->MaxNameLen = Symbol->MaxNameLen;
1219 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1221 copy_symbolW(Symbol, si);
1223 HeapFree(GetProcessHeap(), 0, si);
1227 /******************************************************************
1228 * SymGetSymFromAddr (DBGHELP.@)
1231 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1232 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1234 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1235 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1237 DWORD64 Displacement64;
1239 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1240 si->SizeOfStruct = sizeof(*si);
1241 si->MaxNameLen = MAX_SYM_NAME;
1242 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1246 *Displacement = Displacement64;
1247 Symbol->Address = si->Address;
1248 Symbol->Size = si->Size;
1249 Symbol->Flags = si->Flags;
1250 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1251 lstrcpynA(Symbol->Name, si->Name, len);
1255 /******************************************************************
1256 * SymGetSymFromAddr64 (DBGHELP.@)
1259 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1260 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1262 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1263 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1265 DWORD64 Displacement64;
1267 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1268 si->SizeOfStruct = sizeof(*si);
1269 si->MaxNameLen = MAX_SYM_NAME;
1270 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1274 *Displacement = Displacement64;
1275 Symbol->Address = si->Address;
1276 Symbol->Size = si->Size;
1277 Symbol->Flags = si->Flags;
1278 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1279 lstrcpynA(Symbol->Name, si->Name, len);
1283 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1284 SYMBOL_INFO* symbol)
1286 struct hash_table_iter hti;
1288 struct symt_ht* sym = NULL;
1289 struct module_pair pair;
1292 if (!(pair.requested = module)) return FALSE;
1293 if (!module_get_debug(&pair)) return FALSE;
1295 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1296 while ((ptr = hash_table_iter_up(&hti)))
1298 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1300 if (!strcmp(sym->hash_elt.name, name))
1302 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1309 /******************************************************************
1310 * SymFromName (DBGHELP.@)
1313 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1315 struct process* pcs = process_find_by_handle(hProcess);
1316 struct module* module;
1319 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1320 if (!pcs) return FALSE;
1321 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1322 name = strchr(Name, '!');
1326 assert(name - Name < sizeof(tmp));
1327 memcpy(tmp, Name, name - Name);
1328 tmp[name - Name] = '\0';
1329 module = module_find_by_nameA(pcs, tmp);
1330 return find_name(pcs, module, name + 1, Symbol);
1332 for (module = pcs->lmodules; module; module = module->next)
1334 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1337 /* not found in PE modules, retry on the ELF ones
1339 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1341 for (module = pcs->lmodules; module; module = module->next)
1343 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1344 !module_get_containee(pcs, module) &&
1345 find_name(pcs, module, Name, Symbol))
1352 /***********************************************************************
1353 * SymGetSymFromName (DBGHELP.@)
1355 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1357 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1358 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1361 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1362 si->SizeOfStruct = sizeof(*si);
1363 si->MaxNameLen = MAX_SYM_NAME;
1364 if (!SymFromName(hProcess, Name, si)) return FALSE;
1366 Symbol->Address = si->Address;
1367 Symbol->Size = si->Size;
1368 Symbol->Flags = si->Flags;
1369 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1370 lstrcpynA(Symbol->Name, si->Name, len);
1374 /******************************************************************
1375 * sym_fill_func_line_info
1377 * fills information about a file
1379 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1380 DWORD addr, IMAGEHLP_LINE* line)
1382 struct line_info* dli = NULL;
1386 assert(func->symt.tag == SymTagFunction);
1388 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1390 dli = vector_at(&func->vlines, i);
1391 if (!dli->is_source_file)
1393 if (found || dli->u.pc_offset > addr) continue;
1394 line->LineNumber = dli->line_number;
1395 line->Address = dli->u.pc_offset;
1402 line->FileName = (char*)source_get(module, dli->u.source_file);
1409 /***********************************************************************
1410 * SymGetSymNext (DBGHELP.@)
1412 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1415 * get module from Symbol.Address
1416 * get index in module.addr_sorttab of Symbol.Address
1418 * if out of module bounds, move to next module in process address space
1420 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1421 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1425 /***********************************************************************
1426 * SymGetSymPrev (DBGHELP.@)
1429 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1431 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1432 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1436 /******************************************************************
1437 * SymGetLineFromAddr (DBGHELP.@)
1440 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1441 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1443 struct module_pair pair;
1444 struct symt_ht* symt;
1446 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1448 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1450 pair.pcs = process_find_by_handle(hProcess);
1451 if (!pair.pcs) return FALSE;
1452 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1453 if (!module_get_debug(&pair)) return FALSE;
1454 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1456 if (symt->symt.tag != SymTagFunction) return FALSE;
1457 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1458 dwAddr, Line)) return FALSE;
1459 *pdwDisplacement = dwAddr - Line->Address;
1463 /******************************************************************
1464 * copy_line_64_from_32 (internal)
1467 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1470 l64->Key = l32->Key;
1471 l64->LineNumber = l32->LineNumber;
1472 l64->FileName = l32->FileName;
1473 l64->Address = l32->Address;
1476 /******************************************************************
1477 * copy_line_W64_from_32 (internal)
1480 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1484 l64->Key = l32->Key;
1485 l64->LineNumber = l32->LineNumber;
1486 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1487 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1488 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1489 l64->Address = l32->Address;
1492 /******************************************************************
1493 * copy_line_32_from_64 (internal)
1496 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1499 l32->Key = l64->Key;
1500 l32->LineNumber = l64->LineNumber;
1501 l32->FileName = l64->FileName;
1502 l32->Address = l64->Address;
1505 /******************************************************************
1506 * SymGetLineFromAddr64 (DBGHELP.@)
1509 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1510 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1512 IMAGEHLP_LINE line32;
1514 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1515 if (!validate_addr64(dwAddr)) return FALSE;
1516 line32.SizeOfStruct = sizeof(line32);
1517 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1519 copy_line_64_from_32(Line, &line32);
1523 /******************************************************************
1524 * SymGetLineFromAddrW64 (DBGHELP.@)
1527 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1528 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1530 struct process* pcs = process_find_by_handle(hProcess);
1531 IMAGEHLP_LINE line32;
1533 if (!pcs) return FALSE;
1534 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1535 if (!validate_addr64(dwAddr)) return FALSE;
1536 line32.SizeOfStruct = sizeof(line32);
1537 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1539 copy_line_W64_from_32(pcs, Line, &line32);
1543 /******************************************************************
1544 * SymGetLinePrev (DBGHELP.@)
1547 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1549 struct module_pair pair;
1550 struct line_info* li;
1551 BOOL in_search = FALSE;
1553 TRACE("(%p %p)\n", hProcess, Line);
1555 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1557 pair.pcs = process_find_by_handle(hProcess);
1558 if (!pair.pcs) return FALSE;
1559 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1560 if (!module_get_debug(&pair)) return FALSE;
1562 if (Line->Key == 0) return FALSE;
1564 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1565 * element we have to go back until we find the prev one to get the real
1566 * source file name for the DLIT_OFFSET element just before
1567 * the first DLIT_SOURCEFILE
1569 while (!li->is_first)
1572 if (!li->is_source_file)
1574 Line->LineNumber = li->line_number;
1575 Line->Address = li->u.pc_offset;
1577 if (!in_search) return TRUE;
1583 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1589 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1593 /******************************************************************
1594 * SymGetLinePrev64 (DBGHELP.@)
1597 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1599 IMAGEHLP_LINE line32;
1601 line32.SizeOfStruct = sizeof(line32);
1602 copy_line_32_from_64(&line32, Line);
1603 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1604 copy_line_64_from_32(Line, &line32);
1608 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1610 struct line_info* li;
1612 if (line->Key == 0) return FALSE;
1614 while (!li->is_last)
1617 if (!li->is_source_file)
1619 line->LineNumber = li->line_number;
1620 line->Address = li->u.pc_offset;
1624 line->FileName = (char*)source_get(module, li->u.source_file);
1629 /******************************************************************
1630 * SymGetLineNext (DBGHELP.@)
1633 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1635 struct module_pair pair;
1637 TRACE("(%p %p)\n", hProcess, Line);
1639 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1640 pair.pcs = process_find_by_handle(hProcess);
1641 if (!pair.pcs) return FALSE;
1642 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1643 if (!module_get_debug(&pair)) return FALSE;
1645 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1646 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1650 /******************************************************************
1651 * SymGetLineNext64 (DBGHELP.@)
1654 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1656 IMAGEHLP_LINE line32;
1658 line32.SizeOfStruct = sizeof(line32);
1659 copy_line_32_from_64(&line32, Line);
1660 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1661 copy_line_64_from_32(Line, &line32);
1665 /***********************************************************************
1666 * SymFunctionTableAccess (DBGHELP.@)
1668 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1670 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1674 /***********************************************************************
1675 * SymFunctionTableAccess64 (DBGHELP.@)
1677 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1679 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1683 /***********************************************************************
1684 * SymUnDName (DBGHELP.@)
1686 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1688 TRACE("(%p %s %u)\n", sym, UnDecName, UnDecNameLength);
1689 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1690 UNDNAME_COMPLETE) != 0;
1693 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1694 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1696 /***********************************************************************
1697 * UnDecorateSymbolName (DBGHELP.@)
1699 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1700 DWORD UndecoratedLength, DWORD Flags)
1702 /* undocumented from msvcrt */
1703 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1704 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1706 TRACE("(%s, %p, %d, 0x%08x)\n",
1707 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1711 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1712 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1713 if (!p_undname) return 0;
1716 if (!UnDecoratedName) return 0;
1717 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1718 und_alloc, und_free, Flags))
1720 return strlen(UnDecoratedName);
1723 /******************************************************************
1724 * SymMatchString (DBGHELP.@)
1727 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1732 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1734 compile_regex(re, -1, &preg, _case);
1735 ret = match_regexp(&preg, string);
1740 /******************************************************************
1741 * SymSearch (DBGHELP.@)
1743 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1744 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1745 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1746 PVOID UserContext, DWORD Options)
1750 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1751 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1752 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1753 UserContext, Options);
1755 if (Options != SYMSEARCH_GLOBALSONLY)
1757 FIXME("Unsupported searching with options (%x)\n", Options);
1758 SetLastError(ERROR_INVALID_PARAMETER);
1762 se.cb = EnumSymbolsCallback;
1763 se.user = UserContext;
1767 se.sym_info = (PSYMBOL_INFO)se.buffer;
1769 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1772 /******************************************************************
1773 * SymSearchW (DBGHELP.@)
1775 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1776 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1777 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1778 PVOID UserContext, DWORD Options)
1780 struct sym_enumW sew;
1784 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1785 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1786 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1787 UserContext, Options);
1789 sew.ctx = UserContext;
1790 sew.cb = EnumSymbolsCallback;
1791 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1795 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1796 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1797 if (!maskA) return FALSE;
1798 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1800 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1801 sym_enumW, &sew, Options);
1802 HeapFree(GetProcessHeap(), 0, maskA);
1807 /******************************************************************
1808 * SymAddSymbol (DBGHELP.@)
1811 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1812 DWORD64 addr, DWORD size, DWORD flags)
1814 WCHAR nameW[MAX_SYM_NAME];
1816 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1817 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1820 /******************************************************************
1821 * SymAddSymbolW (DBGHELP.@)
1824 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1825 DWORD64 addr, DWORD size, DWORD flags)
1827 struct module_pair pair;
1829 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1831 pair.pcs = process_find_by_handle(hProcess);
1832 if (!pair.pcs) return FALSE;
1833 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1834 if (!module_get_debug(&pair)) return FALSE;
1836 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1840 /******************************************************************
1841 * SymSetScopeFromAddr (DBGHELP.@)
1843 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1845 struct process* pcs;
1847 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1849 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1853 /******************************************************************
1854 * SymEnumLines (DBGHELP.@)
1857 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1858 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1860 struct module_pair pair;
1861 struct hash_table_iter hti;
1862 struct symt_ht* sym;
1864 struct line_info* dli;
1869 if (!cb) return FALSE;
1870 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1872 pair.pcs = process_find_by_handle(hProcess);
1873 if (!pair.pcs) return FALSE;
1874 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1875 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1876 if (!module_get_debug(&pair)) return FALSE;
1877 if (!compile_file_regex(&re, srcfile)) return FALSE;
1879 sci.SizeOfStruct = sizeof(sci);
1882 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1883 while ((ptr = hash_table_iter_up(&hti)))
1887 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1888 if (sym->symt.tag != SymTagFunction) continue;
1890 sci.FileName[0] = '\0';
1891 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1893 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1894 if (dli->is_source_file)
1896 file = source_get(pair.effective, dli->u.source_file);
1897 if (!match_regexp(&re, file)) file = "";
1898 strcpy(sci.FileName, file);
1900 else if (sci.FileName[0])
1903 sci.Obj[0] = '\0'; /* FIXME */
1904 sci.LineNumber = dli->line_number;
1905 sci.Address = dli->u.pc_offset;
1906 if (!cb(&sci, user)) break;