Update types checked by winapi_check.
[wine/dcerpc.git] / dlls / dbghelp / symbol.c
blob35b5cc67a66d30cb88fe41d048740013beea31c8
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 struct line_info
45 unsigned long is_first : 1,
46 is_last : 1,
47 is_source_file : 1,
48 line_number;
49 union
51 unsigned long pc_offset; /* if is_source_file isn't set */
52 unsigned source_file; /* if is_source_file is set */
53 } u;
56 inline static int cmp_addr(DWORD a1, DWORD a2)
58 if (a1 > a2) return 1;
59 if (a1 < a2) return -1;
60 return 0;
63 inline static int cmp_sorttab_addr(const struct module* module, int idx, DWORD addr)
65 DWORD ref;
67 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
68 return cmp_addr(ref, addr);
71 int symt_cmp_addr(const void* p1, const void* p2)
73 const struct symt* sym1 = *(const struct symt* const *)p1;
74 const struct symt* sym2 = *(const struct symt* const *)p2;
75 DWORD a1, a2;
77 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
78 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
79 return cmp_addr(a1, a2);
82 static inline void re_append(char** mask, unsigned* len, char ch)
84 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
85 (*mask)[*len - 2] = ch;
88 /* transforms a dbghelp's regular expression into a POSIX one
89 * Here are the valid dbghelp reg ex characters:
90 * * 0 or more characters
91 * ? a single character
92 * [] list
93 * # 0 or more of preceding char
94 * + 1 or more of preceding char
95 * escapes \ on #, ?, [, ], *, +. don't work on -
97 static void compile_regex(const char* str, int numchar, regex_t* re)
99 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
100 unsigned len = 1;
101 BOOL in_escape = FALSE;
103 re_append(&mask, &len, '^');
105 while (*str && numchar--)
107 /* FIXME: this shouldn't be valid on '-' */
108 if (in_escape)
110 re_append(&mask, &len, '\\');
111 re_append(&mask, &len, *str);
112 in_escape = FALSE;
114 else switch (*str)
116 case '\\': in_escape = TRUE; break;
117 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
118 case '?': re_append(&mask, &len, '.'); break;
119 case '#': re_append(&mask, &len, '*'); break;
120 /* escape some valid characters in dbghelp reg exp:s */
121 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
122 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
123 default: re_append(&mask, &len, *str); break;
125 str++;
127 if (in_escape)
129 re_append(&mask, &len, '\\');
130 re_append(&mask, &len, '\\');
132 re_append(&mask, &len, '$');
133 mask[len - 1] = '\0';
134 if (regcomp(re, mask, REG_NOSUB)) FIXME("Couldn't compile %s\n", mask);
135 HeapFree(GetProcessHeap(), 0, mask);
138 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
140 struct symt_compiland* sym;
142 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
143 module->module.ModuleName, name);
144 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
146 sym->symt.tag = SymTagCompiland;
147 sym->source = source_new(module, name);
148 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
150 return sym;
153 struct symt_public* symt_new_public(struct module* module,
154 struct symt_compiland* compiland,
155 const char* name,
156 unsigned long address, unsigned size,
157 BOOL in_code, BOOL is_func)
159 struct symt_public* sym;
160 struct symt** p;
162 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
163 module->module.ModuleName, name, address);
164 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
165 symt_find_nearest(module, address) != -1)
166 return NULL;
167 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
169 sym->symt.tag = SymTagPublicSymbol;
170 sym->hash_elt.name = pool_strdup(&module->pool, name);
171 hash_table_add(&module->ht_symbols, &sym->hash_elt);
172 module->sortlist_valid = FALSE;
173 sym->container = compiland ? &compiland->symt : NULL;
174 sym->address = address;
175 sym->size = size;
176 sym->in_code = in_code;
177 sym->is_function = is_func;
178 if (compiland)
180 p = vector_add(&compiland->vchildren, &module->pool);
181 *p = &sym->symt;
184 return sym;
187 struct symt_data* symt_new_global_variable(struct module* module,
188 struct symt_compiland* compiland,
189 const char* name, unsigned is_static,
190 unsigned long addr, unsigned long size,
191 struct symt* type)
193 struct symt_data* sym;
194 struct symt** p;
195 DWORD tsz;
197 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
198 module->module.ModuleName, name, addr, type);
199 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
201 sym->symt.tag = SymTagData;
202 sym->hash_elt.name = pool_strdup(&module->pool, name);
203 hash_table_add(&module->ht_symbols, &sym->hash_elt);
204 module->sortlist_valid = FALSE;
205 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
206 sym->container = compiland ? &compiland->symt : NULL;
207 sym->type = type;
208 sym->u.address = addr;
209 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
211 if (tsz != size)
212 FIXME("Size mismatch for %s.%s between type (%lu) and src (%lu)\n",
213 module->module.ModuleName, name, tsz, size);
215 if (compiland)
217 p = vector_add(&compiland->vchildren, &module->pool);
218 *p = &sym->symt;
221 return sym;
224 struct symt_function* symt_new_function(struct module* module,
225 struct symt_compiland* compiland,
226 const char* name,
227 unsigned long addr, unsigned long size,
228 struct symt* sig_type)
230 struct symt_function* sym;
231 struct symt** p;
233 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
234 module->module.ModuleName, name, addr, addr + size - 1);
236 assert(!sig_type || sig_type->tag == SymTagFunctionType);
237 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
239 sym->symt.tag = SymTagFunction;
240 sym->hash_elt.name = pool_strdup(&module->pool, name);
241 hash_table_add(&module->ht_symbols, &sym->hash_elt);
242 module->sortlist_valid = FALSE;
243 sym->container = &compiland->symt;
244 sym->address = addr;
245 sym->type = sig_type;
246 sym->size = size;
247 vector_init(&sym->vlines, sizeof(struct line_info), 64);
248 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
249 if (compiland)
251 p = vector_add(&compiland->vchildren, &module->pool);
252 *p = &sym->symt;
255 return sym;
258 void symt_add_func_line(struct module* module, struct symt_function* func,
259 unsigned source_idx, int line_num, unsigned long offset)
261 struct line_info* dli;
262 BOOL last_matches = FALSE;
264 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
266 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
267 func, func->hash_elt.name, offset,
268 source_get(module, source_idx), line_num);
270 assert(func->symt.tag == SymTagFunction);
272 dli = NULL;
273 while ((dli = vector_iter_down(&func->vlines, dli)))
275 if (dli->is_source_file)
277 last_matches = (source_idx == dli->u.source_file);
278 break;
282 if (!last_matches)
284 /* we shouldn't have line changes on first line of function */
285 dli = vector_add(&func->vlines, &module->pool);
286 dli->is_source_file = 1;
287 dli->is_first = dli->is_last = 0;
288 dli->line_number = 0;
289 dli->u.source_file = source_idx;
291 dli = vector_add(&func->vlines, &module->pool);
292 dli->is_source_file = 0;
293 dli->is_first = dli->is_last = 0;
294 dli->line_number = line_num;
295 dli->u.pc_offset = func->address + offset;
298 struct symt_data* symt_add_func_local(struct module* module,
299 struct symt_function* func,
300 int regno, int offset,
301 struct symt_block* block,
302 struct symt* type, const char* name)
304 struct symt_data* locsym;
305 struct symt** p;
307 assert(func);
308 assert(func->symt.tag == SymTagFunction);
310 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
311 module->module.ModuleName, func->hash_elt.name,
312 name, type);
313 locsym = pool_alloc(&module->pool, sizeof(*locsym));
314 locsym->symt.tag = SymTagData;
315 locsym->hash_elt.name = pool_strdup(&module->pool, name);
316 locsym->hash_elt.next = NULL;
317 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
318 locsym->container = &block->symt;
319 locsym->type = type;
320 if (regno)
322 locsym->u.s.reg_id = regno;
323 locsym->u.s.offset = 0;
324 locsym->u.s.length = 0;
326 else
328 locsym->u.s.reg_id = 0;
329 locsym->u.s.offset = offset * 8;
330 locsym->u.s.length = 0;
332 if (block)
333 p = vector_add(&block->vchildren, &module->pool);
334 else
335 p = vector_add(&func->vchildren, &module->pool);
336 *p = &locsym->symt;
337 return locsym;
340 struct symt_block* symt_open_func_block(struct module* module,
341 struct symt_function* func,
342 struct symt_block* parent_block,
343 unsigned pc, unsigned len)
345 struct symt_block* block;
346 struct symt** p;
348 assert(func);
349 assert(func->symt.tag == SymTagFunction);
351 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
352 block = pool_alloc(&module->pool, sizeof(*block));
353 block->symt.tag = SymTagBlock;
354 block->address = func->address + pc;
355 block->size = len;
356 block->container = parent_block ? &parent_block->symt : &func->symt;
357 vector_init(&block->vchildren, sizeof(struct symt*), 4);
358 if (parent_block)
359 p = vector_add(&parent_block->vchildren, &module->pool);
360 else
361 p = vector_add(&func->vchildren, &module->pool);
362 *p = &block->symt;
364 return block;
367 struct symt_block* symt_close_func_block(struct module* module,
368 struct symt_function* func,
369 struct symt_block* block, unsigned pc)
371 assert(func->symt.tag == SymTagFunction);
373 if (pc) block->size = func->address + pc - block->address;
374 return (block->container->tag == SymTagBlock) ?
375 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
378 struct symt_function_point* symt_add_function_point(struct module* module,
379 struct symt_function* func,
380 enum SymTagEnum point,
381 unsigned offset, const char* name)
383 struct symt_function_point* sym;
384 struct symt** p;
386 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
388 sym->symt.tag = point;
389 sym->parent = func;
390 sym->offset = offset;
391 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
392 p = vector_add(&func->vchildren, &module->pool);
393 *p = &sym->symt;
395 return sym;
398 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
400 unsigned len;
401 struct line_info* dli;
403 assert(func);
404 /* We aren't adding any more locals or line numbers to this function.
405 * Free any spare memory that we might have allocated.
407 assert(func->symt.tag == SymTagFunction);
409 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
410 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
412 len = vector_length(&func->vlines);
413 if (len--)
415 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
416 dli = vector_at(&func->vlines, len); dli->is_last = 1;
418 return TRUE;
421 struct symt_thunk* symt_new_thunk(struct module* module,
422 struct symt_compiland* compiland,
423 const char* name, THUNK_ORDINAL ord,
424 unsigned long addr, unsigned long size)
426 struct symt_thunk* sym;
428 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
429 module->module.ModuleName, name, addr, addr + size - 1);
431 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
433 sym->symt.tag = SymTagThunk;
434 sym->hash_elt.name = pool_strdup(&module->pool, name);
435 hash_table_add(&module->ht_symbols, &sym->hash_elt);
436 module->sortlist_valid = FALSE;
437 sym->container = &compiland->symt;
438 sym->address = addr;
439 sym->size = size;
440 sym->ordinal = ord;
441 if (compiland)
443 struct symt** p;
444 p = vector_add(&compiland->vchildren, &module->pool);
445 *p = &sym->symt;
448 return sym;
451 /* expect sym_info->MaxNameLen to be set before being called */
452 static void symt_fill_sym_info(const struct module* module,
453 const struct symt* sym, SYMBOL_INFO* sym_info)
455 const char* name;
457 sym_info->TypeIndex = (DWORD)sym;
458 sym_info->info = 0; /* TBD */
459 symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
460 sym_info->ModBase = module->module.BaseOfImage;
461 sym_info->Flags = 0;
462 switch (sym->tag)
464 case SymTagData:
466 const struct symt_data* data = (const struct symt_data*)sym;
467 switch (data->kind)
469 case DataIsLocal:
470 case DataIsParam:
471 if (data->u.s.reg_id)
473 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGISTER;
474 sym_info->Register = data->u.s.reg_id;
475 sym_info->Address = 0;
477 else
479 if (data->u.s.offset < 0)
480 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
481 else
482 sym_info->Flags |= SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
483 /* FIXME: needed ? moreover, it's i386 dependent !!! */
484 sym_info->Register = CV_REG_EBP;
485 sym_info->Address = data->u.s.offset;
487 break;
488 case DataIsGlobal:
489 case DataIsFileStatic:
490 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
491 sym_info->Register = 0;
492 break;
493 case DataIsConstant:
494 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
495 switch (data->u.value.n1.n2.vt)
497 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
498 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
499 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
500 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
501 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
502 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
503 default:
504 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
506 break;
507 default:
508 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
511 break;
512 case SymTagPublicSymbol:
513 sym_info->Flags |= SYMFLAG_EXPORT;
514 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
515 break;
516 case SymTagFunction:
517 sym_info->Flags |= SYMFLAG_FUNCTION;
518 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
519 break;
520 case SymTagThunk:
521 sym_info->Flags |= SYMFLAG_THUNK;
522 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
523 break;
524 default:
525 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
526 sym_info->Register = 0;
527 break;
529 sym_info->Scope = 0; /* FIXME */
530 sym_info->Tag = sym->tag;
531 name = symt_get_name(sym);
532 sym_info->NameLen = strlen(name) + 1;
533 if (sym_info->MaxNameLen)
535 strncpy(sym_info->Name, name, min(sym_info->NameLen, sym_info->MaxNameLen));
536 sym_info->Name[sym_info->MaxNameLen - 1] = '\0';
538 TRACE_(dbghelp_symt)("%p => %s %lu %lx\n",
539 sym, sym_info->Name, sym_info->Size, sym_info->Address);
542 static BOOL symt_enum_module(struct module* module, regex_t* regex,
543 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
545 char buffer[sizeof(SYMBOL_INFO) + 256];
546 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
547 void* ptr;
548 struct symt_ht* sym = NULL;
549 struct hash_table_iter hti;
551 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
552 while ((ptr = hash_table_iter_up(&hti)))
554 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
555 if (sym->hash_elt.name &&
556 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
558 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
559 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
560 symt_fill_sym_info(module, &sym->symt, sym_info);
561 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
564 return FALSE;
567 /***********************************************************************
568 * resort_symbols
570 * Rebuild sorted list of symbols for a module.
572 static BOOL resort_symbols(struct module* module)
574 int nsym = 0;
575 void* ptr;
576 struct symt_ht* sym;
577 struct hash_table_iter hti;
579 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
580 while ((ptr = hash_table_iter_up(&hti)))
581 nsym++;
583 if (!(module->module.NumSyms = nsym)) return FALSE;
585 if (module->addr_sorttab)
586 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
587 module->addr_sorttab,
588 nsym * sizeof(struct symt_ht*));
589 else
590 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
591 nsym * sizeof(struct symt_ht*));
592 if (!module->addr_sorttab) return FALSE;
594 nsym = 0;
595 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
596 while ((ptr = hash_table_iter_up(&hti)))
598 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
599 assert(sym);
600 module->addr_sorttab[nsym++] = sym;
603 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
604 return module->sortlist_valid = TRUE;
607 /* assume addr is in module */
608 int symt_find_nearest(struct module* module, DWORD addr)
610 int mid, high, low;
611 DWORD ref_addr, ref_size;
613 if (!module->sortlist_valid || !module->addr_sorttab)
615 if (!resort_symbols(module)) return -1;
619 * Binary search to find closest symbol.
621 low = 0;
622 high = module->module.NumSyms;
624 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
625 if (addr < ref_addr) return -1;
626 if (high)
628 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
629 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
630 ref_size = 0x1000; /* arbitrary value */
631 if (addr >= ref_addr + ref_size) return -1;
634 while (high > low + 1)
636 mid = (high + low) / 2;
637 if (cmp_sorttab_addr(module, mid, addr) < 0)
638 low = mid;
639 else
640 high = mid;
642 if (low != high && high != module->module.NumSyms &&
643 cmp_sorttab_addr(module, high, addr) <= 0)
644 low = high;
646 /* If found symbol is a public symbol, check if there are any other entries that
647 * might also have the same address, but would get better information
649 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
651 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
652 if (low > 0 &&
653 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
654 !cmp_sorttab_addr(module, low - 1, ref_addr))
655 low--;
656 else if (low < module->module.NumSyms - 1 &&
657 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
658 !cmp_sorttab_addr(module, low + 1, ref_addr))
659 low++;
661 /* finally check that we fit into the found symbol */
662 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
663 if (addr < ref_addr) return -1;
664 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
665 ref_size = 0x1000; /* arbitrary value */
666 if (addr >= ref_addr + ref_size) return -1;
668 return low;
671 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
672 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
673 PVOID user, SYMBOL_INFO* sym_info,
674 struct vector* v)
676 struct symt** plsym = NULL;
677 struct symt* lsym = NULL;
678 DWORD pc = pcs->ctx_frame.InstructionOffset;
680 while ((plsym = vector_iter_up(v, plsym)))
682 lsym = *plsym;
683 switch (lsym->tag)
685 case SymTagBlock:
687 struct symt_block* block = (struct symt_block*)lsym;
688 if (pc < block->address || block->address + block->size <= pc)
689 continue;
690 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
691 sym_info, &block->vchildren))
692 return FALSE;
694 break;
695 case SymTagData:
696 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
698 symt_fill_sym_info(module, lsym, sym_info);
699 if (!cb(sym_info, sym_info->Size, user))
700 return FALSE;
702 break;
703 case SymTagLabel:
704 case SymTagFuncDebugStart:
705 case SymTagFuncDebugEnd:
706 break;
707 default:
708 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
709 assert(0);
712 return TRUE;
715 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
716 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
717 PVOID UserContext)
719 struct module* module;
720 struct symt_ht* sym;
721 char buffer[sizeof(SYMBOL_INFO) + 256];
722 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
723 DWORD pc = pcs->ctx_frame.InstructionOffset;
724 int idx;
726 sym_info->SizeOfStruct = sizeof(*sym_info);
727 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
729 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
730 if (!(module = module_get_debug(pcs, module))) return FALSE;
731 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
733 sym = module->addr_sorttab[idx];
734 if (sym->symt.tag == SymTagFunction)
736 BOOL ret;
737 regex_t preg;
739 compile_regex(mask ? mask : "*", -1, &preg);
740 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
741 UserContext, sym_info,
742 &((struct symt_function*)sym)->vchildren);
743 regfree(&preg);
744 return ret;
747 symt_fill_sym_info(module, &sym->symt, sym_info);
748 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
751 /******************************************************************
752 * SymEnumSymbols (DBGHELP.@)
754 * cases BaseOfDll = 0
755 * !foo fails always (despite what MSDN states)
756 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
757 * no ! in Mask, lookup in local Context
758 * cases BaseOfDll != 0
759 * !foo fails always (despite what MSDN states)
760 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
762 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG BaseOfDll, PCSTR Mask,
763 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
764 PVOID UserContext)
766 struct process* pcs = process_find_by_handle(hProcess);
767 struct module* module;
768 struct module* dbg_module;
769 const char* bang;
770 regex_t mod_regex, sym_regex;
772 TRACE("(%p %08lx %s %p %p)\n",
773 hProcess, BaseOfDll, debugstr_a(Mask), EnumSymbolsCallback, UserContext);
775 if (!pcs) return FALSE;
777 if (BaseOfDll == 0)
779 /* do local variables ? */
780 if (!Mask || !(bang = strchr(Mask, '!')))
781 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
783 if (bang == Mask) return FALSE;
785 compile_regex(Mask, bang - Mask, &mod_regex);
786 compile_regex(bang + 1, -1, &sym_regex);
788 for (module = pcs->lmodules; module; module = module->next)
790 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
792 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
793 symt_enum_module(dbg_module, &sym_regex,
794 EnumSymbolsCallback, UserContext))
795 break;
798 /* not found in PE modules, retry on the ELF ones
800 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
802 for (module = pcs->lmodules; module; module = module->next)
804 if (module->type == DMT_ELF &&
805 !module_get_containee(pcs, module) &&
806 (dbg_module = module_get_debug(pcs, module)))
808 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
809 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
810 break;
814 regfree(&mod_regex);
815 regfree(&sym_regex);
816 return TRUE;
818 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
819 if (!(module = module_get_debug(pcs, module)))
820 return FALSE;
822 /* we always ignore module name from Mask when BaseOfDll is defined */
823 if (Mask && (bang = strchr(Mask, '!')))
825 if (bang == Mask) return FALSE;
826 Mask = bang + 1;
829 compile_regex(Mask ? Mask : "*", -1, &sym_regex);
830 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
831 regfree(&sym_regex);
833 return TRUE;
836 struct sym_enumerate
838 void* ctx;
839 PSYM_ENUMSYMBOLS_CALLBACK cb;
842 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
844 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
845 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
848 /***********************************************************************
849 * SymEnumerateSymbols (DBGHELP.@)
851 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
852 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
853 PVOID UserContext)
855 struct sym_enumerate se;
857 se.ctx = UserContext;
858 se.cb = EnumSymbolsCallback;
860 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
863 /******************************************************************
864 * SymFromAddr (DBGHELP.@)
867 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD Address,
868 DWORD* Displacement, PSYMBOL_INFO Symbol)
870 struct process* pcs = process_find_by_handle(hProcess);
871 struct module* module;
872 struct symt_ht* sym;
873 int idx;
875 if (!pcs) return FALSE;
876 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
877 if (!(module = module_get_debug(pcs, module))) return FALSE;
878 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
880 sym = module->addr_sorttab[idx];
882 symt_fill_sym_info(module, &sym->symt, Symbol);
883 if (Displacement) *Displacement = Address - Symbol->Address;
884 return TRUE;
887 /******************************************************************
888 * SymGetSymFromAddr (DBGHELP.@)
891 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
892 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
894 char buffer[sizeof(SYMBOL_INFO) + 256];
895 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
896 size_t len;
898 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
899 si->SizeOfStruct = sizeof(*si);
900 si->MaxNameLen = 256;
901 if (!SymFromAddr(hProcess, Address, Displacement, si))
902 return FALSE;
904 Symbol->Address = si->Address;
905 Symbol->Size = si->Size;
906 Symbol->Flags = si->Flags;
907 len = min(Symbol->MaxNameLength, si->MaxNameLen);
908 strncpy(Symbol->Name, si->Name, len);
909 Symbol->Name[len - 1] = '\0';
910 return TRUE;
913 /******************************************************************
914 * SymFromName (DBGHELP.@)
917 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
919 struct process* pcs = process_find_by_handle(hProcess);
920 struct module* module;
921 struct hash_table_iter hti;
922 void* ptr;
923 struct symt_ht* sym = NULL;
924 const char* name;
926 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
927 if (!pcs) return FALSE;
928 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
929 name = strchr(Name, '!');
930 if (name)
932 char tmp[128];
933 assert(name - Name < sizeof(tmp));
934 memcpy(tmp, Name, name - Name);
935 tmp[name - Name] = '\0';
936 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
937 if (!module) return FALSE;
938 Name = (char*)(name + 1);
940 else module = pcs->lmodules;
942 /* FIXME: Name could be made out of a regular expression */
943 for (; module; module = (name) ? NULL : module->next)
945 if (module->module.SymType == SymNone) continue;
946 if (module->module.SymType == SymDeferred)
948 struct module* xmodule = module_get_debug(pcs, module);
949 if (!xmodule || xmodule != module) continue;
951 hash_table_iter_init(&module->ht_symbols, &hti, Name);
952 while ((ptr = hash_table_iter_up(&hti)))
954 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
956 if (!strcmp(sym->hash_elt.name, Name))
958 symt_fill_sym_info(module, &sym->symt, Symbol);
959 return TRUE;
963 return FALSE;
966 /***********************************************************************
967 * SymGetSymFromName (DBGHELP.@)
969 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
971 char buffer[sizeof(SYMBOL_INFO) + 256];
972 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
973 size_t len;
975 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
976 si->SizeOfStruct = sizeof(*si);
977 si->MaxNameLen = 256;
978 if (!SymFromName(hProcess, Name, si)) return FALSE;
980 Symbol->Address = si->Address;
981 Symbol->Size = si->Size;
982 Symbol->Flags = si->Flags;
983 len = min(Symbol->MaxNameLength, si->MaxNameLen);
984 strncpy(Symbol->Name, si->Name, len);
985 Symbol->Name[len - 1] = '\0';
986 return TRUE;
989 /******************************************************************
990 * sym_fill_func_line_info
992 * fills information about a file
994 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
995 DWORD addr, IMAGEHLP_LINE* line)
997 struct line_info* dli = NULL;
998 BOOL found = FALSE;
1000 assert(func->symt.tag == SymTagFunction);
1002 while ((dli = vector_iter_down(&func->vlines, dli)))
1004 if (!dli->is_source_file)
1006 if (found || dli->u.pc_offset > addr) continue;
1007 line->LineNumber = dli->line_number;
1008 line->Address = dli->u.pc_offset;
1009 line->Key = dli;
1010 found = TRUE;
1011 continue;
1013 if (found)
1015 line->FileName = (char*)source_get(module, dli->u.source_file);
1016 return TRUE;
1019 return FALSE;
1022 /***********************************************************************
1023 * SymGetSymNext (DBGHELP.@)
1025 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1027 /* algo:
1028 * get module from Symbol.Address
1029 * get index in module.addr_sorttab of Symbol.Address
1030 * increment index
1031 * if out of module bounds, move to next module in process address space
1033 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1034 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1035 return FALSE;
1038 /***********************************************************************
1039 * SymGetSymPrev (DBGHELP.@)
1042 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1044 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1046 return FALSE;
1049 /******************************************************************
1050 * SymGetLineFromAddr (DBGHELP.@)
1053 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1054 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1056 struct process* pcs = process_find_by_handle(hProcess);
1057 struct module* module;
1058 int idx;
1060 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1062 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1064 if (!pcs) return FALSE;
1065 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1066 if (!(module = module_get_debug(pcs, module))) return FALSE;
1067 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1069 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1070 if (!symt_fill_func_line_info(module,
1071 (struct symt_function*)module->addr_sorttab[idx],
1072 dwAddr, Line)) return FALSE;
1073 if (pdwDisplacement) *pdwDisplacement = dwAddr - Line->Address;
1074 return TRUE;
1077 /******************************************************************
1078 * SymGetLinePrev (DBGHELP.@)
1081 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1083 struct process* pcs = process_find_by_handle(hProcess);
1084 struct module* module;
1085 struct line_info* li;
1086 BOOL in_search = FALSE;
1088 TRACE("(%p %p)\n", hProcess, Line);
1090 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1092 if (!pcs) return FALSE;
1093 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1094 if (!(module = module_get_debug(pcs, module))) return FALSE;
1096 if (Line->Key == 0) return FALSE;
1097 li = (struct line_info*)Line->Key;
1098 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1099 * element we have to go back until we find the prev one to get the real
1100 * source file name for the DLIT_OFFSET element just before
1101 * the first DLIT_SOURCEFILE
1103 while (!li->is_first)
1105 li--;
1106 if (!li->is_source_file)
1108 Line->LineNumber = li->line_number;
1109 Line->Address = li->u.pc_offset;
1110 Line->Key = li;
1111 if (!in_search) return TRUE;
1113 else
1115 if (in_search)
1117 Line->FileName = (char*)source_get(module, li->u.source_file);
1118 return TRUE;
1120 in_search = TRUE;
1123 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1124 return FALSE;
1127 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1129 struct line_info* li;
1131 if (line->Key == 0) return FALSE;
1132 li = (struct line_info*)line->Key;
1133 while (!li->is_last)
1135 li++;
1136 if (!li->is_source_file)
1138 line->LineNumber = li->line_number;
1139 line->Address = li->u.pc_offset;
1140 line->Key = li;
1141 return TRUE;
1143 line->FileName = (char*)source_get(module, li->u.source_file);
1145 return FALSE;
1148 /******************************************************************
1149 * SymGetLineNext (DBGHELP.@)
1152 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1154 struct process* pcs = process_find_by_handle(hProcess);
1155 struct module* module;
1157 TRACE("(%p %p)\n", hProcess, Line);
1159 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1160 if (!pcs) return FALSE;
1161 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1162 if (!(module = module_get_debug(pcs, module))) return FALSE;
1164 if (symt_get_func_line_next(module, Line)) return TRUE;
1165 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1166 return FALSE;
1169 /***********************************************************************
1170 * SymFunctionTableAccess (DBGHELP.@)
1172 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1174 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1175 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1176 return FALSE;
1179 /***********************************************************************
1180 * SymUnDName (DBGHELP.@)
1182 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1184 FIXME("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1185 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1186 UNDNAME_COMPLETE);
1189 /***********************************************************************
1190 * UnDecorateSymbolName (DBGHELP.@)
1192 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1193 DWORD UndecoratedLength, DWORD Flags)
1195 FIXME("(%s, %p, %ld, 0x%08lx): stub\n",
1196 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1198 strncpy(UnDecoratedName, DecoratedName, UndecoratedLength);
1199 UnDecoratedName[UndecoratedLength - 1] = '\0';
1200 return TRUE;