Partially implement and test the shelllink object's
[wine/multimedia.git] / dlls / dbghelp / symbol.c
blob923884b5403df33258bd45017e504f6e40582da8
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
40 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
41 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
43 inline static int cmp_addr(ULONG64 a1, ULONG64 a2)
45 if (a1 > a2) return 1;
46 if (a1 < a2) return -1;
47 return 0;
50 inline static int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
52 ULONG64 ref;
54 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
55 return cmp_addr(ref, addr);
58 int symt_cmp_addr(const void* p1, const void* p2)
60 const struct symt* sym1 = *(const struct symt* const *)p1;
61 const struct symt* sym2 = *(const struct symt* const *)p2;
62 ULONG64 a1, a2;
64 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
65 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
66 return cmp_addr(a1, a2);
69 static inline void re_append(char** mask, unsigned* len, char ch)
71 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
72 (*mask)[*len - 2] = ch;
75 /* transforms a dbghelp's regular expression into a POSIX one
76 * Here are the valid dbghelp reg ex characters:
77 * * 0 or more characters
78 * ? a single character
79 * [] list
80 * # 0 or more of preceding char
81 * + 1 or more of preceding char
82 * escapes \ on #, ?, [, ], *, +. don't work on -
84 static void compile_regex(const char* str, int numchar, regex_t* re)
86 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
87 unsigned len = 1;
88 BOOL in_escape = FALSE;
90 re_append(&mask, &len, '^');
92 while (*str && numchar--)
94 /* FIXME: this shouldn't be valid on '-' */
95 if (in_escape)
97 re_append(&mask, &len, '\\');
98 re_append(&mask, &len, *str);
99 in_escape = FALSE;
101 else switch (*str)
103 case '\\': in_escape = TRUE; break;
104 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
105 case '?': re_append(&mask, &len, '.'); break;
106 case '#': re_append(&mask, &len, '*'); break;
107 /* escape some valid characters in dbghelp reg exp:s */
108 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
109 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
110 default: re_append(&mask, &len, *str); break;
112 str++;
114 if (in_escape)
116 re_append(&mask, &len, '\\');
117 re_append(&mask, &len, '\\');
119 re_append(&mask, &len, '$');
120 mask[len - 1] = '\0';
121 if (regcomp(re, mask, REG_NOSUB)) FIXME("Couldn't compile %s\n", mask);
122 HeapFree(GetProcessHeap(), 0, mask);
125 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
127 struct symt_compiland* sym;
129 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
130 module->module.ModuleName, name);
131 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
133 sym->symt.tag = SymTagCompiland;
134 sym->source = source_new(module, name);
135 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
137 return sym;
140 struct symt_public* symt_new_public(struct module* module,
141 struct symt_compiland* compiland,
142 const char* name,
143 unsigned long address, unsigned size,
144 BOOL in_code, BOOL is_func)
146 struct symt_public* sym;
147 struct symt** p;
149 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
150 module->module.ModuleName, name, address);
151 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
152 symt_find_nearest(module, address) != -1)
153 return NULL;
154 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
156 sym->symt.tag = SymTagPublicSymbol;
157 sym->hash_elt.name = pool_strdup(&module->pool, name);
158 hash_table_add(&module->ht_symbols, &sym->hash_elt);
159 module->sortlist_valid = FALSE;
160 sym->container = compiland ? &compiland->symt : NULL;
161 sym->address = address;
162 sym->size = size;
163 sym->in_code = in_code;
164 sym->is_function = is_func;
165 if (compiland)
167 p = vector_add(&compiland->vchildren, &module->pool);
168 *p = &sym->symt;
171 return sym;
174 struct symt_data* symt_new_global_variable(struct module* module,
175 struct symt_compiland* compiland,
176 const char* name, unsigned is_static,
177 unsigned long addr, unsigned long size,
178 struct symt* type)
180 struct symt_data* sym;
181 struct symt** p;
182 DWORD tsz;
184 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
185 module->module.ModuleName, name, addr, type);
186 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
188 sym->symt.tag = SymTagData;
189 sym->hash_elt.name = pool_strdup(&module->pool, name);
190 hash_table_add(&module->ht_symbols, &sym->hash_elt);
191 module->sortlist_valid = FALSE;
192 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
193 sym->container = compiland ? &compiland->symt : NULL;
194 sym->type = type;
195 sym->u.address = addr;
196 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
198 if (tsz != size)
199 FIXME("Size mismatch for %s.%s between type (%lu) and src (%lu)\n",
200 module->module.ModuleName, name, tsz, size);
202 if (compiland)
204 p = vector_add(&compiland->vchildren, &module->pool);
205 *p = &sym->symt;
208 return sym;
211 struct symt_function* symt_new_function(struct module* module,
212 struct symt_compiland* compiland,
213 const char* name,
214 unsigned long addr, unsigned long size,
215 struct symt* sig_type)
217 struct symt_function* sym;
218 struct symt** p;
220 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
221 module->module.ModuleName, name, addr, addr + size - 1);
223 assert(!sig_type || sig_type->tag == SymTagFunctionType);
224 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
226 sym->symt.tag = SymTagFunction;
227 sym->hash_elt.name = pool_strdup(&module->pool, name);
228 hash_table_add(&module->ht_symbols, &sym->hash_elt);
229 module->sortlist_valid = FALSE;
230 sym->container = &compiland->symt;
231 sym->address = addr;
232 sym->type = sig_type;
233 sym->size = size;
234 vector_init(&sym->vlines, sizeof(struct line_info), 64);
235 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
236 if (compiland)
238 p = vector_add(&compiland->vchildren, &module->pool);
239 *p = &sym->symt;
242 return sym;
245 void symt_add_func_line(struct module* module, struct symt_function* func,
246 unsigned source_idx, int line_num, unsigned long offset)
248 struct line_info* dli;
249 BOOL last_matches = FALSE;
251 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
253 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
254 func, func->hash_elt.name, offset,
255 source_get(module, source_idx), line_num);
257 assert(func->symt.tag == SymTagFunction);
259 dli = NULL;
260 while ((dli = vector_iter_down(&func->vlines, dli)))
262 if (dli->is_source_file)
264 last_matches = (source_idx == dli->u.source_file);
265 break;
269 if (!last_matches)
271 /* we shouldn't have line changes on first line of function */
272 dli = vector_add(&func->vlines, &module->pool);
273 dli->is_source_file = 1;
274 dli->is_first = dli->is_last = 0;
275 dli->line_number = 0;
276 dli->u.source_file = source_idx;
278 dli = vector_add(&func->vlines, &module->pool);
279 dli->is_source_file = 0;
280 dli->is_first = dli->is_last = 0;
281 dli->line_number = line_num;
282 dli->u.pc_offset = func->address + offset;
285 struct symt_data* symt_add_func_local(struct module* module,
286 struct symt_function* func,
287 int regno, int offset,
288 struct symt_block* block,
289 struct symt* type, const char* name)
291 struct symt_data* locsym;
292 struct symt** p;
294 assert(func);
295 assert(func->symt.tag == SymTagFunction);
297 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
298 module->module.ModuleName, func->hash_elt.name,
299 name, type);
300 locsym = pool_alloc(&module->pool, sizeof(*locsym));
301 locsym->symt.tag = SymTagData;
302 locsym->hash_elt.name = pool_strdup(&module->pool, name);
303 locsym->hash_elt.next = NULL;
304 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
305 locsym->container = &block->symt;
306 locsym->type = type;
307 if (regno)
309 locsym->u.s.reg_id = regno;
310 locsym->u.s.offset = 0;
311 locsym->u.s.length = 0;
313 else
315 locsym->u.s.reg_id = 0;
316 locsym->u.s.offset = offset * 8;
317 locsym->u.s.length = 0;
319 if (block)
320 p = vector_add(&block->vchildren, &module->pool);
321 else
322 p = vector_add(&func->vchildren, &module->pool);
323 *p = &locsym->symt;
324 return locsym;
327 struct symt_block* symt_open_func_block(struct module* module,
328 struct symt_function* func,
329 struct symt_block* parent_block,
330 unsigned pc, unsigned len)
332 struct symt_block* block;
333 struct symt** p;
335 assert(func);
336 assert(func->symt.tag == SymTagFunction);
338 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
339 block = pool_alloc(&module->pool, sizeof(*block));
340 block->symt.tag = SymTagBlock;
341 block->address = func->address + pc;
342 block->size = len;
343 block->container = parent_block ? &parent_block->symt : &func->symt;
344 vector_init(&block->vchildren, sizeof(struct symt*), 4);
345 if (parent_block)
346 p = vector_add(&parent_block->vchildren, &module->pool);
347 else
348 p = vector_add(&func->vchildren, &module->pool);
349 *p = &block->symt;
351 return block;
354 struct symt_block* symt_close_func_block(struct module* module,
355 struct symt_function* func,
356 struct symt_block* block, unsigned pc)
358 assert(func->symt.tag == SymTagFunction);
360 if (pc) block->size = func->address + pc - block->address;
361 return (block->container->tag == SymTagBlock) ?
362 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
365 struct symt_function_point* symt_add_function_point(struct module* module,
366 struct symt_function* func,
367 enum SymTagEnum point,
368 unsigned offset, const char* name)
370 struct symt_function_point* sym;
371 struct symt** p;
373 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
375 sym->symt.tag = point;
376 sym->parent = func;
377 sym->offset = offset;
378 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
379 p = vector_add(&func->vchildren, &module->pool);
380 *p = &sym->symt;
382 return sym;
385 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
387 unsigned len;
388 struct line_info* dli;
390 assert(func);
391 /* We aren't adding any more locals or line numbers to this function.
392 * Free any spare memory that we might have allocated.
394 assert(func->symt.tag == SymTagFunction);
396 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
397 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
399 len = vector_length(&func->vlines);
400 if (len--)
402 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
403 dli = vector_at(&func->vlines, len); dli->is_last = 1;
405 return TRUE;
408 struct symt_thunk* symt_new_thunk(struct module* module,
409 struct symt_compiland* compiland,
410 const char* name, THUNK_ORDINAL ord,
411 unsigned long addr, unsigned long size)
413 struct symt_thunk* sym;
415 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
416 module->module.ModuleName, name, addr, addr + size - 1);
418 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
420 sym->symt.tag = SymTagThunk;
421 sym->hash_elt.name = pool_strdup(&module->pool, name);
422 hash_table_add(&module->ht_symbols, &sym->hash_elt);
423 module->sortlist_valid = FALSE;
424 sym->container = &compiland->symt;
425 sym->address = addr;
426 sym->size = size;
427 sym->ordinal = ord;
428 if (compiland)
430 struct symt** p;
431 p = vector_add(&compiland->vchildren, &module->pool);
432 *p = &sym->symt;
435 return sym;
438 /* expect sym_info->MaxNameLen to be set before being called */
439 static void symt_fill_sym_info(const struct module* module,
440 const struct symt* sym, SYMBOL_INFO* sym_info)
442 const char* name;
444 sym_info->TypeIndex = (DWORD)sym;
445 sym_info->info = 0; /* TBD */
446 symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
447 sym_info->ModBase = module->module.BaseOfImage;
448 sym_info->Flags = 0;
449 switch (sym->tag)
451 case SymTagData:
453 const struct symt_data* data = (const struct symt_data*)sym;
454 switch (data->kind)
456 case DataIsLocal:
457 case DataIsParam:
458 if (data->u.s.reg_id)
460 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGISTER;
461 sym_info->Register = data->u.s.reg_id;
462 sym_info->Address = 0;
464 else
466 if (data->u.s.offset < 0)
467 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
468 else
469 sym_info->Flags |= SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
470 /* FIXME: needed ? moreover, it's i386 dependent !!! */
471 sym_info->Register = CV_REG_EBP;
472 sym_info->Address = data->u.s.offset;
474 break;
475 case DataIsGlobal:
476 case DataIsFileStatic:
477 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
478 sym_info->Register = 0;
479 break;
480 case DataIsConstant:
481 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
482 switch (data->u.value.n1.n2.vt)
484 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
485 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
486 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
487 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
488 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
489 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
490 default:
491 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
493 break;
494 default:
495 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
498 break;
499 case SymTagPublicSymbol:
500 sym_info->Flags |= SYMFLAG_EXPORT;
501 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
502 break;
503 case SymTagFunction:
504 sym_info->Flags |= SYMFLAG_FUNCTION;
505 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
506 break;
507 case SymTagThunk:
508 sym_info->Flags |= SYMFLAG_THUNK;
509 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
510 break;
511 default:
512 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
513 sym_info->Register = 0;
514 break;
516 sym_info->Scope = 0; /* FIXME */
517 sym_info->Tag = sym->tag;
518 name = symt_get_name(sym);
519 if (sym_info->MaxNameLen)
521 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
522 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
523 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
525 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
526 memcpy(sym_info->Name, name, sym_info->NameLen);
527 sym_info->Name[sym_info->NameLen] = '\0';
530 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
531 sym, sym_info->Name, sym_info->Size,
532 wine_dbgstr_longlong(sym_info->Address));
535 static BOOL symt_enum_module(struct module* module, regex_t* regex,
536 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
538 char buffer[sizeof(SYMBOL_INFO) + 256];
539 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
540 void* ptr;
541 struct symt_ht* sym = NULL;
542 struct hash_table_iter hti;
544 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
545 while ((ptr = hash_table_iter_up(&hti)))
547 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
548 if (sym->hash_elt.name &&
549 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
551 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
552 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
553 symt_fill_sym_info(module, &sym->symt, sym_info);
554 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
557 return FALSE;
560 /***********************************************************************
561 * resort_symbols
563 * Rebuild sorted list of symbols for a module.
565 static BOOL resort_symbols(struct module* module)
567 int nsym = 0;
568 void* ptr;
569 struct symt_ht* sym;
570 struct hash_table_iter hti;
572 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
573 while ((ptr = hash_table_iter_up(&hti)))
574 nsym++;
576 if (!(module->module.NumSyms = nsym)) return FALSE;
578 if (module->addr_sorttab)
579 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
580 module->addr_sorttab,
581 nsym * sizeof(struct symt_ht*));
582 else
583 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
584 nsym * sizeof(struct symt_ht*));
585 if (!module->addr_sorttab) return FALSE;
587 nsym = 0;
588 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
589 while ((ptr = hash_table_iter_up(&hti)))
591 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
592 assert(sym);
593 module->addr_sorttab[nsym++] = sym;
596 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
597 return module->sortlist_valid = TRUE;
600 /* assume addr is in module */
601 int symt_find_nearest(struct module* module, DWORD addr)
603 int mid, high, low;
604 ULONG64 ref_addr;
605 DWORD ref_size;
607 if (!module->sortlist_valid || !module->addr_sorttab)
609 if (!resort_symbols(module)) return -1;
613 * Binary search to find closest symbol.
615 low = 0;
616 high = module->module.NumSyms;
618 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
619 if (addr < ref_addr) return -1;
620 if (high)
622 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
623 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
624 ref_size = 0x1000; /* arbitrary value */
625 if (addr >= ref_addr + ref_size) return -1;
628 while (high > low + 1)
630 mid = (high + low) / 2;
631 if (cmp_sorttab_addr(module, mid, addr) < 0)
632 low = mid;
633 else
634 high = mid;
636 if (low != high && high != module->module.NumSyms &&
637 cmp_sorttab_addr(module, high, addr) <= 0)
638 low = high;
640 /* If found symbol is a public symbol, check if there are any other entries that
641 * might also have the same address, but would get better information
643 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
645 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
646 if (low > 0 &&
647 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
648 !cmp_sorttab_addr(module, low - 1, ref_addr))
649 low--;
650 else if (low < module->module.NumSyms - 1 &&
651 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
652 !cmp_sorttab_addr(module, low + 1, ref_addr))
653 low++;
655 /* finally check that we fit into the found symbol */
656 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
657 if (addr < ref_addr) return -1;
658 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
659 ref_size = 0x1000; /* arbitrary value */
660 if (addr >= ref_addr + ref_size) return -1;
662 return low;
665 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
666 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
667 PVOID user, SYMBOL_INFO* sym_info,
668 struct vector* v)
670 struct symt** plsym = NULL;
671 struct symt* lsym = NULL;
672 DWORD pc = pcs->ctx_frame.InstructionOffset;
674 while ((plsym = vector_iter_up(v, plsym)))
676 lsym = *plsym;
677 switch (lsym->tag)
679 case SymTagBlock:
681 struct symt_block* block = (struct symt_block*)lsym;
682 if (pc < block->address || block->address + block->size <= pc)
683 continue;
684 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
685 sym_info, &block->vchildren))
686 return FALSE;
688 break;
689 case SymTagData:
690 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
692 symt_fill_sym_info(module, lsym, sym_info);
693 if (!cb(sym_info, sym_info->Size, user))
694 return FALSE;
696 break;
697 case SymTagLabel:
698 case SymTagFuncDebugStart:
699 case SymTagFuncDebugEnd:
700 break;
701 default:
702 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
703 assert(0);
706 return TRUE;
709 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
710 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
711 PVOID UserContext)
713 struct module* module;
714 struct symt_ht* sym;
715 char buffer[sizeof(SYMBOL_INFO) + 256];
716 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
717 DWORD pc = pcs->ctx_frame.InstructionOffset;
718 int idx;
720 sym_info->SizeOfStruct = sizeof(*sym_info);
721 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
723 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
724 if (!(module = module_get_debug(pcs, module))) return FALSE;
725 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
727 sym = module->addr_sorttab[idx];
728 if (sym->symt.tag == SymTagFunction)
730 BOOL ret;
731 regex_t preg;
733 compile_regex(mask ? mask : "*", -1, &preg);
734 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
735 UserContext, sym_info,
736 &((struct symt_function*)sym)->vchildren);
737 regfree(&preg);
738 return ret;
741 symt_fill_sym_info(module, &sym->symt, sym_info);
742 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
745 /******************************************************************
746 * SymEnumSymbols (DBGHELP.@)
748 * cases BaseOfDll = 0
749 * !foo fails always (despite what MSDN states)
750 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
751 * no ! in Mask, lookup in local Context
752 * cases BaseOfDll != 0
753 * !foo fails always (despite what MSDN states)
754 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
756 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
757 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
758 PVOID UserContext)
760 struct process* pcs = process_find_by_handle(hProcess);
761 struct module* module;
762 struct module* dbg_module;
763 const char* bang;
764 regex_t mod_regex, sym_regex;
766 TRACE("(%p %s %s %p %p)\n",
767 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
768 EnumSymbolsCallback, UserContext);
770 if (!pcs) return FALSE;
772 if (BaseOfDll == 0)
774 /* do local variables ? */
775 if (!Mask || !(bang = strchr(Mask, '!')))
776 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
778 if (bang == Mask) return FALSE;
780 compile_regex(Mask, bang - Mask, &mod_regex);
781 compile_regex(bang + 1, -1, &sym_regex);
783 for (module = pcs->lmodules; module; module = module->next)
785 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
787 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
788 symt_enum_module(dbg_module, &sym_regex,
789 EnumSymbolsCallback, UserContext))
790 break;
793 /* not found in PE modules, retry on the ELF ones
795 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
797 for (module = pcs->lmodules; module; module = module->next)
799 if (module->type == DMT_ELF &&
800 !module_get_containee(pcs, module) &&
801 (dbg_module = module_get_debug(pcs, module)))
803 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
804 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
805 break;
809 regfree(&mod_regex);
810 regfree(&sym_regex);
811 return TRUE;
813 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
814 if (!(module = module_get_debug(pcs, module)))
815 return FALSE;
817 /* we always ignore module name from Mask when BaseOfDll is defined */
818 if (Mask && (bang = strchr(Mask, '!')))
820 if (bang == Mask) return FALSE;
821 Mask = bang + 1;
824 compile_regex(Mask ? Mask : "*", -1, &sym_regex);
825 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
826 regfree(&sym_regex);
828 return TRUE;
831 struct sym_enumerate
833 void* ctx;
834 PSYM_ENUMSYMBOLS_CALLBACK cb;
837 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
839 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
840 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
843 /***********************************************************************
844 * SymEnumerateSymbols (DBGHELP.@)
846 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
847 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
848 PVOID UserContext)
850 struct sym_enumerate se;
852 se.ctx = UserContext;
853 se.cb = EnumSymbolsCallback;
855 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
858 /******************************************************************
859 * SymFromAddr (DBGHELP.@)
862 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
863 DWORD64* Displacement, PSYMBOL_INFO Symbol)
865 struct process* pcs = process_find_by_handle(hProcess);
866 struct module* module;
867 struct symt_ht* sym;
868 int idx;
870 if (!pcs) return FALSE;
871 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
872 if (!(module = module_get_debug(pcs, module))) return FALSE;
873 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
875 sym = module->addr_sorttab[idx];
877 symt_fill_sym_info(module, &sym->symt, Symbol);
878 *Displacement = Address - Symbol->Address;
879 return TRUE;
882 /******************************************************************
883 * SymGetSymFromAddr (DBGHELP.@)
886 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
887 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
889 char buffer[sizeof(SYMBOL_INFO) + 256];
890 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
891 size_t len;
892 DWORD64 Displacement64;
894 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
895 si->SizeOfStruct = sizeof(*si);
896 si->MaxNameLen = 256;
897 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
898 return FALSE;
900 if (Displacement)
901 *Displacement = Displacement64;
902 Symbol->Address = si->Address;
903 Symbol->Size = si->Size;
904 Symbol->Flags = si->Flags;
905 len = min(Symbol->MaxNameLength, si->MaxNameLen);
906 lstrcpynA(Symbol->Name, si->Name, len);
907 return TRUE;
910 /******************************************************************
911 * SymFromName (DBGHELP.@)
914 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
916 struct process* pcs = process_find_by_handle(hProcess);
917 struct module* module;
918 struct hash_table_iter hti;
919 void* ptr;
920 struct symt_ht* sym = NULL;
921 const char* name;
923 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
924 if (!pcs) return FALSE;
925 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
926 name = strchr(Name, '!');
927 if (name)
929 char tmp[128];
930 assert(name - Name < sizeof(tmp));
931 memcpy(tmp, Name, name - Name);
932 tmp[name - Name] = '\0';
933 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
934 if (!module) return FALSE;
935 Name = (char*)(name + 1);
937 else module = pcs->lmodules;
939 /* FIXME: Name could be made out of a regular expression */
940 for (; module; module = (name) ? NULL : module->next)
942 if (module->module.SymType == SymNone) continue;
943 if (module->module.SymType == SymDeferred)
945 struct module* xmodule = module_get_debug(pcs, module);
946 if (!xmodule || xmodule != module) continue;
948 hash_table_iter_init(&module->ht_symbols, &hti, Name);
949 while ((ptr = hash_table_iter_up(&hti)))
951 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
953 if (!strcmp(sym->hash_elt.name, Name))
955 symt_fill_sym_info(module, &sym->symt, Symbol);
956 return TRUE;
960 return FALSE;
963 /***********************************************************************
964 * SymGetSymFromName (DBGHELP.@)
966 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
968 char buffer[sizeof(SYMBOL_INFO) + 256];
969 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
970 size_t len;
972 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
973 si->SizeOfStruct = sizeof(*si);
974 si->MaxNameLen = 256;
975 if (!SymFromName(hProcess, Name, si)) return FALSE;
977 Symbol->Address = si->Address;
978 Symbol->Size = si->Size;
979 Symbol->Flags = si->Flags;
980 len = min(Symbol->MaxNameLength, si->MaxNameLen);
981 lstrcpynA(Symbol->Name, si->Name, len);
982 return TRUE;
985 /******************************************************************
986 * sym_fill_func_line_info
988 * fills information about a file
990 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
991 DWORD addr, IMAGEHLP_LINE* line)
993 struct line_info* dli = NULL;
994 BOOL found = FALSE;
996 assert(func->symt.tag == SymTagFunction);
998 while ((dli = vector_iter_down(&func->vlines, dli)))
1000 if (!dli->is_source_file)
1002 if (found || dli->u.pc_offset > addr) continue;
1003 line->LineNumber = dli->line_number;
1004 line->Address = dli->u.pc_offset;
1005 line->Key = dli;
1006 found = TRUE;
1007 continue;
1009 if (found)
1011 line->FileName = (char*)source_get(module, dli->u.source_file);
1012 return TRUE;
1015 return FALSE;
1018 /***********************************************************************
1019 * SymGetSymNext (DBGHELP.@)
1021 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1023 /* algo:
1024 * get module from Symbol.Address
1025 * get index in module.addr_sorttab of Symbol.Address
1026 * increment index
1027 * if out of module bounds, move to next module in process address space
1029 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1030 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1031 return FALSE;
1034 /***********************************************************************
1035 * SymGetSymPrev (DBGHELP.@)
1038 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1040 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1041 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1042 return FALSE;
1045 /******************************************************************
1046 * SymGetLineFromAddr (DBGHELP.@)
1049 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1050 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1052 struct process* pcs = process_find_by_handle(hProcess);
1053 struct module* module;
1054 int idx;
1056 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1058 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1060 if (!pcs) return FALSE;
1061 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1062 if (!(module = module_get_debug(pcs, module))) return FALSE;
1063 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1065 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1066 if (!symt_fill_func_line_info(module,
1067 (struct symt_function*)module->addr_sorttab[idx],
1068 dwAddr, Line)) return FALSE;
1069 *pdwDisplacement = dwAddr - Line->Address;
1070 return TRUE;
1073 /******************************************************************
1074 * SymGetLinePrev (DBGHELP.@)
1077 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1079 struct process* pcs = process_find_by_handle(hProcess);
1080 struct module* module;
1081 struct line_info* li;
1082 BOOL in_search = FALSE;
1084 TRACE("(%p %p)\n", hProcess, Line);
1086 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1088 if (!pcs) return FALSE;
1089 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1090 if (!(module = module_get_debug(pcs, module))) return FALSE;
1092 if (Line->Key == 0) return FALSE;
1093 li = (struct line_info*)Line->Key;
1094 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1095 * element we have to go back until we find the prev one to get the real
1096 * source file name for the DLIT_OFFSET element just before
1097 * the first DLIT_SOURCEFILE
1099 while (!li->is_first)
1101 li--;
1102 if (!li->is_source_file)
1104 Line->LineNumber = li->line_number;
1105 Line->Address = li->u.pc_offset;
1106 Line->Key = li;
1107 if (!in_search) return TRUE;
1109 else
1111 if (in_search)
1113 Line->FileName = (char*)source_get(module, li->u.source_file);
1114 return TRUE;
1116 in_search = TRUE;
1119 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1120 return FALSE;
1123 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1125 struct line_info* li;
1127 if (line->Key == 0) return FALSE;
1128 li = (struct line_info*)line->Key;
1129 while (!li->is_last)
1131 li++;
1132 if (!li->is_source_file)
1134 line->LineNumber = li->line_number;
1135 line->Address = li->u.pc_offset;
1136 line->Key = li;
1137 return TRUE;
1139 line->FileName = (char*)source_get(module, li->u.source_file);
1141 return FALSE;
1144 /******************************************************************
1145 * SymGetLineNext (DBGHELP.@)
1148 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1150 struct process* pcs = process_find_by_handle(hProcess);
1151 struct module* module;
1153 TRACE("(%p %p)\n", hProcess, Line);
1155 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1156 if (!pcs) return FALSE;
1157 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1158 if (!(module = module_get_debug(pcs, module))) return FALSE;
1160 if (symt_get_func_line_next(module, Line)) return TRUE;
1161 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1162 return FALSE;
1165 /***********************************************************************
1166 * SymFunctionTableAccess (DBGHELP.@)
1168 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1170 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1171 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1172 return FALSE;
1175 /***********************************************************************
1176 * SymUnDName (DBGHELP.@)
1178 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1180 TRACE("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1181 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1182 UNDNAME_COMPLETE) != 0;
1185 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1186 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1188 /***********************************************************************
1189 * UnDecorateSymbolName (DBGHELP.@)
1191 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1192 DWORD UndecoratedLength, DWORD Flags)
1194 /* undocumented from msvcrt */
1195 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1196 static WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1198 TRACE("(%s, %p, %ld, 0x%08lx): stub\n",
1199 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1201 if (!p_undname)
1203 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1204 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1205 if (!p_undname) return 0;
1208 if (!UnDecoratedName) return 0;
1209 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1210 und_alloc, und_free, Flags))
1211 return 0;
1212 return strlen(UnDecoratedName);