makefiles: Generate the config.status dependency as part of the makefile dependencies.
[wine/multimedia.git] / dlls / dbghelp / symbol.c
blob0b66cdb71e3168bb0ebe47f43d2e44344408307e
1 /*
2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004, Eric Pouech
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
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <assert.h>
33 #ifdef HAVE_REGEX_H
34 # include <regex.h>
35 #endif
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
39 #include "winnls.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;
48 return 0;
51 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
53 ULONG64 ref;
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;
65 ULONG64 a1, a2;
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)
74 #ifdef _WIN64
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)
82 return i + 1;
84 /* not found */
85 c = vector_add(&module->vsymt, &module->pool);
86 if (c) *c = sym;
87 return len + 1;
88 #else
89 return (DWORD)sym;
90 #endif
93 struct symt* symt_index2ptr(struct module* module, DWORD id)
95 #ifdef _WIN64
96 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
97 return *(struct symt**)vector_at(&module->vsymt, id);
98 #else
99 return (struct symt*)id;
100 #endif
103 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
105 struct symt_ht** new;
106 unsigned int size;
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*));
115 else
117 size = 64;
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;
123 return TRUE;
126 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
128 ULONG64 addr;
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;
142 #ifdef HAVE_REGEX_H
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
148 * [] list
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)
155 char *mask, *p;
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 );
162 *p++ = '^';
164 while (*str && numchar--)
166 /* FIXME: this shouldn't be valid on '-' */
167 if (in_escape)
169 *p++ = '\\';
170 *p++ = *str;
171 in_escape = FALSE;
173 else switch (*str)
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;
184 str++;
186 if (in_escape)
188 *p++ = '\\';
189 *p++ = '\\';
191 *p++ = '$';
192 *p = 0;
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)
200 char *mask, *p;
201 BOOL ret;
203 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
205 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
206 *p++ = '^';
207 while (*srcfile)
209 switch (*srcfile)
211 case '\\':
212 case '/':
213 *p++ = '[';
214 *p++ = '\\';
215 *p++ = '\\';
216 *p++ = '/';
217 *p++ = ']';
218 break;
219 case '.':
220 *p++ = '\\';
221 *p++ = '.';
222 break;
223 default:
224 *p++ = *srcfile;
225 break;
227 srcfile++;
229 *p++ = '$';
230 *p = 0;
231 ret = !regcomp(re, mask, REG_NOSUB);
232 HeapFree(GetProcessHeap(), 0, mask);
233 if (!ret)
235 FIXME("Couldn't compile %s\n", mask);
236 SetLastError(ERROR_INVALID_PARAMETER);
238 return ret;
241 static int match_regexp( const regex_t *re, const char *str )
243 return !regexec( re, str, 0, NULL, 0 );
246 #else /* HAVE_REGEX_H */
248 /* if we don't have regexp support, fall back to a simple string comparison */
250 typedef struct
252 char *str;
253 BOOL icase;
254 } regex_t;
256 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
258 if (numchar == -1) numchar = strlen( str );
260 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
261 memcpy( re->str, str, numchar );
262 re->str[numchar] = 0;
263 re->icase = _case;
266 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
268 if (!srcfile || !*srcfile) re->str = NULL;
269 else compile_regex( srcfile, -1, re, FALSE );
270 return TRUE;
273 static int match_regexp( const regex_t *re, const char *str )
275 if (!re->str) return 1;
276 if (re->icase) return !lstrcmpiA( re->str, str );
277 return !strcmp( re->str, str );
280 static void regfree( regex_t *re )
282 HeapFree( GetProcessHeap(), 0, re->str );
285 #endif /* HAVE_REGEX_H */
287 struct symt_compiland* symt_new_compiland(struct module* module,
288 unsigned long address, unsigned src_idx)
290 struct symt_compiland* sym;
292 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
293 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
294 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
296 sym->symt.tag = SymTagCompiland;
297 sym->address = address;
298 sym->source = src_idx;
299 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
301 return sym;
304 struct symt_public* symt_new_public(struct module* module,
305 struct symt_compiland* compiland,
306 const char* name,
307 unsigned long address, unsigned size)
309 struct symt_public* sym;
310 struct symt** p;
312 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
313 debugstr_w(module->module.ModuleName), name, address);
314 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
315 symt_find_nearest(module, address) != NULL)
316 return NULL;
317 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
319 sym->symt.tag = SymTagPublicSymbol;
320 sym->hash_elt.name = pool_strdup(&module->pool, name);
321 sym->container = compiland ? &compiland->symt : NULL;
322 sym->address = address;
323 sym->size = size;
324 symt_add_module_ht(module, (struct symt_ht*)sym);
325 if (compiland)
327 p = vector_add(&compiland->vchildren, &module->pool);
328 *p = &sym->symt;
331 return sym;
334 struct symt_data* symt_new_global_variable(struct module* module,
335 struct symt_compiland* compiland,
336 const char* name, unsigned is_static,
337 unsigned long addr, unsigned long size,
338 struct symt* type)
340 struct symt_data* sym;
341 struct symt** p;
342 DWORD64 tsz;
344 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
345 debugstr_w(module->module.ModuleName), name, addr, type);
346 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
348 sym->symt.tag = SymTagData;
349 sym->hash_elt.name = pool_strdup(&module->pool, name);
350 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
351 sym->container = compiland ? &compiland->symt : NULL;
352 sym->type = type;
353 sym->u.var.offset = addr;
354 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
356 if (tsz != size)
357 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
358 debugstr_w(module->module.ModuleName), name,
359 wine_dbgstr_longlong(tsz), size);
361 symt_add_module_ht(module, (struct symt_ht*)sym);
362 if (compiland)
364 p = vector_add(&compiland->vchildren, &module->pool);
365 *p = &sym->symt;
368 return sym;
371 struct symt_function* symt_new_function(struct module* module,
372 struct symt_compiland* compiland,
373 const char* name,
374 unsigned long addr, unsigned long size,
375 struct symt* sig_type)
377 struct symt_function* sym;
378 struct symt** p;
380 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
381 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
383 assert(!sig_type || sig_type->tag == SymTagFunctionType);
384 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
386 sym->symt.tag = SymTagFunction;
387 sym->hash_elt.name = pool_strdup(&module->pool, name);
388 sym->container = &compiland->symt;
389 sym->address = addr;
390 sym->type = sig_type;
391 sym->size = size;
392 vector_init(&sym->vlines, sizeof(struct line_info), 64);
393 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
394 symt_add_module_ht(module, (struct symt_ht*)sym);
395 if (compiland)
397 p = vector_add(&compiland->vchildren, &module->pool);
398 *p = &sym->symt;
401 return sym;
404 void symt_add_func_line(struct module* module, struct symt_function* func,
405 unsigned source_idx, int line_num, unsigned long offset)
407 struct line_info* dli;
408 BOOL last_matches = FALSE;
409 int i;
411 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
413 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
414 func, func->hash_elt.name, offset,
415 source_get(module, source_idx), line_num);
417 assert(func->symt.tag == SymTagFunction);
419 for (i=vector_length(&func->vlines)-1; i>=0; i--)
421 dli = vector_at(&func->vlines, i);
422 if (dli->is_source_file)
424 last_matches = (source_idx == dli->u.source_file);
425 break;
429 if (!last_matches)
431 /* we shouldn't have line changes on first line of function */
432 dli = vector_add(&func->vlines, &module->pool);
433 dli->is_source_file = 1;
434 dli->is_first = dli->is_last = 0;
435 dli->line_number = 0;
436 dli->u.source_file = source_idx;
438 dli = vector_add(&func->vlines, &module->pool);
439 dli->is_source_file = 0;
440 dli->is_first = dli->is_last = 0;
441 dli->line_number = line_num;
442 dli->u.pc_offset = func->address + offset;
445 /******************************************************************
446 * symt_add_func_local
448 * Adds a new local/parameter to a given function:
449 * In any cases, dt tells whether it's a local variable or a parameter
450 * If regno it's not 0:
451 * - then variable is stored in a register
452 * - otherwise, value is referenced by register + offset
453 * Otherwise, the variable is stored on the stack:
454 * - offset is then the offset from the frame register
456 struct symt_data* symt_add_func_local(struct module* module,
457 struct symt_function* func,
458 enum DataKind dt,
459 const struct location* loc,
460 struct symt_block* block,
461 struct symt* type, const char* name)
463 struct symt_data* locsym;
464 struct symt** p;
466 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
467 debugstr_w(module->module.ModuleName), func->hash_elt.name,
468 name, type);
470 assert(func);
471 assert(func->symt.tag == SymTagFunction);
472 assert(dt == DataIsParam || dt == DataIsLocal);
474 locsym = pool_alloc(&module->pool, sizeof(*locsym));
475 locsym->symt.tag = SymTagData;
476 locsym->hash_elt.name = pool_strdup(&module->pool, name);
477 locsym->hash_elt.next = NULL;
478 locsym->kind = dt;
479 locsym->container = &block->symt;
480 locsym->type = type;
481 locsym->u.var = *loc;
482 if (block)
483 p = vector_add(&block->vchildren, &module->pool);
484 else
485 p = vector_add(&func->vchildren, &module->pool);
486 *p = &locsym->symt;
487 return locsym;
491 struct symt_block* symt_open_func_block(struct module* module,
492 struct symt_function* func,
493 struct symt_block* parent_block,
494 unsigned pc, unsigned len)
496 struct symt_block* block;
497 struct symt** p;
499 assert(func);
500 assert(func->symt.tag == SymTagFunction);
502 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
503 block = pool_alloc(&module->pool, sizeof(*block));
504 block->symt.tag = SymTagBlock;
505 block->address = func->address + pc;
506 block->size = len;
507 block->container = parent_block ? &parent_block->symt : &func->symt;
508 vector_init(&block->vchildren, sizeof(struct symt*), 4);
509 if (parent_block)
510 p = vector_add(&parent_block->vchildren, &module->pool);
511 else
512 p = vector_add(&func->vchildren, &module->pool);
513 *p = &block->symt;
515 return block;
518 struct symt_block* symt_close_func_block(struct module* module,
519 const struct symt_function* func,
520 struct symt_block* block, unsigned pc)
522 assert(func);
523 assert(func->symt.tag == SymTagFunction);
525 if (pc) block->size = func->address + pc - block->address;
526 return (block->container->tag == SymTagBlock) ?
527 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
530 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
531 struct symt_function* func,
532 enum SymTagEnum point,
533 const struct location* loc,
534 const char* name)
536 struct symt_hierarchy_point*sym;
537 struct symt** p;
539 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
541 sym->symt.tag = point;
542 sym->parent = &func->symt;
543 sym->loc = *loc;
544 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
545 p = vector_add(&func->vchildren, &module->pool);
546 *p = &sym->symt;
548 return sym;
551 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
553 unsigned len;
554 struct line_info* dli;
556 assert(func);
557 /* We aren't adding any more locals or line numbers to this function.
558 * Free any spare memory that we might have allocated.
560 assert(func->symt.tag == SymTagFunction);
562 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
563 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
565 len = vector_length(&func->vlines);
566 if (len--)
568 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
569 dli = vector_at(&func->vlines, len); dli->is_last = 1;
571 return TRUE;
574 struct symt_thunk* symt_new_thunk(struct module* module,
575 struct symt_compiland* compiland,
576 const char* name, THUNK_ORDINAL ord,
577 unsigned long addr, unsigned long size)
579 struct symt_thunk* sym;
581 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
582 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
584 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
586 sym->symt.tag = SymTagThunk;
587 sym->hash_elt.name = pool_strdup(&module->pool, name);
588 sym->container = &compiland->symt;
589 sym->address = addr;
590 sym->size = size;
591 sym->ordinal = ord;
592 symt_add_module_ht(module, (struct symt_ht*)sym);
593 if (compiland)
595 struct symt** p;
596 p = vector_add(&compiland->vchildren, &module->pool);
597 *p = &sym->symt;
600 return sym;
603 struct symt_data* symt_new_constant(struct module* module,
604 struct symt_compiland* compiland,
605 const char* name, struct symt* type,
606 const VARIANT* v)
608 struct symt_data* sym;
610 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
611 debugstr_w(module->module.ModuleName), name);
613 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
615 sym->symt.tag = SymTagData;
616 sym->hash_elt.name = pool_strdup(&module->pool, name);
617 sym->kind = DataIsConstant;
618 sym->container = compiland ? &compiland->symt : NULL;
619 sym->type = type;
620 sym->u.value = *v;
621 symt_add_module_ht(module, (struct symt_ht*)sym);
622 if (compiland)
624 struct symt** p;
625 p = vector_add(&compiland->vchildren, &module->pool);
626 *p = &sym->symt;
629 return sym;
632 struct symt_hierarchy_point* symt_new_label(struct module* module,
633 struct symt_compiland* compiland,
634 const char* name, unsigned long address)
636 struct symt_hierarchy_point* sym;
638 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
639 debugstr_w(module->module.ModuleName), name);
641 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
643 sym->symt.tag = SymTagLabel;
644 sym->hash_elt.name = pool_strdup(&module->pool, name);
645 sym->loc.kind = loc_absolute;
646 sym->loc.offset = address;
647 sym->parent = compiland ? &compiland->symt : NULL;
648 symt_add_module_ht(module, (struct symt_ht*)sym);
649 if (compiland)
651 struct symt** p;
652 p = vector_add(&compiland->vchildren, &module->pool);
653 *p = &sym->symt;
656 return sym;
659 /* expect sym_info->MaxNameLen to be set before being called */
660 static void symt_fill_sym_info(struct module_pair* pair,
661 const struct symt_function* func,
662 const struct symt* sym, SYMBOL_INFO* sym_info)
664 const char* name;
665 DWORD64 size;
667 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
668 sym_info->TypeIndex = 0;
669 sym_info->info = symt_ptr2index(pair->effective, sym);
670 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
671 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
672 (!sym_info->TypeIndex ||
673 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
674 TI_GET_LENGTH, &size)))
675 size = 0;
676 sym_info->Size = (DWORD)size;
677 sym_info->ModBase = pair->requested->module.BaseOfImage;
678 sym_info->Flags = 0;
679 sym_info->Value = 0;
681 switch (sym->tag)
683 case SymTagData:
685 const struct symt_data* data = (const struct symt_data*)sym;
686 switch (data->kind)
688 case DataIsParam:
689 sym_info->Flags |= SYMFLAG_PARAMETER;
690 /* fall through */
691 case DataIsLocal:
693 struct location loc = data->u.var;
695 if (loc.kind >= loc_user)
696 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
698 switch (loc.kind)
700 case loc_error:
701 /* for now we report error cases as a negative register number */
702 sym_info->Flags |= SYMFLAG_LOCAL;
703 /* fall through */
704 case loc_register:
705 sym_info->Flags |= SYMFLAG_REGISTER;
706 sym_info->Register = loc.reg;
707 sym_info->Address = 0;
708 break;
709 case loc_regrel:
710 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
711 /* FIXME: it's i386 dependent !!! */
712 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
713 sym_info->Address = loc.offset;
714 break;
715 default:
716 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
717 assert(0);
720 break;
721 case DataIsGlobal:
722 case DataIsFileStatic:
723 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
724 sym_info->Register = 0;
725 break;
726 case DataIsConstant:
727 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
728 switch (data->u.value.n1.n2.vt)
730 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
731 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
732 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
733 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
734 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
735 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
736 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
737 case VT_EMPTY: sym_info->Value = 0; break;
738 default:
739 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
740 sym_info->Value = 0;
741 break;
743 break;
744 default:
745 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
748 break;
749 case SymTagPublicSymbol:
750 sym_info->Flags |= SYMFLAG_EXPORT;
751 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
752 break;
753 case SymTagFunction:
754 sym_info->Flags |= SYMFLAG_FUNCTION;
755 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
756 break;
757 case SymTagThunk:
758 sym_info->Flags |= SYMFLAG_THUNK;
759 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
760 break;
761 default:
762 symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
763 sym_info->Register = 0;
764 break;
766 sym_info->Scope = 0; /* FIXME */
767 sym_info->Tag = sym->tag;
768 name = symt_get_name(sym);
769 if (sym_info->MaxNameLen)
771 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
772 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
773 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
775 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
776 memcpy(sym_info->Name, name, sym_info->NameLen);
777 sym_info->Name[sym_info->NameLen] = '\0';
780 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
781 sym, sym_info->Name, sym_info->Size,
782 wine_dbgstr_longlong(sym_info->Address));
785 struct sym_enum
787 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
788 PVOID user;
789 SYMBOL_INFO* sym_info;
790 DWORD index;
791 DWORD tag;
792 DWORD64 addr;
793 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
796 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
797 const struct symt_function* func, const struct symt* sym)
799 symt_fill_sym_info(pair, func, sym, se->sym_info);
800 if (se->index && se->sym_info->info != se->index) return FALSE;
801 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
802 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
803 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
806 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
807 const struct sym_enum* se)
809 void* ptr;
810 struct symt_ht* sym = NULL;
811 struct hash_table_iter hti;
813 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
814 while ((ptr = hash_table_iter_up(&hti)))
816 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
817 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
819 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
820 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
821 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
824 return FALSE;
827 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
829 unsigned low = 0, mid = high / 2;
830 ULONG64 addr;
832 if (!high) return 0;
833 symt_get_info(module, &elt->symt, TI_GET_ADDRESS, &addr);
836 switch (cmp_sorttab_addr(module, mid, addr))
838 case 0: return mid;
839 case -1: low = mid + 1; break;
840 case 1: high = mid; break;
842 mid = low + (high - low) / 2;
843 } while (low < high);
844 return mid;
847 /***********************************************************************
848 * resort_symbols
850 * Rebuild sorted list of symbols for a module.
852 static BOOL resort_symbols(struct module* module)
854 if (!(module->module.NumSyms = module->num_symbols))
855 return FALSE;
857 /* FIXME: what's the optimal value here ??? */
858 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
860 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
861 struct symt_ht* tmp[30];
863 delta = module->num_symbols - module->num_sorttab;
864 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
865 symt_cmp_addr_module = module;
866 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
868 for (i = delta - 1; i >= 0; i--)
870 prev_ins_idx = ins_idx;
871 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
872 memmove(&module->addr_sorttab[ins_idx + i + 1],
873 &module->addr_sorttab[ins_idx],
874 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
875 module->addr_sorttab[ins_idx + i] = tmp[i];
878 else
880 symt_cmp_addr_module = module;
881 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
883 module->num_sorttab = module->num_symbols;
884 return module->sortlist_valid = TRUE;
887 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
889 DWORD type_index;
891 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
892 return;
894 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
895 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
896 *size = 0x1000; /* arbitrary value */
899 /* assume addr is in module */
900 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
902 int mid, high, low;
903 ULONG64 ref_addr, ref_size;
905 if (!module->sortlist_valid || !module->addr_sorttab)
907 if (!resort_symbols(module)) return NULL;
911 * Binary search to find closest symbol.
913 low = 0;
914 high = module->num_sorttab;
916 symt_get_info(module, &module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
917 if (addr < ref_addr) return NULL;
918 if (high)
920 symt_get_info(module, &module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
921 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
922 if (addr >= ref_addr + ref_size) return NULL;
925 while (high > low + 1)
927 mid = (high + low) / 2;
928 if (cmp_sorttab_addr(module, mid, addr) < 0)
929 low = mid;
930 else
931 high = mid;
933 if (low != high && high != module->num_sorttab &&
934 cmp_sorttab_addr(module, high, addr) <= 0)
935 low = high;
937 /* If found symbol is a public symbol, check if there are any other entries that
938 * might also have the same address, but would get better information
940 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
942 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
943 if (low > 0 &&
944 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
945 !cmp_sorttab_addr(module, low - 1, ref_addr))
946 low--;
947 else if (low < module->num_sorttab - 1 &&
948 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
949 !cmp_sorttab_addr(module, low + 1, ref_addr))
950 low++;
952 /* finally check that we fit into the found symbol */
953 symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
954 if (addr < ref_addr) return NULL;
955 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
956 if (addr >= ref_addr + ref_size) return NULL;
958 return module->addr_sorttab[low];
961 static BOOL symt_enum_locals_helper(struct module_pair* pair,
962 regex_t* preg, const struct sym_enum* se,
963 struct symt_function* func, const struct vector* v)
965 struct symt* lsym = NULL;
966 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
967 unsigned int i;
969 for (i=0; i<vector_length(v); i++)
971 lsym = *(struct symt**)vector_at(v, i);
972 switch (lsym->tag)
974 case SymTagBlock:
976 struct symt_block* block = (struct symt_block*)lsym;
977 if (pc < block->address || block->address + block->size <= pc)
978 continue;
979 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
980 return FALSE;
982 break;
983 case SymTagData:
984 if (match_regexp(preg, symt_get_name(lsym)))
986 if (send_symbol(se, pair, func, lsym)) return FALSE;
988 break;
989 case SymTagLabel:
990 case SymTagFuncDebugStart:
991 case SymTagFuncDebugEnd:
992 case SymTagCustom:
993 break;
994 default:
995 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
996 assert(0);
999 return TRUE;
1002 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
1003 const struct sym_enum* se)
1005 struct module_pair pair;
1006 struct symt_ht* sym;
1007 DWORD pc = pcs->ctx_frame.InstructionOffset;
1009 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1010 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1012 pair.pcs = pcs;
1013 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1014 if (!module_get_debug(&pair)) return FALSE;
1015 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1017 if (sym->symt.tag == SymTagFunction)
1019 BOOL ret;
1020 regex_t preg;
1022 compile_regex(mask ? mask : "*", -1, &preg,
1023 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1024 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1025 &((struct symt_function*)sym)->vchildren);
1026 regfree(&preg);
1027 return ret;
1030 return send_symbol(se, &pair, NULL, &sym->symt);
1033 /******************************************************************
1034 * copy_symbolW
1036 * Helper for transforming an ANSI symbol info into a UNICODE one.
1037 * Assume that MaxNameLen is the same for both version (A & W).
1039 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1041 siw->SizeOfStruct = si->SizeOfStruct;
1042 siw->TypeIndex = si->TypeIndex;
1043 siw->Reserved[0] = si->Reserved[0];
1044 siw->Reserved[1] = si->Reserved[1];
1045 siw->Index = si->info; /* FIXME: see dbghelp.h */
1046 siw->Size = si->Size;
1047 siw->ModBase = si->ModBase;
1048 siw->Flags = si->Flags;
1049 siw->Value = si->Value;
1050 siw->Address = si->Address;
1051 siw->Register = si->Register;
1052 siw->Scope = si->Scope;
1053 siw->Tag = si->Tag;
1054 siw->NameLen = si->NameLen;
1055 siw->MaxNameLen = si->MaxNameLen;
1056 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1059 /******************************************************************
1060 * sym_enum
1062 * Core routine for most of the enumeration of symbols
1064 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1065 const struct sym_enum* se)
1067 struct module_pair pair;
1068 const char* bang;
1069 regex_t mod_regex, sym_regex;
1071 pair.pcs = process_find_by_handle(hProcess);
1072 if (!pair.pcs) return FALSE;
1073 if (BaseOfDll == 0)
1075 /* do local variables ? */
1076 if (!Mask || !(bang = strchr(Mask, '!')))
1077 return symt_enum_locals(pair.pcs, Mask, se);
1079 if (bang == Mask) return FALSE;
1081 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1082 compile_regex(bang + 1, -1, &sym_regex,
1083 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1085 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1087 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1089 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1090 symt_enum_module(&pair, &sym_regex, se))
1091 break;
1094 /* not found in PE modules, retry on the ELF ones
1096 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1098 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1100 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1101 !module_get_containee(pair.pcs, pair.requested) &&
1102 module_get_debug(&pair))
1104 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1105 symt_enum_module(&pair, &sym_regex, se))
1106 break;
1110 regfree(&mod_regex);
1111 regfree(&sym_regex);
1112 return TRUE;
1114 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1115 if (!module_get_debug(&pair))
1116 return FALSE;
1118 /* we always ignore module name from Mask when BaseOfDll is defined */
1119 if (Mask && (bang = strchr(Mask, '!')))
1121 if (bang == Mask) return FALSE;
1122 Mask = bang + 1;
1125 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1126 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1127 symt_enum_module(&pair, &sym_regex, se);
1128 regfree(&sym_regex);
1130 return TRUE;
1133 /******************************************************************
1134 * SymEnumSymbols (DBGHELP.@)
1136 * cases BaseOfDll = 0
1137 * !foo fails always (despite what MSDN states)
1138 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1139 * no ! in Mask, lookup in local Context
1140 * cases BaseOfDll != 0
1141 * !foo fails always (despite what MSDN states)
1142 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1144 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1145 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1146 PVOID UserContext)
1148 struct sym_enum se;
1150 TRACE("(%p %s %s %p %p)\n",
1151 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1152 EnumSymbolsCallback, UserContext);
1154 se.cb = EnumSymbolsCallback;
1155 se.user = UserContext;
1156 se.index = 0;
1157 se.tag = 0;
1158 se.addr = 0;
1159 se.sym_info = (PSYMBOL_INFO)se.buffer;
1161 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1164 struct sym_enumW
1166 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1167 void* ctx;
1168 PSYMBOL_INFOW sym_info;
1169 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1173 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1175 struct sym_enumW* sew = ctx;
1177 copy_symbolW(sew->sym_info, si);
1179 return (sew->cb)(sew->sym_info, size, sew->ctx);
1182 /******************************************************************
1183 * SymEnumSymbolsW (DBGHELP.@)
1186 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1187 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1188 PVOID UserContext)
1190 struct sym_enumW sew;
1191 BOOL ret = FALSE;
1192 char* maskA = NULL;
1194 sew.ctx = UserContext;
1195 sew.cb = EnumSymbolsCallback;
1196 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1198 if (Mask)
1200 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1201 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1202 if (!maskA) return FALSE;
1203 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1205 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1206 HeapFree(GetProcessHeap(), 0, maskA);
1208 return ret;
1211 struct sym_enumerate
1213 void* ctx;
1214 PSYM_ENUMSYMBOLS_CALLBACK cb;
1217 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1219 struct sym_enumerate* se = ctx;
1220 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1223 /***********************************************************************
1224 * SymEnumerateSymbols (DBGHELP.@)
1226 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1227 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1228 PVOID UserContext)
1230 struct sym_enumerate se;
1232 se.ctx = UserContext;
1233 se.cb = EnumSymbolsCallback;
1235 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1238 struct sym_enumerate64
1240 void* ctx;
1241 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1244 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1246 struct sym_enumerate64* se = ctx;
1247 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1250 /***********************************************************************
1251 * SymEnumerateSymbols64 (DBGHELP.@)
1253 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1254 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1255 PVOID UserContext)
1257 struct sym_enumerate64 se;
1259 se.ctx = UserContext;
1260 se.cb = EnumSymbolsCallback;
1262 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1265 /******************************************************************
1266 * SymFromAddr (DBGHELP.@)
1269 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1270 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1272 struct module_pair pair;
1273 struct symt_ht* sym;
1275 pair.pcs = process_find_by_handle(hProcess);
1276 if (!pair.pcs) return FALSE;
1277 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1278 if (!module_get_debug(&pair)) return FALSE;
1279 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1281 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1282 *Displacement = Address - Symbol->Address;
1283 return TRUE;
1286 /******************************************************************
1287 * SymFromAddrW (DBGHELP.@)
1290 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1291 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1293 PSYMBOL_INFO si;
1294 unsigned len;
1295 BOOL ret;
1297 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1298 si = HeapAlloc(GetProcessHeap(), 0, len);
1299 if (!si) return FALSE;
1301 si->SizeOfStruct = sizeof(*si);
1302 si->MaxNameLen = Symbol->MaxNameLen;
1303 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1305 copy_symbolW(Symbol, si);
1307 HeapFree(GetProcessHeap(), 0, si);
1308 return ret;
1311 /******************************************************************
1312 * SymGetSymFromAddr (DBGHELP.@)
1315 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1316 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1318 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1319 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1320 size_t len;
1321 DWORD64 Displacement64;
1323 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1324 si->SizeOfStruct = sizeof(*si);
1325 si->MaxNameLen = MAX_SYM_NAME;
1326 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1327 return FALSE;
1329 if (Displacement)
1330 *Displacement = Displacement64;
1331 Symbol->Address = si->Address;
1332 Symbol->Size = si->Size;
1333 Symbol->Flags = si->Flags;
1334 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1335 lstrcpynA(Symbol->Name, si->Name, len);
1336 return TRUE;
1339 /******************************************************************
1340 * SymGetSymFromAddr64 (DBGHELP.@)
1343 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1344 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1346 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1347 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1348 size_t len;
1349 DWORD64 Displacement64;
1351 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1352 si->SizeOfStruct = sizeof(*si);
1353 si->MaxNameLen = MAX_SYM_NAME;
1354 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1355 return FALSE;
1357 if (Displacement)
1358 *Displacement = Displacement64;
1359 Symbol->Address = si->Address;
1360 Symbol->Size = si->Size;
1361 Symbol->Flags = si->Flags;
1362 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1363 lstrcpynA(Symbol->Name, si->Name, len);
1364 return TRUE;
1367 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1368 SYMBOL_INFO* symbol)
1370 struct hash_table_iter hti;
1371 void* ptr;
1372 struct symt_ht* sym = NULL;
1373 struct module_pair pair;
1375 pair.pcs = pcs;
1376 if (!(pair.requested = module)) return FALSE;
1377 if (!module_get_debug(&pair)) return FALSE;
1379 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1380 while ((ptr = hash_table_iter_up(&hti)))
1382 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1384 if (!strcmp(sym->hash_elt.name, name))
1386 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1387 return TRUE;
1390 return FALSE;
1393 /******************************************************************
1394 * SymFromName (DBGHELP.@)
1397 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1399 struct process* pcs = process_find_by_handle(hProcess);
1400 struct module* module;
1401 const char* name;
1403 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1404 if (!pcs) return FALSE;
1405 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1406 name = strchr(Name, '!');
1407 if (name)
1409 char tmp[128];
1410 assert(name - Name < sizeof(tmp));
1411 memcpy(tmp, Name, name - Name);
1412 tmp[name - Name] = '\0';
1413 module = module_find_by_nameA(pcs, tmp);
1414 return find_name(pcs, module, name + 1, Symbol);
1416 for (module = pcs->lmodules; module; module = module->next)
1418 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1419 return TRUE;
1421 /* not found in PE modules, retry on the ELF ones
1423 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1425 for (module = pcs->lmodules; module; module = module->next)
1427 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1428 !module_get_containee(pcs, module) &&
1429 find_name(pcs, module, Name, Symbol))
1430 return TRUE;
1433 return FALSE;
1436 /***********************************************************************
1437 * SymGetSymFromName64 (DBGHELP.@)
1439 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1441 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1442 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1443 size_t len;
1445 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1446 si->SizeOfStruct = sizeof(*si);
1447 si->MaxNameLen = MAX_SYM_NAME;
1448 if (!SymFromName(hProcess, Name, si)) return FALSE;
1450 Symbol->Address = si->Address;
1451 Symbol->Size = si->Size;
1452 Symbol->Flags = si->Flags;
1453 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1454 lstrcpynA(Symbol->Name, si->Name, len);
1455 return TRUE;
1458 /***********************************************************************
1459 * SymGetSymFromName (DBGHELP.@)
1461 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1463 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1464 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1465 size_t len;
1467 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1468 si->SizeOfStruct = sizeof(*si);
1469 si->MaxNameLen = MAX_SYM_NAME;
1470 if (!SymFromName(hProcess, Name, si)) return FALSE;
1472 Symbol->Address = si->Address;
1473 Symbol->Size = si->Size;
1474 Symbol->Flags = si->Flags;
1475 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1476 lstrcpynA(Symbol->Name, si->Name, len);
1477 return TRUE;
1480 /******************************************************************
1481 * sym_fill_func_line_info
1483 * fills information about a file
1485 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1486 DWORD64 addr, IMAGEHLP_LINE64* line)
1488 struct line_info* dli = NULL;
1489 BOOL found = FALSE;
1490 int i;
1492 assert(func->symt.tag == SymTagFunction);
1494 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1496 dli = vector_at(&func->vlines, i);
1497 if (!dli->is_source_file)
1499 if (found || dli->u.pc_offset > addr) continue;
1500 line->LineNumber = dli->line_number;
1501 line->Address = dli->u.pc_offset;
1502 line->Key = dli;
1503 found = TRUE;
1504 continue;
1506 if (found)
1508 line->FileName = (char*)source_get(module, dli->u.source_file);
1509 return TRUE;
1512 return FALSE;
1515 /***********************************************************************
1516 * SymGetSymNext64 (DBGHELP.@)
1518 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1520 /* algo:
1521 * get module from Symbol.Address
1522 * get index in module.addr_sorttab of Symbol.Address
1523 * increment index
1524 * if out of module bounds, move to next module in process address space
1526 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1527 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1528 return FALSE;
1531 /***********************************************************************
1532 * SymGetSymNext (DBGHELP.@)
1534 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1536 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1537 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1538 return FALSE;
1541 /***********************************************************************
1542 * SymGetSymPrev64 (DBGHELP.@)
1544 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1546 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1547 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1548 return FALSE;
1551 /***********************************************************************
1552 * SymGetSymPrev (DBGHELP.@)
1554 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1556 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1557 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1558 return FALSE;
1561 /******************************************************************
1562 * copy_line_64_from_32 (internal)
1565 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1568 l64->Key = l32->Key;
1569 l64->LineNumber = l32->LineNumber;
1570 l64->FileName = l32->FileName;
1571 l64->Address = l32->Address;
1574 /******************************************************************
1575 * copy_line_W64_from_32 (internal)
1578 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1580 unsigned len;
1582 l64w->Key = l64->Key;
1583 l64w->LineNumber = l64->LineNumber;
1584 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1585 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1586 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1587 l64w->Address = l64->Address;
1590 /******************************************************************
1591 * copy_line_32_from_64 (internal)
1594 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1597 l32->Key = l64->Key;
1598 l32->LineNumber = l64->LineNumber;
1599 l32->FileName = l64->FileName;
1600 l32->Address = l64->Address;
1603 /******************************************************************
1604 * SymGetLineFromAddr (DBGHELP.@)
1607 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1608 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1610 IMAGEHLP_LINE64 il64;
1612 il64.SizeOfStruct = sizeof(il64);
1613 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1614 return FALSE;
1615 copy_line_32_from_64(Line, &il64);
1616 return TRUE;
1619 /******************************************************************
1620 * SymGetLineFromAddr64 (DBGHELP.@)
1623 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1624 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1626 struct module_pair pair;
1627 struct symt_ht* symt;
1629 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1631 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1633 pair.pcs = process_find_by_handle(hProcess);
1634 if (!pair.pcs) return FALSE;
1635 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1636 if (!module_get_debug(&pair)) return FALSE;
1637 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1639 if (symt->symt.tag != SymTagFunction) return FALSE;
1640 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1641 dwAddr, Line)) return FALSE;
1642 *pdwDisplacement = dwAddr - Line->Address;
1643 return TRUE;
1646 /******************************************************************
1647 * SymGetLineFromAddrW64 (DBGHELP.@)
1650 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1651 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1653 IMAGEHLP_LINE64 il64;
1655 il64.SizeOfStruct = sizeof(il64);
1656 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1657 return FALSE;
1658 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1659 return TRUE;
1662 /******************************************************************
1663 * SymGetLinePrev64 (DBGHELP.@)
1666 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1668 struct module_pair pair;
1669 struct line_info* li;
1670 BOOL in_search = FALSE;
1672 TRACE("(%p %p)\n", hProcess, Line);
1674 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1676 pair.pcs = process_find_by_handle(hProcess);
1677 if (!pair.pcs) return FALSE;
1678 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1679 if (!module_get_debug(&pair)) return FALSE;
1681 if (Line->Key == 0) return FALSE;
1682 li = Line->Key;
1683 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1684 * element we have to go back until we find the prev one to get the real
1685 * source file name for the DLIT_OFFSET element just before
1686 * the first DLIT_SOURCEFILE
1688 while (!li->is_first)
1690 li--;
1691 if (!li->is_source_file)
1693 Line->LineNumber = li->line_number;
1694 Line->Address = li->u.pc_offset;
1695 Line->Key = li;
1696 if (!in_search) return TRUE;
1698 else
1700 if (in_search)
1702 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1703 return TRUE;
1705 in_search = TRUE;
1708 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1709 return FALSE;
1712 /******************************************************************
1713 * SymGetLinePrev (DBGHELP.@)
1716 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1718 IMAGEHLP_LINE64 line64;
1720 line64.SizeOfStruct = sizeof(line64);
1721 copy_line_64_from_32(&line64, Line);
1722 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1723 copy_line_32_from_64(Line, &line64);
1724 return TRUE;
1727 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1729 struct line_info* li;
1731 if (line->Key == 0) return FALSE;
1732 li = line->Key;
1733 while (!li->is_last)
1735 li++;
1736 if (!li->is_source_file)
1738 line->LineNumber = li->line_number;
1739 line->Address = li->u.pc_offset;
1740 line->Key = li;
1741 return TRUE;
1743 line->FileName = (char*)source_get(module, li->u.source_file);
1745 return FALSE;
1748 /******************************************************************
1749 * SymGetLineNext64 (DBGHELP.@)
1752 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1754 struct module_pair pair;
1756 TRACE("(%p %p)\n", hProcess, Line);
1758 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1759 pair.pcs = process_find_by_handle(hProcess);
1760 if (!pair.pcs) return FALSE;
1761 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1762 if (!module_get_debug(&pair)) return FALSE;
1764 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1765 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1766 return FALSE;
1769 /******************************************************************
1770 * SymGetLineNext (DBGHELP.@)
1773 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1775 IMAGEHLP_LINE64 line64;
1777 line64.SizeOfStruct = sizeof(line64);
1778 copy_line_64_from_32(&line64, Line);
1779 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1780 copy_line_32_from_64(Line, &line64);
1781 return TRUE;
1784 /***********************************************************************
1785 * SymFunctionTableAccess (DBGHELP.@)
1787 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1789 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1790 return NULL;
1793 /***********************************************************************
1794 * SymFunctionTableAccess64 (DBGHELP.@)
1796 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1798 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1799 return NULL;
1802 /***********************************************************************
1803 * SymUnDName (DBGHELP.@)
1805 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1807 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1808 UNDNAME_COMPLETE) != 0;
1811 /***********************************************************************
1812 * SymUnDName64 (DBGHELP.@)
1814 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1816 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1817 UNDNAME_COMPLETE) != 0;
1820 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1821 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1823 /***********************************************************************
1824 * UnDecorateSymbolName (DBGHELP.@)
1826 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1827 DWORD UndecoratedLength, DWORD Flags)
1829 /* undocumented from msvcrt */
1830 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1831 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1833 TRACE("(%s, %p, %d, 0x%08x)\n",
1834 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1836 if (!p_undname)
1838 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1839 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1840 if (!p_undname) return 0;
1843 if (!UnDecoratedName) return 0;
1844 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1845 und_alloc, und_free, Flags))
1846 return 0;
1847 return strlen(UnDecoratedName);
1850 /******************************************************************
1851 * SymMatchString (DBGHELP.@)
1854 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1856 regex_t preg;
1857 BOOL ret;
1859 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1861 compile_regex(re, -1, &preg, _case);
1862 ret = match_regexp(&preg, string);
1863 regfree(&preg);
1864 return ret;
1867 /******************************************************************
1868 * SymSearch (DBGHELP.@)
1870 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1871 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1872 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1873 PVOID UserContext, DWORD Options)
1875 struct sym_enum se;
1877 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1878 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1879 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1880 UserContext, Options);
1882 if (Options != SYMSEARCH_GLOBALSONLY)
1884 FIXME("Unsupported searching with options (%x)\n", Options);
1885 SetLastError(ERROR_INVALID_PARAMETER);
1886 return FALSE;
1889 se.cb = EnumSymbolsCallback;
1890 se.user = UserContext;
1891 se.index = Index;
1892 se.tag = SymTag;
1893 se.addr = Address;
1894 se.sym_info = (PSYMBOL_INFO)se.buffer;
1896 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1899 /******************************************************************
1900 * SymSearchW (DBGHELP.@)
1902 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1903 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1904 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1905 PVOID UserContext, DWORD Options)
1907 struct sym_enumW sew;
1908 BOOL ret = FALSE;
1909 char* maskA = NULL;
1911 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1912 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1913 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1914 UserContext, Options);
1916 sew.ctx = UserContext;
1917 sew.cb = EnumSymbolsCallback;
1918 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1920 if (Mask)
1922 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1923 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1924 if (!maskA) return FALSE;
1925 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1927 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1928 sym_enumW, &sew, Options);
1929 HeapFree(GetProcessHeap(), 0, maskA);
1931 return ret;
1934 /******************************************************************
1935 * SymAddSymbol (DBGHELP.@)
1938 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1939 DWORD64 addr, DWORD size, DWORD flags)
1941 WCHAR nameW[MAX_SYM_NAME];
1943 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1944 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1947 /******************************************************************
1948 * SymAddSymbolW (DBGHELP.@)
1951 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1952 DWORD64 addr, DWORD size, DWORD flags)
1954 struct module_pair pair;
1956 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1958 pair.pcs = process_find_by_handle(hProcess);
1959 if (!pair.pcs) return FALSE;
1960 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1961 if (!module_get_debug(&pair)) return FALSE;
1963 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1964 return FALSE;
1967 /******************************************************************
1968 * SymSetScopeFromAddr (DBGHELP.@)
1970 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1972 struct process* pcs;
1974 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1976 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1977 return TRUE;
1980 /******************************************************************
1981 * SymEnumLines (DBGHELP.@)
1984 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1985 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1987 struct module_pair pair;
1988 struct hash_table_iter hti;
1989 struct symt_ht* sym;
1990 regex_t re;
1991 struct line_info* dli;
1992 void* ptr;
1993 SRCCODEINFO sci;
1994 const char* file;
1996 if (!cb) return FALSE;
1997 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1999 pair.pcs = process_find_by_handle(hProcess);
2000 if (!pair.pcs) return FALSE;
2001 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2002 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2003 if (!module_get_debug(&pair)) return FALSE;
2004 if (!compile_file_regex(&re, srcfile)) return FALSE;
2006 sci.SizeOfStruct = sizeof(sci);
2007 sci.ModBase = base;
2009 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2010 while ((ptr = hash_table_iter_up(&hti)))
2012 unsigned int i;
2014 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2015 if (sym->symt.tag != SymTagFunction) continue;
2017 sci.FileName[0] = '\0';
2018 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2020 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2021 if (dli->is_source_file)
2023 file = source_get(pair.effective, dli->u.source_file);
2024 if (!match_regexp(&re, file)) file = "";
2025 strcpy(sci.FileName, file);
2027 else if (sci.FileName[0])
2029 sci.Key = dli;
2030 sci.Obj[0] = '\0'; /* FIXME */
2031 sci.LineNumber = dli->line_number;
2032 sci.Address = dli->u.pc_offset;
2033 if (!cb(&sci, user)) break;
2037 regfree(&re);
2038 return TRUE;