mshtml: Added generic bind_to_tree node notification and use it to set frame's conten...
[wine/multimedia.git] / dlls / dbghelp / symbol.c
blobc322a62c45af7656d8173bdeb4a9ef9bbfc03de0
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(const struct module* module, int idx, ULONG64 addr)
53 ULONG64 ref;
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;
63 ULONG64 a1, a2;
65 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
66 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
67 return cmp_addr(a1, a2);
70 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
72 struct symt_ht** new;
73 unsigned int size;
75 if (sz <= module->sorttab_size) return TRUE;
76 if (module->addr_sorttab)
78 size = module->sorttab_size * 2;
79 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
80 size * sizeof(struct symt_ht*));
82 else
84 size = 64;
85 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
87 if (!new) return FALSE;
88 module->sorttab_size = size;
89 module->addr_sorttab = new;
90 return TRUE;
93 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
95 ULONG64 addr;
97 hash_table_add(&module->ht_symbols, &ht->hash_elt);
98 /* Don't store in sorttab a symbol without address, they are of
99 * no use here (e.g. constant values)
101 if (symt_get_info(&ht->symt, TI_GET_ADDRESS, &addr) &&
102 symt_grow_sorttab(module, module->num_symbols + 1))
104 module->addr_sorttab[module->num_symbols++] = ht;
105 module->sortlist_valid = FALSE;
109 #ifdef HAVE_REGEX_H
111 /* transforms a dbghelp's regular expression into a POSIX one
112 * Here are the valid dbghelp reg ex characters:
113 * * 0 or more characters
114 * ? a single character
115 * [] list
116 * # 0 or more of preceding char
117 * + 1 or more of preceding char
118 * escapes \ on #, ?, [, ], *, +. don't work on -
120 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
122 char *mask, *p;
123 BOOL in_escape = FALSE;
124 unsigned flags = REG_NOSUB;
126 if (numchar == -1) numchar = strlen( str );
128 p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
129 *p++ = '^';
131 while (*str && numchar--)
133 /* FIXME: this shouldn't be valid on '-' */
134 if (in_escape)
136 *p++ = '\\';
137 *p++ = *str;
138 in_escape = FALSE;
140 else switch (*str)
142 case '\\': in_escape = TRUE; break;
143 case '*': *p++ = '.'; *p++ = '*'; break;
144 case '?': *p++ = '.'; break;
145 case '#': *p++ = '*'; break;
146 /* escape some valid characters in dbghelp reg exp:s */
147 case '$': *p++ = '\\'; *p++ = '$'; break;
148 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
149 default: *p++ = *str; break;
151 str++;
153 if (in_escape)
155 *p++ = '\\';
156 *p++ = '\\';
158 *p++ = '$';
159 *p = 0;
160 if (_case) flags |= REG_ICASE;
161 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
162 HeapFree(GetProcessHeap(), 0, mask);
165 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
167 char *mask, *p;
168 BOOL ret;
170 if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
172 p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
173 *p++ = '^';
174 while (*srcfile)
176 switch (*srcfile)
178 case '\\':
179 case '/':
180 *p++ = '[';
181 *p++ = '\\';
182 *p++ = '\\';
183 *p++ = '/';
184 *p++ = ']';
185 break;
186 case '.':
187 *p++ = '\\';
188 *p++ = '.';
189 break;
190 default:
191 *p++ = *srcfile;
192 break;
194 srcfile++;
196 *p++ = '$';
197 *p = 0;
198 ret = !regcomp(re, mask, REG_NOSUB);
199 HeapFree(GetProcessHeap(), 0, mask);
200 if (!ret)
202 FIXME("Couldn't compile %s\n", mask);
203 SetLastError(ERROR_INVALID_PARAMETER);
205 return ret;
208 static int match_regexp( const regex_t *re, const char *str )
210 return !regexec( re, str, 0, NULL, 0 );
213 #else /* HAVE_REGEX_H */
215 /* if we don't have regexp support, fall back to a simple string comparison */
217 typedef struct
219 char *str;
220 BOOL icase;
221 } regex_t;
223 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
225 if (numchar == -1) numchar = strlen( str );
227 re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
228 memcpy( re->str, str, numchar );
229 re->str[numchar] = 0;
230 re->icase = _case;
233 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
235 if (!srcfile || !*srcfile) re->str = NULL;
236 else compile_regex( srcfile, -1, re, FALSE );
237 return TRUE;
240 static int match_regexp( const regex_t *re, const char *str )
242 if (!re->str) return 1;
243 if (re->icase) return !lstrcmpiA( re->str, str );
244 return !strcmp( re->str, str );
247 static void regfree( regex_t *re )
249 HeapFree( GetProcessHeap(), 0, re->str );
252 #endif /* HAVE_REGEX_H */
254 struct symt_compiland* symt_new_compiland(struct module* module,
255 unsigned long address, unsigned src_idx)
257 struct symt_compiland* sym;
259 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
260 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
261 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
263 sym->symt.tag = SymTagCompiland;
264 sym->address = address;
265 sym->source = src_idx;
266 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
268 return sym;
271 struct symt_public* symt_new_public(struct module* module,
272 struct symt_compiland* compiland,
273 const char* name,
274 unsigned long address, unsigned size,
275 BOOL in_code, BOOL is_func)
277 struct symt_public* sym;
278 struct symt** p;
280 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
281 debugstr_w(module->module.ModuleName), name, address);
282 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
283 symt_find_nearest(module, address) != NULL)
284 return NULL;
285 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
287 sym->symt.tag = SymTagPublicSymbol;
288 sym->hash_elt.name = pool_strdup(&module->pool, name);
289 sym->container = compiland ? &compiland->symt : NULL;
290 sym->address = address;
291 sym->size = size;
292 sym->in_code = in_code;
293 sym->is_function = is_func;
294 symt_add_module_ht(module, (struct symt_ht*)sym);
295 if (compiland)
297 p = vector_add(&compiland->vchildren, &module->pool);
298 *p = &sym->symt;
301 return sym;
304 struct symt_data* symt_new_global_variable(struct module* module,
305 struct symt_compiland* compiland,
306 const char* name, unsigned is_static,
307 unsigned long addr, unsigned long size,
308 struct symt* type)
310 struct symt_data* sym;
311 struct symt** p;
312 DWORD64 tsz;
314 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
315 debugstr_w(module->module.ModuleName), name, addr, type);
316 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
318 sym->symt.tag = SymTagData;
319 sym->hash_elt.name = pool_strdup(&module->pool, name);
320 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
321 sym->container = compiland ? &compiland->symt : NULL;
322 sym->type = type;
323 sym->u.var.offset = addr;
324 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
326 if (tsz != size)
327 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
328 debugstr_w(module->module.ModuleName), name,
329 wine_dbgstr_longlong(tsz), size);
331 symt_add_module_ht(module, (struct symt_ht*)sym);
332 if (compiland)
334 p = vector_add(&compiland->vchildren, &module->pool);
335 *p = &sym->symt;
338 return sym;
341 struct symt_function* symt_new_function(struct module* module,
342 struct symt_compiland* compiland,
343 const char* name,
344 unsigned long addr, unsigned long size,
345 struct symt* sig_type)
347 struct symt_function* sym;
348 struct symt** p;
350 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
351 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
353 assert(!sig_type || sig_type->tag == SymTagFunctionType);
354 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
356 sym->symt.tag = SymTagFunction;
357 sym->hash_elt.name = pool_strdup(&module->pool, name);
358 sym->container = &compiland->symt;
359 sym->address = addr;
360 sym->type = sig_type;
361 sym->size = size;
362 vector_init(&sym->vlines, sizeof(struct line_info), 64);
363 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
364 symt_add_module_ht(module, (struct symt_ht*)sym);
365 if (compiland)
367 p = vector_add(&compiland->vchildren, &module->pool);
368 *p = &sym->symt;
371 return sym;
374 void symt_add_func_line(struct module* module, struct symt_function* func,
375 unsigned source_idx, int line_num, unsigned long offset)
377 struct line_info* dli;
378 BOOL last_matches = FALSE;
379 int i;
381 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
383 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
384 func, func->hash_elt.name, offset,
385 source_get(module, source_idx), line_num);
387 assert(func->symt.tag == SymTagFunction);
389 for (i=vector_length(&func->vlines)-1; i>=0; i--)
391 dli = vector_at(&func->vlines, i);
392 if (dli->is_source_file)
394 last_matches = (source_idx == dli->u.source_file);
395 break;
399 if (!last_matches)
401 /* we shouldn't have line changes on first line of function */
402 dli = vector_add(&func->vlines, &module->pool);
403 dli->is_source_file = 1;
404 dli->is_first = dli->is_last = 0;
405 dli->line_number = 0;
406 dli->u.source_file = source_idx;
408 dli = vector_add(&func->vlines, &module->pool);
409 dli->is_source_file = 0;
410 dli->is_first = dli->is_last = 0;
411 dli->line_number = line_num;
412 dli->u.pc_offset = func->address + offset;
415 /******************************************************************
416 * symt_add_func_local
418 * Adds a new local/parameter to a given function:
419 * In any cases, dt tells whether it's a local variable or a parameter
420 * If regno it's not 0:
421 * - then variable is stored in a register
422 * - otherwise, value is referenced by register + offset
423 * Otherwise, the variable is stored on the stack:
424 * - offset is then the offset from the frame register
426 struct symt_data* symt_add_func_local(struct module* module,
427 struct symt_function* func,
428 enum DataKind dt,
429 const struct location* loc,
430 struct symt_block* block,
431 struct symt* type, const char* name)
433 struct symt_data* locsym;
434 struct symt** p;
436 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
437 debugstr_w(module->module.ModuleName), func->hash_elt.name,
438 name, type);
440 assert(func);
441 assert(func->symt.tag == SymTagFunction);
442 assert(dt == DataIsParam || dt == DataIsLocal);
444 locsym = pool_alloc(&module->pool, sizeof(*locsym));
445 locsym->symt.tag = SymTagData;
446 locsym->hash_elt.name = pool_strdup(&module->pool, name);
447 locsym->hash_elt.next = NULL;
448 locsym->kind = dt;
449 locsym->container = &block->symt;
450 locsym->type = type;
451 locsym->u.var = *loc;
452 if (block)
453 p = vector_add(&block->vchildren, &module->pool);
454 else
455 p = vector_add(&func->vchildren, &module->pool);
456 *p = &locsym->symt;
457 return locsym;
461 struct symt_block* symt_open_func_block(struct module* module,
462 struct symt_function* func,
463 struct symt_block* parent_block,
464 unsigned pc, unsigned len)
466 struct symt_block* block;
467 struct symt** p;
469 assert(func);
470 assert(func->symt.tag == SymTagFunction);
472 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
473 block = pool_alloc(&module->pool, sizeof(*block));
474 block->symt.tag = SymTagBlock;
475 block->address = func->address + pc;
476 block->size = len;
477 block->container = parent_block ? &parent_block->symt : &func->symt;
478 vector_init(&block->vchildren, sizeof(struct symt*), 4);
479 if (parent_block)
480 p = vector_add(&parent_block->vchildren, &module->pool);
481 else
482 p = vector_add(&func->vchildren, &module->pool);
483 *p = &block->symt;
485 return block;
488 struct symt_block* symt_close_func_block(struct module* module,
489 const struct symt_function* func,
490 struct symt_block* block, unsigned pc)
492 assert(func);
493 assert(func->symt.tag == SymTagFunction);
495 if (pc) block->size = func->address + pc - block->address;
496 return (block->container->tag == SymTagBlock) ?
497 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
500 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
501 struct symt_function* func,
502 enum SymTagEnum point,
503 const struct location* loc,
504 const char* name)
506 struct symt_hierarchy_point*sym;
507 struct symt** p;
509 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
511 sym->symt.tag = point;
512 sym->parent = &func->symt;
513 sym->loc = *loc;
514 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
515 p = vector_add(&func->vchildren, &module->pool);
516 *p = &sym->symt;
518 return sym;
521 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
523 unsigned len;
524 struct line_info* dli;
526 assert(func);
527 /* We aren't adding any more locals or line numbers to this function.
528 * Free any spare memory that we might have allocated.
530 assert(func->symt.tag == SymTagFunction);
532 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
533 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
535 len = vector_length(&func->vlines);
536 if (len--)
538 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
539 dli = vector_at(&func->vlines, len); dli->is_last = 1;
541 return TRUE;
544 struct symt_thunk* symt_new_thunk(struct module* module,
545 struct symt_compiland* compiland,
546 const char* name, THUNK_ORDINAL ord,
547 unsigned long addr, unsigned long size)
549 struct symt_thunk* sym;
551 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
552 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
554 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
556 sym->symt.tag = SymTagThunk;
557 sym->hash_elt.name = pool_strdup(&module->pool, name);
558 sym->container = &compiland->symt;
559 sym->address = addr;
560 sym->size = size;
561 sym->ordinal = ord;
562 symt_add_module_ht(module, (struct symt_ht*)sym);
563 if (compiland)
565 struct symt** p;
566 p = vector_add(&compiland->vchildren, &module->pool);
567 *p = &sym->symt;
570 return sym;
573 struct symt_data* symt_new_constant(struct module* module,
574 struct symt_compiland* compiland,
575 const char* name, struct symt* type,
576 const VARIANT* v)
578 struct symt_data* sym;
580 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
581 debugstr_w(module->module.ModuleName), name);
583 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
585 sym->symt.tag = SymTagData;
586 sym->hash_elt.name = pool_strdup(&module->pool, name);
587 sym->kind = DataIsConstant;
588 sym->container = compiland ? &compiland->symt : NULL;
589 sym->type = type;
590 sym->u.value = *v;
591 symt_add_module_ht(module, (struct symt_ht*)sym);
592 if (compiland)
594 struct symt** p;
595 p = vector_add(&compiland->vchildren, &module->pool);
596 *p = &sym->symt;
599 return sym;
602 struct symt_hierarchy_point* symt_new_label(struct module* module,
603 struct symt_compiland* compiland,
604 const char* name, unsigned long address)
606 struct symt_hierarchy_point* sym;
608 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
609 debugstr_w(module->module.ModuleName), name);
611 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
613 sym->symt.tag = SymTagLabel;
614 sym->hash_elt.name = pool_strdup(&module->pool, name);
615 sym->loc.kind = loc_absolute;
616 sym->loc.offset = address;
617 sym->parent = compiland ? &compiland->symt : NULL;
618 symt_add_module_ht(module, (struct symt_ht*)sym);
619 if (compiland)
621 struct symt** p;
622 p = vector_add(&compiland->vchildren, &module->pool);
623 *p = &sym->symt;
626 return sym;
629 /* expect sym_info->MaxNameLen to be set before being called */
630 static void symt_fill_sym_info(const struct module_pair* pair,
631 const struct symt_function* func,
632 const struct symt* sym, SYMBOL_INFO* sym_info)
634 const char* name;
635 DWORD64 size;
637 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
638 sym_info->TypeIndex = 0;
639 sym_info->info = (DWORD)sym;
640 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
641 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
642 (!sym_info->TypeIndex ||
643 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
644 size = 0;
645 sym_info->Size = (DWORD)size;
646 sym_info->ModBase = pair->requested->module.BaseOfImage;
647 sym_info->Flags = 0;
648 sym_info->Value = 0;
650 switch (sym->tag)
652 case SymTagData:
654 const struct symt_data* data = (const struct symt_data*)sym;
655 switch (data->kind)
657 case DataIsParam:
658 sym_info->Flags |= SYMFLAG_PARAMETER;
659 /* fall through */
660 case DataIsLocal:
662 struct location loc = data->u.var;
664 if (loc.kind >= loc_user)
665 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
667 switch (loc.kind)
669 case loc_error:
670 /* for now we report error cases as a negative register number */
671 sym_info->Flags |= SYMFLAG_LOCAL;
672 /* fall through */
673 case loc_register:
674 sym_info->Flags |= SYMFLAG_REGISTER;
675 sym_info->Register = loc.reg;
676 sym_info->Address = 0;
677 break;
678 case loc_regrel:
679 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
680 /* FIXME: it's i386 dependent !!! */
681 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
682 sym_info->Address = loc.offset;
683 break;
684 default:
685 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
686 assert(0);
689 break;
690 case DataIsGlobal:
691 case DataIsFileStatic:
692 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
693 sym_info->Register = 0;
694 break;
695 case DataIsConstant:
696 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
697 switch (data->u.value.n1.n2.vt)
699 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
700 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
701 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
702 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
703 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
704 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
705 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
706 case VT_EMPTY: sym_info->Value = 0; break;
707 default:
708 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
709 sym_info->Value = 0;
710 break;
712 break;
713 default:
714 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
717 break;
718 case SymTagPublicSymbol:
719 sym_info->Flags |= SYMFLAG_EXPORT;
720 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
721 break;
722 case SymTagFunction:
723 sym_info->Flags |= SYMFLAG_FUNCTION;
724 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
725 break;
726 case SymTagThunk:
727 sym_info->Flags |= SYMFLAG_THUNK;
728 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
729 break;
730 default:
731 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
732 sym_info->Register = 0;
733 break;
735 sym_info->Scope = 0; /* FIXME */
736 sym_info->Tag = sym->tag;
737 name = symt_get_name(sym);
738 if (sym_info->MaxNameLen)
740 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
741 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
742 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
744 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
745 memcpy(sym_info->Name, name, sym_info->NameLen);
746 sym_info->Name[sym_info->NameLen] = '\0';
749 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
750 sym, sym_info->Name, sym_info->Size,
751 wine_dbgstr_longlong(sym_info->Address));
754 struct sym_enum
756 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
757 PVOID user;
758 SYMBOL_INFO* sym_info;
759 DWORD index;
760 DWORD tag;
761 DWORD64 addr;
762 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
765 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
766 const struct symt_function* func, const struct symt* sym)
768 symt_fill_sym_info(pair, func, sym, se->sym_info);
769 if (se->index && se->sym_info->info != se->index) return FALSE;
770 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
771 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
772 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
775 static BOOL symt_enum_module(const struct module_pair* pair, const regex_t* regex,
776 const struct sym_enum* se)
778 void* ptr;
779 struct symt_ht* sym = NULL;
780 struct hash_table_iter hti;
782 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
783 while ((ptr = hash_table_iter_up(&hti)))
785 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
786 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
788 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
789 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
790 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
793 return FALSE;
796 static inline unsigned where_to_insert(const struct module* module, unsigned high, const struct symt_ht* elt)
798 unsigned low = 0, mid = high / 2;
799 ULONG64 addr;
801 if (!high) return 0;
802 symt_get_info(&elt->symt, TI_GET_ADDRESS, &addr);
805 switch (cmp_sorttab_addr(module, mid, addr))
807 case 0: return mid;
808 case -1: low = mid + 1; break;
809 case 1: high = mid; break;
811 mid = low + (high - low) / 2;
812 } while (low < high);
813 return mid;
816 /***********************************************************************
817 * resort_symbols
819 * Rebuild sorted list of symbols for a module.
821 static BOOL resort_symbols(struct module* module)
823 if (!(module->module.NumSyms = module->num_symbols))
824 return FALSE;
826 /* FIXME: what's the optimal value here ??? */
827 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
829 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
830 struct symt_ht* tmp[30];
832 delta = module->num_symbols - module->num_sorttab;
833 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
834 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
836 for (i = delta - 1; i >= 0; i--)
838 prev_ins_idx = ins_idx;
839 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
840 memmove(&module->addr_sorttab[ins_idx + i + 1],
841 &module->addr_sorttab[ins_idx],
842 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
843 module->addr_sorttab[ins_idx + i] = tmp[i];
846 else
847 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
848 module->num_sorttab = module->num_symbols;
849 return module->sortlist_valid = TRUE;
852 static void symt_get_length(const struct symt* symt, ULONG64* size)
854 DWORD type_index;
856 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
857 return;
859 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
860 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
861 *size = 0x1000; /* arbitrary value */
864 /* assume addr is in module */
865 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
867 int mid, high, low;
868 ULONG64 ref_addr, ref_size;
870 if (!module->sortlist_valid || !module->addr_sorttab)
872 if (!resort_symbols(module)) return NULL;
876 * Binary search to find closest symbol.
878 low = 0;
879 high = module->num_sorttab;
881 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
882 if (addr < ref_addr) return NULL;
883 if (high)
885 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
886 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
887 if (addr >= ref_addr + ref_size) return NULL;
890 while (high > low + 1)
892 mid = (high + low) / 2;
893 if (cmp_sorttab_addr(module, mid, addr) < 0)
894 low = mid;
895 else
896 high = mid;
898 if (low != high && high != module->num_sorttab &&
899 cmp_sorttab_addr(module, high, addr) <= 0)
900 low = high;
902 /* If found symbol is a public symbol, check if there are any other entries that
903 * might also have the same address, but would get better information
905 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
907 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
908 if (low > 0 &&
909 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
910 !cmp_sorttab_addr(module, low - 1, ref_addr))
911 low--;
912 else if (low < module->num_sorttab - 1 &&
913 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
914 !cmp_sorttab_addr(module, low + 1, ref_addr))
915 low++;
917 /* finally check that we fit into the found symbol */
918 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
919 if (addr < ref_addr) return NULL;
920 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
921 if (addr >= ref_addr + ref_size) return NULL;
923 return module->addr_sorttab[low];
926 static BOOL symt_enum_locals_helper(struct module_pair* pair,
927 regex_t* preg, const struct sym_enum* se,
928 struct symt_function* func, const struct vector* v)
930 struct symt* lsym = NULL;
931 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
932 unsigned int i;
934 for (i=0; i<vector_length(v); i++)
936 lsym = *(struct symt**)vector_at(v, i);
937 switch (lsym->tag)
939 case SymTagBlock:
941 struct symt_block* block = (struct symt_block*)lsym;
942 if (pc < block->address || block->address + block->size <= pc)
943 continue;
944 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
945 return FALSE;
947 break;
948 case SymTagData:
949 if (match_regexp(preg, symt_get_name(lsym)))
951 if (send_symbol(se, pair, func, lsym)) return FALSE;
953 break;
954 case SymTagLabel:
955 case SymTagFuncDebugStart:
956 case SymTagFuncDebugEnd:
957 case SymTagCustom:
958 break;
959 default:
960 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
961 assert(0);
964 return TRUE;
967 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
968 const struct sym_enum* se)
970 struct module_pair pair;
971 struct symt_ht* sym;
972 DWORD pc = pcs->ctx_frame.InstructionOffset;
974 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
975 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
977 pair.pcs = pcs;
978 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
979 if (!module_get_debug(&pair)) return FALSE;
980 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
982 if (sym->symt.tag == SymTagFunction)
984 BOOL ret;
985 regex_t preg;
987 compile_regex(mask ? mask : "*", -1, &preg,
988 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
989 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
990 &((struct symt_function*)sym)->vchildren);
991 regfree(&preg);
992 return ret;
995 return send_symbol(se, &pair, NULL, &sym->symt);
998 /******************************************************************
999 * copy_symbolW
1001 * Helper for transforming an ANSI symbol info into a UNICODE one.
1002 * Assume that MaxNameLen is the same for both version (A & W).
1004 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1006 siw->SizeOfStruct = si->SizeOfStruct;
1007 siw->TypeIndex = si->TypeIndex;
1008 siw->Reserved[0] = si->Reserved[0];
1009 siw->Reserved[1] = si->Reserved[1];
1010 siw->Index = si->info; /* FIXME: see dbghelp.h */
1011 siw->Size = si->Size;
1012 siw->ModBase = si->ModBase;
1013 siw->Flags = si->Flags;
1014 siw->Value = si->Value;
1015 siw->Address = si->Address;
1016 siw->Register = si->Register;
1017 siw->Scope = si->Scope;
1018 siw->Tag = si->Tag;
1019 siw->NameLen = si->NameLen;
1020 siw->MaxNameLen = si->MaxNameLen;
1021 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1024 /******************************************************************
1025 * sym_enum
1027 * Core routine for most of the enumeration of symbols
1029 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1030 const struct sym_enum* se)
1032 struct module_pair pair;
1033 const char* bang;
1034 regex_t mod_regex, sym_regex;
1036 pair.pcs = process_find_by_handle(hProcess);
1037 if (BaseOfDll == 0)
1039 /* do local variables ? */
1040 if (!Mask || !(bang = strchr(Mask, '!')))
1041 return symt_enum_locals(pair.pcs, Mask, se);
1043 if (bang == Mask) return FALSE;
1045 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1046 compile_regex(bang + 1, -1, &sym_regex,
1047 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1049 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1051 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1053 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1054 symt_enum_module(&pair, &sym_regex, se))
1055 break;
1058 /* not found in PE modules, retry on the ELF ones
1060 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1062 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1064 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1065 !module_get_containee(pair.pcs, pair.requested) &&
1066 module_get_debug(&pair))
1068 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1069 symt_enum_module(&pair, &sym_regex, se))
1070 break;
1074 regfree(&mod_regex);
1075 regfree(&sym_regex);
1076 return TRUE;
1078 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1079 if (!module_get_debug(&pair))
1080 return FALSE;
1082 /* we always ignore module name from Mask when BaseOfDll is defined */
1083 if (Mask && (bang = strchr(Mask, '!')))
1085 if (bang == Mask) return FALSE;
1086 Mask = bang + 1;
1089 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1090 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1091 symt_enum_module(&pair, &sym_regex, se);
1092 regfree(&sym_regex);
1094 return TRUE;
1097 /******************************************************************
1098 * SymEnumSymbols (DBGHELP.@)
1100 * cases BaseOfDll = 0
1101 * !foo fails always (despite what MSDN states)
1102 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1103 * no ! in Mask, lookup in local Context
1104 * cases BaseOfDll != 0
1105 * !foo fails always (despite what MSDN states)
1106 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1108 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1109 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1110 PVOID UserContext)
1112 struct sym_enum se;
1114 TRACE("(%p %s %s %p %p)\n",
1115 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1116 EnumSymbolsCallback, UserContext);
1118 se.cb = EnumSymbolsCallback;
1119 se.user = UserContext;
1120 se.index = 0;
1121 se.tag = 0;
1122 se.addr = 0;
1123 se.sym_info = (PSYMBOL_INFO)se.buffer;
1125 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1128 struct sym_enumW
1130 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1131 void* ctx;
1132 PSYMBOL_INFOW sym_info;
1133 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1137 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1139 struct sym_enumW* sew = ctx;
1141 copy_symbolW(sew->sym_info, si);
1143 return (sew->cb)(sew->sym_info, size, sew->ctx);
1146 /******************************************************************
1147 * SymEnumSymbolsW (DBGHELP.@)
1150 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1151 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1152 PVOID UserContext)
1154 struct sym_enumW sew;
1155 BOOL ret = FALSE;
1156 char* maskA = NULL;
1158 sew.ctx = UserContext;
1159 sew.cb = EnumSymbolsCallback;
1160 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1162 if (Mask)
1164 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1165 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1166 if (!maskA) return FALSE;
1167 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1169 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1170 HeapFree(GetProcessHeap(), 0, maskA);
1172 return ret;
1175 struct sym_enumerate
1177 void* ctx;
1178 PSYM_ENUMSYMBOLS_CALLBACK cb;
1181 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1183 struct sym_enumerate* se = ctx;
1184 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1187 /***********************************************************************
1188 * SymEnumerateSymbols (DBGHELP.@)
1190 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1191 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1192 PVOID UserContext)
1194 struct sym_enumerate se;
1196 se.ctx = UserContext;
1197 se.cb = EnumSymbolsCallback;
1199 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1202 struct sym_enumerate64
1204 void* ctx;
1205 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1208 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1210 struct sym_enumerate64* se = ctx;
1211 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1214 /***********************************************************************
1215 * SymEnumerateSymbols64 (DBGHELP.@)
1217 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1218 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1219 PVOID UserContext)
1221 struct sym_enumerate64 se;
1223 se.ctx = UserContext;
1224 se.cb = EnumSymbolsCallback;
1226 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1229 /******************************************************************
1230 * SymFromAddr (DBGHELP.@)
1233 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1234 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1236 struct module_pair pair;
1237 struct symt_ht* sym;
1239 pair.pcs = process_find_by_handle(hProcess);
1240 if (!pair.pcs) return FALSE;
1241 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1242 if (!module_get_debug(&pair)) return FALSE;
1243 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1245 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1246 *Displacement = Address - Symbol->Address;
1247 return TRUE;
1250 /******************************************************************
1251 * SymFromAddrW (DBGHELP.@)
1254 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1255 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1257 PSYMBOL_INFO si;
1258 unsigned len;
1259 BOOL ret;
1261 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1262 si = HeapAlloc(GetProcessHeap(), 0, len);
1263 if (!si) return FALSE;
1265 si->SizeOfStruct = sizeof(*si);
1266 si->MaxNameLen = Symbol->MaxNameLen;
1267 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1269 copy_symbolW(Symbol, si);
1271 HeapFree(GetProcessHeap(), 0, si);
1272 return ret;
1275 /******************************************************************
1276 * SymGetSymFromAddr (DBGHELP.@)
1279 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1280 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1282 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1283 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1284 size_t len;
1285 DWORD64 Displacement64;
1287 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1288 si->SizeOfStruct = sizeof(*si);
1289 si->MaxNameLen = MAX_SYM_NAME;
1290 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1291 return FALSE;
1293 if (Displacement)
1294 *Displacement = Displacement64;
1295 Symbol->Address = si->Address;
1296 Symbol->Size = si->Size;
1297 Symbol->Flags = si->Flags;
1298 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1299 lstrcpynA(Symbol->Name, si->Name, len);
1300 return TRUE;
1303 /******************************************************************
1304 * SymGetSymFromAddr64 (DBGHELP.@)
1307 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1308 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1310 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1311 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1312 size_t len;
1313 DWORD64 Displacement64;
1315 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1316 si->SizeOfStruct = sizeof(*si);
1317 si->MaxNameLen = MAX_SYM_NAME;
1318 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1319 return FALSE;
1321 if (Displacement)
1322 *Displacement = Displacement64;
1323 Symbol->Address = si->Address;
1324 Symbol->Size = si->Size;
1325 Symbol->Flags = si->Flags;
1326 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1327 lstrcpynA(Symbol->Name, si->Name, len);
1328 return TRUE;
1331 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1332 SYMBOL_INFO* symbol)
1334 struct hash_table_iter hti;
1335 void* ptr;
1336 struct symt_ht* sym = NULL;
1337 struct module_pair pair;
1339 pair.pcs = pcs;
1340 if (!(pair.requested = module)) return FALSE;
1341 if (!module_get_debug(&pair)) return FALSE;
1343 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1344 while ((ptr = hash_table_iter_up(&hti)))
1346 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1348 if (!strcmp(sym->hash_elt.name, name))
1350 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1351 return TRUE;
1354 return FALSE;
1357 /******************************************************************
1358 * SymFromName (DBGHELP.@)
1361 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1363 struct process* pcs = process_find_by_handle(hProcess);
1364 struct module* module;
1365 const char* name;
1367 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1368 if (!pcs) return FALSE;
1369 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1370 name = strchr(Name, '!');
1371 if (name)
1373 char tmp[128];
1374 assert(name - Name < sizeof(tmp));
1375 memcpy(tmp, Name, name - Name);
1376 tmp[name - Name] = '\0';
1377 module = module_find_by_nameA(pcs, tmp);
1378 return find_name(pcs, module, name + 1, Symbol);
1380 for (module = pcs->lmodules; module; module = module->next)
1382 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1383 return TRUE;
1385 /* not found in PE modules, retry on the ELF ones
1387 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1389 for (module = pcs->lmodules; module; module = module->next)
1391 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1392 !module_get_containee(pcs, module) &&
1393 find_name(pcs, module, Name, Symbol))
1394 return TRUE;
1397 return FALSE;
1400 /***********************************************************************
1401 * SymGetSymFromName64 (DBGHELP.@)
1403 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1405 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1406 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1407 size_t len;
1409 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1410 si->SizeOfStruct = sizeof(*si);
1411 si->MaxNameLen = MAX_SYM_NAME;
1412 if (!SymFromName(hProcess, Name, si)) return FALSE;
1414 Symbol->Address = si->Address;
1415 Symbol->Size = si->Size;
1416 Symbol->Flags = si->Flags;
1417 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1418 lstrcpynA(Symbol->Name, si->Name, len);
1419 return TRUE;
1422 /***********************************************************************
1423 * SymGetSymFromName (DBGHELP.@)
1425 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1427 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1428 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1429 size_t len;
1431 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1432 si->SizeOfStruct = sizeof(*si);
1433 si->MaxNameLen = MAX_SYM_NAME;
1434 if (!SymFromName(hProcess, Name, si)) return FALSE;
1436 Symbol->Address = si->Address;
1437 Symbol->Size = si->Size;
1438 Symbol->Flags = si->Flags;
1439 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1440 lstrcpynA(Symbol->Name, si->Name, len);
1441 return TRUE;
1444 /******************************************************************
1445 * sym_fill_func_line_info
1447 * fills information about a file
1449 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1450 DWORD addr, IMAGEHLP_LINE* line)
1452 struct line_info* dli = NULL;
1453 BOOL found = FALSE;
1454 int i;
1456 assert(func->symt.tag == SymTagFunction);
1458 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1460 dli = vector_at(&func->vlines, i);
1461 if (!dli->is_source_file)
1463 if (found || dli->u.pc_offset > addr) continue;
1464 line->LineNumber = dli->line_number;
1465 line->Address = dli->u.pc_offset;
1466 line->Key = dli;
1467 found = TRUE;
1468 continue;
1470 if (found)
1472 line->FileName = (char*)source_get(module, dli->u.source_file);
1473 return TRUE;
1476 return FALSE;
1479 /***********************************************************************
1480 * SymGetSymNext64 (DBGHELP.@)
1482 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1484 /* algo:
1485 * get module from Symbol.Address
1486 * get index in module.addr_sorttab of Symbol.Address
1487 * increment index
1488 * if out of module bounds, move to next module in process address space
1490 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1491 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1492 return FALSE;
1495 /***********************************************************************
1496 * SymGetSymNext (DBGHELP.@)
1498 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1500 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1501 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1502 return FALSE;
1505 /***********************************************************************
1506 * SymGetSymPrev64 (DBGHELP.@)
1508 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1510 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1511 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1512 return FALSE;
1515 /***********************************************************************
1516 * SymGetSymPrev (DBGHELP.@)
1518 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1520 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1521 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1522 return FALSE;
1525 /******************************************************************
1526 * SymGetLineFromAddr (DBGHELP.@)
1529 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1530 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1532 struct module_pair pair;
1533 struct symt_ht* symt;
1535 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1537 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1539 pair.pcs = process_find_by_handle(hProcess);
1540 if (!pair.pcs) return FALSE;
1541 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1542 if (!module_get_debug(&pair)) return FALSE;
1543 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1545 if (symt->symt.tag != SymTagFunction) return FALSE;
1546 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1547 dwAddr, Line)) return FALSE;
1548 *pdwDisplacement = dwAddr - Line->Address;
1549 return TRUE;
1552 /******************************************************************
1553 * copy_line_64_from_32 (internal)
1556 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1559 l64->Key = l32->Key;
1560 l64->LineNumber = l32->LineNumber;
1561 l64->FileName = l32->FileName;
1562 l64->Address = l32->Address;
1565 /******************************************************************
1566 * copy_line_W64_from_32 (internal)
1569 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1571 unsigned len;
1573 l64->Key = l32->Key;
1574 l64->LineNumber = l32->LineNumber;
1575 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1576 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1577 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1578 l64->Address = l32->Address;
1581 /******************************************************************
1582 * copy_line_32_from_64 (internal)
1585 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1588 l32->Key = l64->Key;
1589 l32->LineNumber = l64->LineNumber;
1590 l32->FileName = l64->FileName;
1591 l32->Address = l64->Address;
1594 /******************************************************************
1595 * SymGetLineFromAddr64 (DBGHELP.@)
1598 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1599 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1601 IMAGEHLP_LINE line32;
1603 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1604 if (!validate_addr64(dwAddr)) return FALSE;
1605 line32.SizeOfStruct = sizeof(line32);
1606 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1607 return FALSE;
1608 copy_line_64_from_32(Line, &line32);
1609 return TRUE;
1612 /******************************************************************
1613 * SymGetLineFromAddrW64 (DBGHELP.@)
1616 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1617 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1619 struct process* pcs = process_find_by_handle(hProcess);
1620 IMAGEHLP_LINE line32;
1622 if (!pcs) return FALSE;
1623 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1624 if (!validate_addr64(dwAddr)) return FALSE;
1625 line32.SizeOfStruct = sizeof(line32);
1626 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1627 return FALSE;
1628 copy_line_W64_from_32(pcs, Line, &line32);
1629 return TRUE;
1632 /******************************************************************
1633 * SymGetLinePrev (DBGHELP.@)
1636 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1638 struct module_pair pair;
1639 struct line_info* li;
1640 BOOL in_search = FALSE;
1642 TRACE("(%p %p)\n", hProcess, Line);
1644 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1646 pair.pcs = process_find_by_handle(hProcess);
1647 if (!pair.pcs) return FALSE;
1648 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1649 if (!module_get_debug(&pair)) return FALSE;
1651 if (Line->Key == 0) return FALSE;
1652 li = Line->Key;
1653 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1654 * element we have to go back until we find the prev one to get the real
1655 * source file name for the DLIT_OFFSET element just before
1656 * the first DLIT_SOURCEFILE
1658 while (!li->is_first)
1660 li--;
1661 if (!li->is_source_file)
1663 Line->LineNumber = li->line_number;
1664 Line->Address = li->u.pc_offset;
1665 Line->Key = li;
1666 if (!in_search) return TRUE;
1668 else
1670 if (in_search)
1672 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1673 return TRUE;
1675 in_search = TRUE;
1678 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1679 return FALSE;
1682 /******************************************************************
1683 * SymGetLinePrev64 (DBGHELP.@)
1686 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1688 IMAGEHLP_LINE line32;
1690 line32.SizeOfStruct = sizeof(line32);
1691 copy_line_32_from_64(&line32, Line);
1692 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1693 copy_line_64_from_32(Line, &line32);
1694 return TRUE;
1697 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1699 struct line_info* li;
1701 if (line->Key == 0) return FALSE;
1702 li = line->Key;
1703 while (!li->is_last)
1705 li++;
1706 if (!li->is_source_file)
1708 line->LineNumber = li->line_number;
1709 line->Address = li->u.pc_offset;
1710 line->Key = li;
1711 return TRUE;
1713 line->FileName = (char*)source_get(module, li->u.source_file);
1715 return FALSE;
1718 /******************************************************************
1719 * SymGetLineNext (DBGHELP.@)
1722 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1724 struct module_pair pair;
1726 TRACE("(%p %p)\n", hProcess, Line);
1728 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1729 pair.pcs = process_find_by_handle(hProcess);
1730 if (!pair.pcs) return FALSE;
1731 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1732 if (!module_get_debug(&pair)) return FALSE;
1734 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1735 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1736 return FALSE;
1739 /******************************************************************
1740 * SymGetLineNext64 (DBGHELP.@)
1743 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1745 IMAGEHLP_LINE line32;
1747 line32.SizeOfStruct = sizeof(line32);
1748 copy_line_32_from_64(&line32, Line);
1749 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1750 copy_line_64_from_32(Line, &line32);
1751 return TRUE;
1754 /***********************************************************************
1755 * SymFunctionTableAccess (DBGHELP.@)
1757 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1759 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1760 return NULL;
1763 /***********************************************************************
1764 * SymFunctionTableAccess64 (DBGHELP.@)
1766 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1768 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1769 return NULL;
1772 /***********************************************************************
1773 * SymUnDName (DBGHELP.@)
1775 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1777 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1778 UNDNAME_COMPLETE) != 0;
1781 /***********************************************************************
1782 * SymUnDName64 (DBGHELP.@)
1784 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1786 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1787 UNDNAME_COMPLETE) != 0;
1790 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1791 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1793 /***********************************************************************
1794 * UnDecorateSymbolName (DBGHELP.@)
1796 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1797 DWORD UndecoratedLength, DWORD Flags)
1799 /* undocumented from msvcrt */
1800 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1801 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1803 TRACE("(%s, %p, %d, 0x%08x)\n",
1804 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1806 if (!p_undname)
1808 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1809 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1810 if (!p_undname) return 0;
1813 if (!UnDecoratedName) return 0;
1814 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1815 und_alloc, und_free, Flags))
1816 return 0;
1817 return strlen(UnDecoratedName);
1820 /******************************************************************
1821 * SymMatchString (DBGHELP.@)
1824 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1826 regex_t preg;
1827 BOOL ret;
1829 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1831 compile_regex(re, -1, &preg, _case);
1832 ret = match_regexp(&preg, string);
1833 regfree(&preg);
1834 return ret;
1837 /******************************************************************
1838 * SymSearch (DBGHELP.@)
1840 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1841 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1842 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1843 PVOID UserContext, DWORD Options)
1845 struct sym_enum se;
1847 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1848 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1849 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1850 UserContext, Options);
1852 if (Options != SYMSEARCH_GLOBALSONLY)
1854 FIXME("Unsupported searching with options (%x)\n", Options);
1855 SetLastError(ERROR_INVALID_PARAMETER);
1856 return FALSE;
1859 se.cb = EnumSymbolsCallback;
1860 se.user = UserContext;
1861 se.index = Index;
1862 se.tag = SymTag;
1863 se.addr = Address;
1864 se.sym_info = (PSYMBOL_INFO)se.buffer;
1866 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1869 /******************************************************************
1870 * SymSearchW (DBGHELP.@)
1872 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1873 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1874 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1875 PVOID UserContext, DWORD Options)
1877 struct sym_enumW sew;
1878 BOOL ret = FALSE;
1879 char* maskA = NULL;
1881 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1882 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1883 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1884 UserContext, Options);
1886 sew.ctx = UserContext;
1887 sew.cb = EnumSymbolsCallback;
1888 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1890 if (Mask)
1892 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1893 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1894 if (!maskA) return FALSE;
1895 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1897 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1898 sym_enumW, &sew, Options);
1899 HeapFree(GetProcessHeap(), 0, maskA);
1901 return ret;
1904 /******************************************************************
1905 * SymAddSymbol (DBGHELP.@)
1908 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1909 DWORD64 addr, DWORD size, DWORD flags)
1911 WCHAR nameW[MAX_SYM_NAME];
1913 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1914 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1917 /******************************************************************
1918 * SymAddSymbolW (DBGHELP.@)
1921 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1922 DWORD64 addr, DWORD size, DWORD flags)
1924 struct module_pair pair;
1926 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1928 pair.pcs = process_find_by_handle(hProcess);
1929 if (!pair.pcs) return FALSE;
1930 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1931 if (!module_get_debug(&pair)) return FALSE;
1933 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1934 return FALSE;
1937 /******************************************************************
1938 * SymSetScopeFromAddr (DBGHELP.@)
1940 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1942 struct process* pcs;
1944 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1946 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1947 return TRUE;
1950 /******************************************************************
1951 * SymEnumLines (DBGHELP.@)
1954 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1955 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1957 struct module_pair pair;
1958 struct hash_table_iter hti;
1959 struct symt_ht* sym;
1960 regex_t re;
1961 struct line_info* dli;
1962 void* ptr;
1963 SRCCODEINFO sci;
1964 const char* file;
1966 if (!cb) return FALSE;
1967 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1969 pair.pcs = process_find_by_handle(hProcess);
1970 if (!pair.pcs) return FALSE;
1971 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1972 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1973 if (!module_get_debug(&pair)) return FALSE;
1974 if (!compile_file_regex(&re, srcfile)) return FALSE;
1976 sci.SizeOfStruct = sizeof(sci);
1977 sci.ModBase = base;
1979 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1980 while ((ptr = hash_table_iter_up(&hti)))
1982 unsigned int i;
1984 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1985 if (sym->symt.tag != SymTagFunction) continue;
1987 sci.FileName[0] = '\0';
1988 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1990 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1991 if (dli->is_source_file)
1993 file = source_get(pair.effective, dli->u.source_file);
1994 if (!match_regexp(&re, file)) file = "";
1995 strcpy(sci.FileName, file);
1997 else if (sci.FileName[0])
1999 sci.Key = dli;
2000 sci.Obj[0] = '\0'; /* FIXME */
2001 sci.LineNumber = dli->line_number;
2002 sci.Address = dli->u.pc_offset;
2003 if (!cb(&sci, user)) break;
2007 regfree(&re);
2008 return TRUE;