winex11: Introduce a new function for looking up the physical color of a pixel for...
[wine/multimedia.git] / dlls / dbghelp / symbol.c
blob34d00b2a0212fe78bc92ed959892f1483e5e0b98
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 default:
707 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
708 sym_info->Value = 0;
709 break;
711 break;
712 default:
713 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
716 break;
717 case SymTagPublicSymbol:
718 sym_info->Flags |= SYMFLAG_EXPORT;
719 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
720 break;
721 case SymTagFunction:
722 sym_info->Flags |= SYMFLAG_FUNCTION;
723 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
724 break;
725 case SymTagThunk:
726 sym_info->Flags |= SYMFLAG_THUNK;
727 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
728 break;
729 default:
730 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
731 sym_info->Register = 0;
732 break;
734 sym_info->Scope = 0; /* FIXME */
735 sym_info->Tag = sym->tag;
736 name = symt_get_name(sym);
737 if (sym_info->MaxNameLen)
739 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
740 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
741 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
743 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
744 memcpy(sym_info->Name, name, sym_info->NameLen);
745 sym_info->Name[sym_info->NameLen] = '\0';
748 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
749 sym, sym_info->Name, sym_info->Size,
750 wine_dbgstr_longlong(sym_info->Address));
753 struct sym_enum
755 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
756 PVOID user;
757 SYMBOL_INFO* sym_info;
758 DWORD index;
759 DWORD tag;
760 DWORD64 addr;
761 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
764 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
765 const struct symt_function* func, const struct symt* sym)
767 symt_fill_sym_info(pair, func, sym, se->sym_info);
768 if (se->index && se->sym_info->info != se->index) return FALSE;
769 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
770 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
771 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
774 static BOOL symt_enum_module(const struct module_pair* pair, const regex_t* regex,
775 const struct sym_enum* se)
777 void* ptr;
778 struct symt_ht* sym = NULL;
779 struct hash_table_iter hti;
781 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
782 while ((ptr = hash_table_iter_up(&hti)))
784 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
785 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
787 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
788 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
789 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
792 return FALSE;
795 static inline unsigned where_to_insert(const struct module* module, unsigned high, const struct symt_ht* elt)
797 unsigned low = 0, mid = high / 2;
798 ULONG64 addr;
800 if (!high) return 0;
801 symt_get_info(&elt->symt, TI_GET_ADDRESS, &addr);
804 switch (cmp_sorttab_addr(module, mid, addr))
806 case 0: return mid;
807 case -1: low = mid + 1; break;
808 case 1: high = mid; break;
810 mid = low + (high - low) / 2;
811 } while (low < high);
812 return mid;
815 /***********************************************************************
816 * resort_symbols
818 * Rebuild sorted list of symbols for a module.
820 static BOOL resort_symbols(struct module* module)
822 if (!(module->module.NumSyms = module->num_symbols))
823 return FALSE;
825 /* FIXME: what's the optimal value here ??? */
826 if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
828 int i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
829 struct symt_ht* tmp[30];
831 delta = module->num_symbols - module->num_sorttab;
832 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
833 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
835 for (i = delta - 1; i >= 0; i--)
837 prev_ins_idx = ins_idx;
838 ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
839 memmove(&module->addr_sorttab[ins_idx + i + 1],
840 &module->addr_sorttab[ins_idx],
841 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
842 module->addr_sorttab[ins_idx + i] = tmp[i];
845 else
846 qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
847 module->num_sorttab = module->num_symbols;
848 return module->sortlist_valid = TRUE;
851 static void symt_get_length(const struct symt* symt, ULONG64* size)
853 DWORD type_index;
855 if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
856 return;
858 if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
859 symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
860 *size = 0x1000; /* arbitrary value */
863 /* assume addr is in module */
864 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
866 int mid, high, low;
867 ULONG64 ref_addr, ref_size;
869 if (!module->sortlist_valid || !module->addr_sorttab)
871 if (!resort_symbols(module)) return NULL;
875 * Binary search to find closest symbol.
877 low = 0;
878 high = module->num_sorttab;
880 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
881 if (addr < ref_addr) return NULL;
882 if (high)
884 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
885 symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
886 if (addr >= ref_addr + ref_size) return NULL;
889 while (high > low + 1)
891 mid = (high + low) / 2;
892 if (cmp_sorttab_addr(module, mid, addr) < 0)
893 low = mid;
894 else
895 high = mid;
897 if (low != high && high != module->num_sorttab &&
898 cmp_sorttab_addr(module, high, addr) <= 0)
899 low = high;
901 /* If found symbol is a public symbol, check if there are any other entries that
902 * might also have the same address, but would get better information
904 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
906 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
907 if (low > 0 &&
908 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
909 !cmp_sorttab_addr(module, low - 1, ref_addr))
910 low--;
911 else if (low < module->num_sorttab - 1 &&
912 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
913 !cmp_sorttab_addr(module, low + 1, ref_addr))
914 low++;
916 /* finally check that we fit into the found symbol */
917 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
918 if (addr < ref_addr) return NULL;
919 symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
920 if (addr >= ref_addr + ref_size) return NULL;
922 return module->addr_sorttab[low];
925 static BOOL symt_enum_locals_helper(struct module_pair* pair,
926 regex_t* preg, const struct sym_enum* se,
927 struct symt_function* func, const struct vector* v)
929 struct symt* lsym = NULL;
930 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
931 unsigned int i;
933 for (i=0; i<vector_length(v); i++)
935 lsym = *(struct symt**)vector_at(v, i);
936 switch (lsym->tag)
938 case SymTagBlock:
940 struct symt_block* block = (struct symt_block*)lsym;
941 if (pc < block->address || block->address + block->size <= pc)
942 continue;
943 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
944 return FALSE;
946 break;
947 case SymTagData:
948 if (match_regexp(preg, symt_get_name(lsym)))
950 if (send_symbol(se, pair, func, lsym)) return FALSE;
952 break;
953 case SymTagLabel:
954 case SymTagFuncDebugStart:
955 case SymTagFuncDebugEnd:
956 case SymTagCustom:
957 break;
958 default:
959 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
960 assert(0);
963 return TRUE;
966 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
967 const struct sym_enum* se)
969 struct module_pair pair;
970 struct symt_ht* sym;
971 DWORD pc = pcs->ctx_frame.InstructionOffset;
973 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
974 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
976 pair.pcs = pcs;
977 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
978 if (!module_get_debug(&pair)) return FALSE;
979 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
981 if (sym->symt.tag == SymTagFunction)
983 BOOL ret;
984 regex_t preg;
986 compile_regex(mask ? mask : "*", -1, &preg,
987 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
988 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
989 &((struct symt_function*)sym)->vchildren);
990 regfree(&preg);
991 return ret;
994 return send_symbol(se, &pair, NULL, &sym->symt);
997 /******************************************************************
998 * copy_symbolW
1000 * Helper for transforming an ANSI symbol info into a UNICODE one.
1001 * Assume that MaxNameLen is the same for both version (A & W).
1003 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1005 siw->SizeOfStruct = si->SizeOfStruct;
1006 siw->TypeIndex = si->TypeIndex;
1007 siw->Reserved[0] = si->Reserved[0];
1008 siw->Reserved[1] = si->Reserved[1];
1009 siw->Index = si->info; /* FIXME: see dbghelp.h */
1010 siw->Size = si->Size;
1011 siw->ModBase = si->ModBase;
1012 siw->Flags = si->Flags;
1013 siw->Value = si->Value;
1014 siw->Address = si->Address;
1015 siw->Register = si->Register;
1016 siw->Scope = si->Scope;
1017 siw->Tag = si->Tag;
1018 siw->NameLen = si->NameLen;
1019 siw->MaxNameLen = si->MaxNameLen;
1020 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1023 /******************************************************************
1024 * sym_enum
1026 * Core routine for most of the enumeration of symbols
1028 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1029 const struct sym_enum* se)
1031 struct module_pair pair;
1032 const char* bang;
1033 regex_t mod_regex, sym_regex;
1035 pair.pcs = process_find_by_handle(hProcess);
1036 if (BaseOfDll == 0)
1038 /* do local variables ? */
1039 if (!Mask || !(bang = strchr(Mask, '!')))
1040 return symt_enum_locals(pair.pcs, Mask, se);
1042 if (bang == Mask) return FALSE;
1044 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1045 compile_regex(bang + 1, -1, &sym_regex,
1046 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1048 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1050 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1052 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1053 symt_enum_module(&pair, &sym_regex, se))
1054 break;
1057 /* not found in PE modules, retry on the ELF ones
1059 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1061 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1063 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1064 !module_get_containee(pair.pcs, pair.requested) &&
1065 module_get_debug(&pair))
1067 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1068 symt_enum_module(&pair, &sym_regex, se))
1069 break;
1073 regfree(&mod_regex);
1074 regfree(&sym_regex);
1075 return TRUE;
1077 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1078 if (!module_get_debug(&pair))
1079 return FALSE;
1081 /* we always ignore module name from Mask when BaseOfDll is defined */
1082 if (Mask && (bang = strchr(Mask, '!')))
1084 if (bang == Mask) return FALSE;
1085 Mask = bang + 1;
1088 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1089 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1090 symt_enum_module(&pair, &sym_regex, se);
1091 regfree(&sym_regex);
1093 return TRUE;
1096 /******************************************************************
1097 * SymEnumSymbols (DBGHELP.@)
1099 * cases BaseOfDll = 0
1100 * !foo fails always (despite what MSDN states)
1101 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1102 * no ! in Mask, lookup in local Context
1103 * cases BaseOfDll != 0
1104 * !foo fails always (despite what MSDN states)
1105 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1107 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1108 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1109 PVOID UserContext)
1111 struct sym_enum se;
1113 TRACE("(%p %s %s %p %p)\n",
1114 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1115 EnumSymbolsCallback, UserContext);
1117 se.cb = EnumSymbolsCallback;
1118 se.user = UserContext;
1119 se.index = 0;
1120 se.tag = 0;
1121 se.addr = 0;
1122 se.sym_info = (PSYMBOL_INFO)se.buffer;
1124 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1127 struct sym_enumW
1129 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1130 void* ctx;
1131 PSYMBOL_INFOW sym_info;
1132 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1136 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1138 struct sym_enumW* sew = ctx;
1140 copy_symbolW(sew->sym_info, si);
1142 return (sew->cb)(sew->sym_info, size, sew->ctx);
1145 /******************************************************************
1146 * SymEnumSymbolsW (DBGHELP.@)
1149 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1150 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1151 PVOID UserContext)
1153 struct sym_enumW sew;
1154 BOOL ret = FALSE;
1155 char* maskA = NULL;
1157 sew.ctx = UserContext;
1158 sew.cb = EnumSymbolsCallback;
1159 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1161 if (Mask)
1163 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1164 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1165 if (!maskA) return FALSE;
1166 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1168 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1169 HeapFree(GetProcessHeap(), 0, maskA);
1171 return ret;
1174 struct sym_enumerate
1176 void* ctx;
1177 PSYM_ENUMSYMBOLS_CALLBACK cb;
1180 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1182 struct sym_enumerate* se = ctx;
1183 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1186 /***********************************************************************
1187 * SymEnumerateSymbols (DBGHELP.@)
1189 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1190 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1191 PVOID UserContext)
1193 struct sym_enumerate se;
1195 se.ctx = UserContext;
1196 se.cb = EnumSymbolsCallback;
1198 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1201 struct sym_enumerate64
1203 void* ctx;
1204 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1207 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1209 struct sym_enumerate64* se = ctx;
1210 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1213 /***********************************************************************
1214 * SymEnumerateSymbols64 (DBGHELP.@)
1216 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1217 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1218 PVOID UserContext)
1220 struct sym_enumerate64 se;
1222 se.ctx = UserContext;
1223 se.cb = EnumSymbolsCallback;
1225 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1228 /******************************************************************
1229 * SymFromAddr (DBGHELP.@)
1232 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1233 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1235 struct module_pair pair;
1236 struct symt_ht* sym;
1238 pair.pcs = process_find_by_handle(hProcess);
1239 if (!pair.pcs) return FALSE;
1240 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1241 if (!module_get_debug(&pair)) return FALSE;
1242 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1244 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1245 *Displacement = Address - Symbol->Address;
1246 return TRUE;
1249 /******************************************************************
1250 * SymFromAddrW (DBGHELP.@)
1253 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1254 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1256 PSYMBOL_INFO si;
1257 unsigned len;
1258 BOOL ret;
1260 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1261 si = HeapAlloc(GetProcessHeap(), 0, len);
1262 if (!si) return FALSE;
1264 si->SizeOfStruct = sizeof(*si);
1265 si->MaxNameLen = Symbol->MaxNameLen;
1266 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1268 copy_symbolW(Symbol, si);
1270 HeapFree(GetProcessHeap(), 0, si);
1271 return ret;
1274 /******************************************************************
1275 * SymGetSymFromAddr (DBGHELP.@)
1278 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1279 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1281 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1282 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1283 size_t len;
1284 DWORD64 Displacement64;
1286 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1287 si->SizeOfStruct = sizeof(*si);
1288 si->MaxNameLen = MAX_SYM_NAME;
1289 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1290 return FALSE;
1292 if (Displacement)
1293 *Displacement = Displacement64;
1294 Symbol->Address = si->Address;
1295 Symbol->Size = si->Size;
1296 Symbol->Flags = si->Flags;
1297 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1298 lstrcpynA(Symbol->Name, si->Name, len);
1299 return TRUE;
1302 /******************************************************************
1303 * SymGetSymFromAddr64 (DBGHELP.@)
1306 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1307 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1309 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1310 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1311 size_t len;
1312 DWORD64 Displacement64;
1314 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1315 si->SizeOfStruct = sizeof(*si);
1316 si->MaxNameLen = MAX_SYM_NAME;
1317 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1318 return FALSE;
1320 if (Displacement)
1321 *Displacement = Displacement64;
1322 Symbol->Address = si->Address;
1323 Symbol->Size = si->Size;
1324 Symbol->Flags = si->Flags;
1325 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1326 lstrcpynA(Symbol->Name, si->Name, len);
1327 return TRUE;
1330 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1331 SYMBOL_INFO* symbol)
1333 struct hash_table_iter hti;
1334 void* ptr;
1335 struct symt_ht* sym = NULL;
1336 struct module_pair pair;
1338 pair.pcs = pcs;
1339 if (!(pair.requested = module)) return FALSE;
1340 if (!module_get_debug(&pair)) return FALSE;
1342 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1343 while ((ptr = hash_table_iter_up(&hti)))
1345 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1347 if (!strcmp(sym->hash_elt.name, name))
1349 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1350 return TRUE;
1353 return FALSE;
1356 /******************************************************************
1357 * SymFromName (DBGHELP.@)
1360 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1362 struct process* pcs = process_find_by_handle(hProcess);
1363 struct module* module;
1364 const char* name;
1366 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1367 if (!pcs) return FALSE;
1368 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1369 name = strchr(Name, '!');
1370 if (name)
1372 char tmp[128];
1373 assert(name - Name < sizeof(tmp));
1374 memcpy(tmp, Name, name - Name);
1375 tmp[name - Name] = '\0';
1376 module = module_find_by_nameA(pcs, tmp);
1377 return find_name(pcs, module, name + 1, Symbol);
1379 for (module = pcs->lmodules; module; module = module->next)
1381 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1382 return TRUE;
1384 /* not found in PE modules, retry on the ELF ones
1386 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1388 for (module = pcs->lmodules; module; module = module->next)
1390 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1391 !module_get_containee(pcs, module) &&
1392 find_name(pcs, module, Name, Symbol))
1393 return TRUE;
1396 return FALSE;
1399 /***********************************************************************
1400 * SymGetSymFromName64 (DBGHELP.@)
1402 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1404 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1405 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1406 size_t len;
1408 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1409 si->SizeOfStruct = sizeof(*si);
1410 si->MaxNameLen = MAX_SYM_NAME;
1411 if (!SymFromName(hProcess, Name, si)) return FALSE;
1413 Symbol->Address = si->Address;
1414 Symbol->Size = si->Size;
1415 Symbol->Flags = si->Flags;
1416 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1417 lstrcpynA(Symbol->Name, si->Name, len);
1418 return TRUE;
1421 /***********************************************************************
1422 * SymGetSymFromName (DBGHELP.@)
1424 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1426 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1427 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1428 size_t len;
1430 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1431 si->SizeOfStruct = sizeof(*si);
1432 si->MaxNameLen = MAX_SYM_NAME;
1433 if (!SymFromName(hProcess, Name, si)) return FALSE;
1435 Symbol->Address = si->Address;
1436 Symbol->Size = si->Size;
1437 Symbol->Flags = si->Flags;
1438 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1439 lstrcpynA(Symbol->Name, si->Name, len);
1440 return TRUE;
1443 /******************************************************************
1444 * sym_fill_func_line_info
1446 * fills information about a file
1448 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1449 DWORD addr, IMAGEHLP_LINE* line)
1451 struct line_info* dli = NULL;
1452 BOOL found = FALSE;
1453 int i;
1455 assert(func->symt.tag == SymTagFunction);
1457 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1459 dli = vector_at(&func->vlines, i);
1460 if (!dli->is_source_file)
1462 if (found || dli->u.pc_offset > addr) continue;
1463 line->LineNumber = dli->line_number;
1464 line->Address = dli->u.pc_offset;
1465 line->Key = dli;
1466 found = TRUE;
1467 continue;
1469 if (found)
1471 line->FileName = (char*)source_get(module, dli->u.source_file);
1472 return TRUE;
1475 return FALSE;
1478 /***********************************************************************
1479 * SymGetSymNext64 (DBGHELP.@)
1481 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1483 /* algo:
1484 * get module from Symbol.Address
1485 * get index in module.addr_sorttab of Symbol.Address
1486 * increment index
1487 * if out of module bounds, move to next module in process address space
1489 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1490 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1491 return FALSE;
1494 /***********************************************************************
1495 * SymGetSymNext (DBGHELP.@)
1497 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1499 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1500 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1501 return FALSE;
1504 /***********************************************************************
1505 * SymGetSymPrev64 (DBGHELP.@)
1507 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1509 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1510 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1511 return FALSE;
1514 /***********************************************************************
1515 * SymGetSymPrev (DBGHELP.@)
1517 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1519 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1520 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1521 return FALSE;
1524 /******************************************************************
1525 * SymGetLineFromAddr (DBGHELP.@)
1528 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1529 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1531 struct module_pair pair;
1532 struct symt_ht* symt;
1534 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1536 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1538 pair.pcs = process_find_by_handle(hProcess);
1539 if (!pair.pcs) return FALSE;
1540 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1541 if (!module_get_debug(&pair)) return FALSE;
1542 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1544 if (symt->symt.tag != SymTagFunction) return FALSE;
1545 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1546 dwAddr, Line)) return FALSE;
1547 *pdwDisplacement = dwAddr - Line->Address;
1548 return TRUE;
1551 /******************************************************************
1552 * copy_line_64_from_32 (internal)
1555 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1558 l64->Key = l32->Key;
1559 l64->LineNumber = l32->LineNumber;
1560 l64->FileName = l32->FileName;
1561 l64->Address = l32->Address;
1564 /******************************************************************
1565 * copy_line_W64_from_32 (internal)
1568 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1570 unsigned len;
1572 l64->Key = l32->Key;
1573 l64->LineNumber = l32->LineNumber;
1574 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1575 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1576 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1577 l64->Address = l32->Address;
1580 /******************************************************************
1581 * copy_line_32_from_64 (internal)
1584 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1587 l32->Key = l64->Key;
1588 l32->LineNumber = l64->LineNumber;
1589 l32->FileName = l64->FileName;
1590 l32->Address = l64->Address;
1593 /******************************************************************
1594 * SymGetLineFromAddr64 (DBGHELP.@)
1597 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1598 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1600 IMAGEHLP_LINE line32;
1602 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1603 if (!validate_addr64(dwAddr)) return FALSE;
1604 line32.SizeOfStruct = sizeof(line32);
1605 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1606 return FALSE;
1607 copy_line_64_from_32(Line, &line32);
1608 return TRUE;
1611 /******************************************************************
1612 * SymGetLineFromAddrW64 (DBGHELP.@)
1615 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1616 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1618 struct process* pcs = process_find_by_handle(hProcess);
1619 IMAGEHLP_LINE line32;
1621 if (!pcs) return FALSE;
1622 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1623 if (!validate_addr64(dwAddr)) return FALSE;
1624 line32.SizeOfStruct = sizeof(line32);
1625 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1626 return FALSE;
1627 copy_line_W64_from_32(pcs, Line, &line32);
1628 return TRUE;
1631 /******************************************************************
1632 * SymGetLinePrev (DBGHELP.@)
1635 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1637 struct module_pair pair;
1638 struct line_info* li;
1639 BOOL in_search = FALSE;
1641 TRACE("(%p %p)\n", hProcess, Line);
1643 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1645 pair.pcs = process_find_by_handle(hProcess);
1646 if (!pair.pcs) return FALSE;
1647 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1648 if (!module_get_debug(&pair)) return FALSE;
1650 if (Line->Key == 0) return FALSE;
1651 li = Line->Key;
1652 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1653 * element we have to go back until we find the prev one to get the real
1654 * source file name for the DLIT_OFFSET element just before
1655 * the first DLIT_SOURCEFILE
1657 while (!li->is_first)
1659 li--;
1660 if (!li->is_source_file)
1662 Line->LineNumber = li->line_number;
1663 Line->Address = li->u.pc_offset;
1664 Line->Key = li;
1665 if (!in_search) return TRUE;
1667 else
1669 if (in_search)
1671 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1672 return TRUE;
1674 in_search = TRUE;
1677 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1678 return FALSE;
1681 /******************************************************************
1682 * SymGetLinePrev64 (DBGHELP.@)
1685 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1687 IMAGEHLP_LINE line32;
1689 line32.SizeOfStruct = sizeof(line32);
1690 copy_line_32_from_64(&line32, Line);
1691 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1692 copy_line_64_from_32(Line, &line32);
1693 return TRUE;
1696 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1698 struct line_info* li;
1700 if (line->Key == 0) return FALSE;
1701 li = line->Key;
1702 while (!li->is_last)
1704 li++;
1705 if (!li->is_source_file)
1707 line->LineNumber = li->line_number;
1708 line->Address = li->u.pc_offset;
1709 line->Key = li;
1710 return TRUE;
1712 line->FileName = (char*)source_get(module, li->u.source_file);
1714 return FALSE;
1717 /******************************************************************
1718 * SymGetLineNext (DBGHELP.@)
1721 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1723 struct module_pair pair;
1725 TRACE("(%p %p)\n", hProcess, Line);
1727 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1728 pair.pcs = process_find_by_handle(hProcess);
1729 if (!pair.pcs) return FALSE;
1730 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1731 if (!module_get_debug(&pair)) return FALSE;
1733 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1734 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1735 return FALSE;
1738 /******************************************************************
1739 * SymGetLineNext64 (DBGHELP.@)
1742 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1744 IMAGEHLP_LINE line32;
1746 line32.SizeOfStruct = sizeof(line32);
1747 copy_line_32_from_64(&line32, Line);
1748 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1749 copy_line_64_from_32(Line, &line32);
1750 return TRUE;
1753 /***********************************************************************
1754 * SymFunctionTableAccess (DBGHELP.@)
1756 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1758 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1759 return NULL;
1762 /***********************************************************************
1763 * SymFunctionTableAccess64 (DBGHELP.@)
1765 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1767 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1768 return NULL;
1771 /***********************************************************************
1772 * SymUnDName (DBGHELP.@)
1774 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1776 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1777 UNDNAME_COMPLETE) != 0;
1780 /***********************************************************************
1781 * SymUnDName64 (DBGHELP.@)
1783 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1785 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1786 UNDNAME_COMPLETE) != 0;
1789 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1790 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1792 /***********************************************************************
1793 * UnDecorateSymbolName (DBGHELP.@)
1795 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1796 DWORD UndecoratedLength, DWORD Flags)
1798 /* undocumented from msvcrt */
1799 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1800 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1802 TRACE("(%s, %p, %d, 0x%08x)\n",
1803 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1805 if (!p_undname)
1807 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1808 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1809 if (!p_undname) return 0;
1812 if (!UnDecoratedName) return 0;
1813 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1814 und_alloc, und_free, Flags))
1815 return 0;
1816 return strlen(UnDecoratedName);
1819 /******************************************************************
1820 * SymMatchString (DBGHELP.@)
1823 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1825 regex_t preg;
1826 BOOL ret;
1828 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1830 compile_regex(re, -1, &preg, _case);
1831 ret = match_regexp(&preg, string);
1832 regfree(&preg);
1833 return ret;
1836 /******************************************************************
1837 * SymSearch (DBGHELP.@)
1839 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1840 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1841 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1842 PVOID UserContext, DWORD Options)
1844 struct sym_enum se;
1846 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1847 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1848 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1849 UserContext, Options);
1851 if (Options != SYMSEARCH_GLOBALSONLY)
1853 FIXME("Unsupported searching with options (%x)\n", Options);
1854 SetLastError(ERROR_INVALID_PARAMETER);
1855 return FALSE;
1858 se.cb = EnumSymbolsCallback;
1859 se.user = UserContext;
1860 se.index = Index;
1861 se.tag = SymTag;
1862 se.addr = Address;
1863 se.sym_info = (PSYMBOL_INFO)se.buffer;
1865 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1868 /******************************************************************
1869 * SymSearchW (DBGHELP.@)
1871 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1872 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1873 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1874 PVOID UserContext, DWORD Options)
1876 struct sym_enumW sew;
1877 BOOL ret = FALSE;
1878 char* maskA = NULL;
1880 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1881 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1882 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1883 UserContext, Options);
1885 sew.ctx = UserContext;
1886 sew.cb = EnumSymbolsCallback;
1887 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1889 if (Mask)
1891 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1892 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1893 if (!maskA) return FALSE;
1894 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1896 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1897 sym_enumW, &sew, Options);
1898 HeapFree(GetProcessHeap(), 0, maskA);
1900 return ret;
1903 /******************************************************************
1904 * SymAddSymbol (DBGHELP.@)
1907 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1908 DWORD64 addr, DWORD size, DWORD flags)
1910 WCHAR nameW[MAX_SYM_NAME];
1912 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1913 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1916 /******************************************************************
1917 * SymAddSymbolW (DBGHELP.@)
1920 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1921 DWORD64 addr, DWORD size, DWORD flags)
1923 struct module_pair pair;
1925 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1927 pair.pcs = process_find_by_handle(hProcess);
1928 if (!pair.pcs) return FALSE;
1929 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1930 if (!module_get_debug(&pair)) return FALSE;
1932 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1933 return FALSE;
1936 /******************************************************************
1937 * SymSetScopeFromAddr (DBGHELP.@)
1939 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1941 struct process* pcs;
1943 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1945 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1946 return TRUE;
1949 /******************************************************************
1950 * SymEnumLines (DBGHELP.@)
1953 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1954 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1956 struct module_pair pair;
1957 struct hash_table_iter hti;
1958 struct symt_ht* sym;
1959 regex_t re;
1960 struct line_info* dli;
1961 void* ptr;
1962 SRCCODEINFO sci;
1963 const char* file;
1965 if (!cb) return FALSE;
1966 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
1968 pair.pcs = process_find_by_handle(hProcess);
1969 if (!pair.pcs) return FALSE;
1970 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
1971 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
1972 if (!module_get_debug(&pair)) return FALSE;
1973 if (!compile_file_regex(&re, srcfile)) return FALSE;
1975 sci.SizeOfStruct = sizeof(sci);
1976 sci.ModBase = base;
1978 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
1979 while ((ptr = hash_table_iter_up(&hti)))
1981 unsigned int i;
1983 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1984 if (sym->symt.tag != SymTagFunction) continue;
1986 sci.FileName[0] = '\0';
1987 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
1989 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
1990 if (dli->is_source_file)
1992 file = source_get(pair.effective, dli->u.source_file);
1993 if (!match_regexp(&re, file)) file = "";
1994 strcpy(sci.FileName, file);
1996 else if (sci.FileName[0])
1998 sci.Key = dli;
1999 sci.Obj[0] = '\0'; /* FIXME */
2000 sci.LineNumber = dli->line_number;
2001 sci.Address = dli->u.pc_offset;
2002 if (!cb(&sci, user)) break;
2006 regfree(&re);
2007 return TRUE;