Assorted spelling fixes.
[wine] / dlls / dbghelp / symbol.c
1 /*
2  * File symbol.c - management of symbols (lexical tree)
3  *
4  * Copyright (C) 1993, Eric Youngdale.
5  *               2004, Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  */
21
22
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <assert.h>
32 #include <regex.h>
33 #include "wine/debug.h"
34
35 #include "dbghelp_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
38 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symtype);
39
40 #define DLIT_OFFSET     0x00
41 #define DLIT_FIRST      0x01
42 #define DLIT_LAST       0x02
43 #define DLIT_SOURCEFILE 0x04
44
45 struct line_info
46 {
47     unsigned long               cookie : 3,
48                                 line_number;
49     union
50     {
51         unsigned long               pc_offset;
52         unsigned                    source_file;
53     } u;
54 };
55
56 inline static int cmp_addr(DWORD a1, DWORD a2)
57 {
58     if (a1 > a2) return 1;
59     if (a1 < a2) return -1;
60     return 0;
61 }
62
63 inline static int cmp_sorttab_addr(const struct module* module, int idx, DWORD addr)
64 {
65     DWORD       ref;
66
67     symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
68     return cmp_addr(ref, addr);
69 }
70
71 int symt_cmp_addr(const void* p1, const void* p2)
72 {
73     struct symt*        sym1 = *(struct symt**)p1;
74     struct symt*        sym2 = *(struct symt**)p2;
75     DWORD               a1, a2;
76
77     symt_get_info(sym1, TI_GET_ADDRESS, &a1);
78     symt_get_info(sym2, TI_GET_ADDRESS, &a2);
79     return cmp_addr(a1, a2);
80 }
81
82 static inline void re_append(char** mask, unsigned* len, char ch)
83 {
84     *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
85     (*mask)[*len - 2] = ch;
86 }
87
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 -
96  */
97 static void compile_regex(const char* str, regex_t* re)
98 {
99     char*       mask = HeapAlloc(GetProcessHeap(), 0, 1);
100     unsigned    len = 1;
101     BOOL        in_escape = FALSE;
102
103     re_append(&mask, &len, '^');
104     while (*str)
105     {
106         /* FIXME: this shouldn't be valid on '-' */
107         if (in_escape)
108         {
109             re_append(&mask, &len, '\\');
110             re_append(&mask, &len, *str);
111             in_escape = FALSE;
112         }
113         else switch (*str)
114         {
115         case '\\': in_escape = TRUE; break;
116         case '*':  re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
117         case '?':  re_append(&mask, &len, '.'); break;
118         case '#':  re_append(&mask, &len, '*'); break;
119         /* escape some valid characters in dbghelp reg exp:s */
120         case '$':  re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
121         /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
122         default:   re_append(&mask, &len, *str); break;
123         }
124         str++;
125     }
126     if (in_escape)
127     {
128         re_append(&mask, &len, '\\');
129         re_append(&mask, &len, '\\');
130     }
131     re_append(&mask, &len, '$');
132     mask[len - 1] = '\0';
133     regcomp(re, mask, REG_NOSUB);
134     HeapFree(GetProcessHeap(), 0, mask);
135 }
136
137 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
138 {
139     struct symt_compiland*    sym;
140
141     TRACE_(dbghelp_symtype)("Adding compiland symbol %s:%s\n", 
142                             module->module.ModuleName, name);
143     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
144     {
145         sym->symt.tag = SymTagCompiland;
146         sym->source   = source_new(module, name);
147         vector_init(&sym->vchildren, sizeof(struct symt*), 32);
148     }
149     return sym;
150 }
151
152 struct symt_public* symt_new_public(struct module* module, 
153                                     struct symt_compiland* compiland,
154                                     const char* name,
155                                     unsigned long address, unsigned size,
156                                     BOOL in_code, BOOL is_func)
157 {
158     struct symt_public* sym;
159     struct symt**       p;
160
161     TRACE_(dbghelp_symtype)("Adding public symbol %s:%s @%lx\n", 
162                             module->module.ModuleName, name, address);
163     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
164     {
165         sym->symt.tag      = SymTagPublicSymbol;
166         sym->hash_elt.name = pool_strdup(&module->pool, name);
167         hash_table_add(&module->ht_symbols, &sym->hash_elt);
168         module->sortlist_valid = FALSE;
169         sym->container     = compiland ? &compiland->symt : NULL;
170         sym->address       = address;
171         sym->size          = size;
172         sym->in_code       = in_code;
173         sym->is_function   = is_func;
174         if (compiland)
175         {
176             p = vector_add(&compiland->vchildren, &module->pool);
177             *p = &sym->symt;
178         }
179     }
180     return sym;
181 }
182
183 struct symt_data* symt_new_global_variable(struct module* module, 
184                                            struct symt_compiland* compiland, 
185                                            const char* name, unsigned is_static,
186                                            unsigned long addr, unsigned long size,
187                                            struct symt* type)
188 {
189     struct symt_data*   sym;
190     struct symt**       p;
191
192     TRACE_(dbghelp_symtype)("Adding global symbol %s:%s @%lx %p\n", 
193                             module->module.ModuleName, name, addr, type);
194     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
195     {
196         sym->symt.tag      = SymTagData;
197         sym->hash_elt.name = pool_strdup(&module->pool, name);
198         hash_table_add(&module->ht_symbols, &sym->hash_elt);
199         module->sortlist_valid = FALSE;
200         sym->kind          = is_static ? DataIsFileStatic : DataIsGlobal;
201         sym->container     = compiland ? &compiland->symt : NULL;
202         sym->type          = type;
203         sym->location      = LocIsStatic; /* FIXME */
204         sym->u.address     = addr;
205         if (compiland)
206         {
207             p = vector_add(&compiland->vchildren, &module->pool);
208             *p = &sym->symt;
209         }
210     }
211     return sym;
212 }
213
214 struct symt_function* symt_new_function(struct module* module, 
215                                         struct symt_compiland* compiland, 
216                                         const char* name,
217                                         unsigned long addr, unsigned long size,
218                                         struct symt* sig_type)
219 {
220     struct symt_function*       sym;
221     struct symt**               p;
222
223     TRACE_(dbghelp_symtype)("Adding global function %s:%s @%lx-%lx\n", 
224                             module->module.ModuleName, name, addr, addr + size - 1);
225
226     assert(!sig_type || sig_type->tag == SymTagFunctionType);
227     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
228     {
229         sym->symt.tag  = SymTagFunction;
230         sym->hash_elt.name = pool_strdup(&module->pool, name);
231         hash_table_add(&module->ht_symbols, &sym->hash_elt);
232         module->sortlist_valid = FALSE;
233         sym->container = &compiland->symt;
234         sym->addr      = addr;
235         sym->type      = sig_type;
236         sym->size      = size;
237         sym->addr      = addr;
238         vector_init(&sym->vlines,  sizeof(struct line_info), 64);
239         vector_init(&sym->vchildren, sizeof(struct symt*), 8);
240         if (compiland)
241         {
242             p = vector_add(&compiland->vchildren, &module->pool);
243             *p = &sym->symt;
244         }
245     }
246     return sym;
247 }
248
249 void symt_add_func_line(struct module* module, struct symt_function* func,
250                         unsigned source_idx, int line_num, unsigned long offset)
251 {
252     struct line_info*   dli;
253     BOOL                last_matches = FALSE;
254
255     if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
256
257     TRACE_(dbghelp_symtype)("(%p)%s:%lx %s:%u\n", 
258                             func, func->hash_elt.name, offset, 
259                             source_get(module, source_idx), line_num);
260
261     assert(func->symt.tag == SymTagFunction);
262
263     dli = NULL;
264     while ((dli = vector_iter_down(&func->vlines, dli)))
265     {
266         if (dli->cookie & DLIT_SOURCEFILE)
267         {
268             last_matches = (source_idx == dli->u.source_file);
269             break;
270         }
271     }
272
273     if (!last_matches)
274     {
275         /* we shouldn't have line changes on first line of function */
276         dli = vector_add(&func->vlines, &module->pool);
277         dli->cookie        = DLIT_SOURCEFILE;
278         dli->line_number   = 0;
279         dli->u.source_file = source_idx;
280     }
281     dli = vector_add(&func->vlines, &module->pool);
282     dli->cookie      = DLIT_OFFSET;
283     dli->line_number = line_num;
284     dli->u.pc_offset = func->addr + offset;
285 }
286
287 struct symt_data* symt_add_func_local(struct module* module, 
288                                       struct symt_function* func, 
289                                       int regno, int offset, 
290                                       struct symt_block* block, 
291                                       struct symt* type, const char* name)
292 {
293     struct symt_data*   locsym;
294     struct symt**       p;
295
296     assert(func);
297     assert(func->symt.tag == SymTagFunction);
298
299     TRACE_(dbghelp_symtype)("Adding local symbol (%s:%s): %s %p\n", 
300                             module->module.ModuleName, func->hash_elt.name, 
301                             name, type);
302     locsym = pool_alloc(&module->pool, sizeof(*locsym));
303     locsym->symt.tag      = SymTagData;
304     locsym->hash_elt.name = pool_strdup(&module->pool, name);
305     locsym->hash_elt.next = NULL;
306     locsym->kind          = (offset < 0) ? DataIsParam : DataIsLocal;
307     locsym->container     = &block->symt;
308     locsym->type          = type;
309     if (regno)
310     {
311         locsym->location = LocIsEnregistered;
312         locsym->u.reg_id = regno;
313     }
314     else
315     {
316         locsym->location = LocIsRegRel;
317         locsym->u.reg_id = CV_REG_EBP;
318         locsym->u.offset = offset;
319     }
320     if (block)
321         p = vector_add(&block->vchildren, &module->pool);
322     else
323         p = vector_add(&func->vchildren, &module->pool);
324     *p = &locsym->symt;
325     return locsym;
326 }
327
328 struct symt_block* symt_open_func_block(struct module* module, 
329                                         struct symt_function* func,
330                                         struct symt_block* parent_block, 
331                                         unsigned pc, unsigned len)
332 {
333     struct symt_block*  block;
334     struct symt**       p;
335
336     assert(func);
337     assert(func->symt.tag == SymTagFunction);
338
339     assert(!parent_block || parent_block->symt.tag == SymTagBlock);
340     block = pool_alloc(&module->pool, sizeof(*block));
341     block->symt.tag = SymTagBlock;
342     block->address  = func->addr + pc;
343     block->size     = len;
344     block->container = parent_block ? &parent_block->symt : &func->symt;
345     vector_init(&block->vchildren, sizeof(struct symt*), 4);
346     if (parent_block)
347         p = vector_add(&parent_block->vchildren, &module->pool);
348     else
349         p = vector_add(&func->vchildren, &module->pool);
350     *p = &block->symt;
351
352     return block;
353 }
354
355 struct symt_block* symt_close_func_block(struct module* module, 
356                                          struct symt_function* func,
357                                          struct symt_block* block, unsigned pc)
358 {
359     assert(func->symt.tag == SymTagFunction);
360
361     if (pc) block->size = func->addr + pc - block->address;
362     return (block->container->tag == SymTagBlock) ? 
363         GET_ENTRY(block->container, struct symt_block, symt) : NULL;
364 }
365
366 struct symt_function_point* symt_add_function_point(struct module* module, 
367                                                     struct symt_function* func,
368                                                     enum SymTagEnum point, 
369                                                     unsigned offset, const char* name)
370 {
371     struct symt_function_point* sym;
372     struct symt**               p;
373
374     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
375     {
376         sym->symt.tag = point;
377         sym->parent   = func;
378         sym->offset   = offset;
379         sym->name     = name ? pool_strdup(&module->pool, name) : NULL;
380         p = vector_add(&func->vchildren, &module->pool);
381         *p = &sym->symt;
382     }
383     return sym;
384 }
385
386 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
387 {
388     unsigned            len;
389     struct line_info*   dli;
390
391     assert(func);
392     /* We aren't adding any more locals or line numbers to this function.
393      * Free any spare memory that we might have allocated.
394      */
395     assert(func->symt.tag == SymTagFunction);
396
397 /* EPP     vector_pool_normalize(&func->vlines,    &module->pool); */
398 /* EPP     vector_pool_normalize(&func->vchildren, &module->pool); */
399
400     len = vector_length(&func->vlines);
401     if (len--)
402     {
403         dli = vector_at(&func->vlines,   0);  dli->cookie |= DLIT_FIRST;
404         dli = vector_at(&func->vlines, len);  dli->cookie |= DLIT_LAST;
405     }
406     return TRUE;
407 }
408
409 /* expect sym_info->MaxNameLen to be set before being called */
410 static void symt_fill_sym_info(const struct module* module, 
411                                const struct symt* sym, SYMBOL_INFO* sym_info)
412 {
413     const char* name;
414
415     sym_info->TypeIndex = (DWORD)sym;
416     sym_info->info = 0; /* TBD */
417     symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
418     sym_info->ModBase = module->module.BaseOfImage;
419     sym_info->Flags = 0;
420     switch (sym->tag)
421     {
422     case SymTagData:
423         {
424             struct symt_data*  data = (struct symt_data*)sym;
425             switch (data->location)
426             {
427             case LocIsEnregistered:
428                 sym_info->Flags |= SYMFLAG_REGISTER;
429                 sym_info->Register = data->u.reg_id;
430                 sym_info->Address = 0;
431                 break;
432             case LocIsRegRel:
433                 sym_info->Flags |= 
434                     ((data->u.offset < 0) ? SYMFLAG_LOCAL : SYMFLAG_PARAMETER) |
435                     SYMFLAG_FRAMEREL;
436                 sym_info->Register = data->u.reg_id;
437                 sym_info->Address = data->u.offset;
438                 break;
439             case LocIsStatic:
440                 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
441                 sym_info->Register = 0;
442                 break;
443             case LocIsConstant:
444                 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
445                 switch (data->u.value.n1.n2.vt)
446                 {
447                 case VT_I4:  sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
448                 case VT_I2:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
449                 case VT_I1:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
450                 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
451                 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
452                 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
453                 default:        
454                     FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
455                 }
456                 break;
457             default:
458                 FIXME("Unhandled loc (%u) in sym data\n", data->location);
459             }
460         }
461         break;
462     case SymTagPublicSymbol:
463         sym_info->Flags |= SYMFLAG_EXPORT;
464         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
465         break;
466     case SymTagFunction:
467         sym_info->Flags |= SYMFLAG_FUNCTION;
468         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
469         break;
470     default:
471         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
472         sym_info->Register = 0;
473         break;
474     }
475     sym_info->Scope = 0; /* FIXME */
476     sym_info->Tag = sym->tag;
477     name = symt_get_name(sym);
478     sym_info->NameLen = strlen(name) + 1;
479     if (sym_info->MaxNameLen)
480     {
481         strncpy(sym_info->Name, name, min(sym_info->NameLen, sym_info->MaxNameLen));
482         sym_info->Name[sym_info->MaxNameLen - 1] = '\0';
483     }
484     TRACE_(dbghelp_symtype)("%p => %s %lu %lx\n",
485                             sym, sym_info->Name, sym_info->Size, sym_info->Address);
486 }
487
488 static BOOL symt_enum_module(struct module* module, const char* mask,
489                              PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
490 {
491     char                        buffer[sizeof(SYMBOL_INFO) + 256];
492     SYMBOL_INFO*                sym_info = (SYMBOL_INFO*)buffer;
493     void*                       ptr;
494     struct symt_ht*             sym = NULL;
495     struct hash_table_iter      hti;
496     regex_t                     preg;
497
498     assert(mask);
499     assert(mask[0] != '!');
500     compile_regex(mask, &preg);
501     hash_table_iter_init(&module->ht_symbols, &hti, NULL);
502     while ((ptr = hash_table_iter_up(&hti)))
503     {
504         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
505         /* FIXME: this is not true, we should only drop the public
506          * symbol iff no other one is found
507          */
508         if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
509             sym->symt.tag == SymTagPublicSymbol) continue;
510
511         if (sym->hash_elt.name &&
512             regexec(&preg, sym->hash_elt.name, 0, NULL, 0) == 0)
513         {
514             sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
515             sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
516             symt_fill_sym_info(module, &sym->symt, sym_info);
517             if (!cb(sym_info, sym_info->Size, user)) break;
518         }
519     }   
520     regfree(&preg);
521     return sym ? FALSE : TRUE;
522 }
523
524 /***********************************************************************
525  *              resort_symbols
526  *
527  * Rebuild sorted list of symbols for a module.
528  */
529 static BOOL resort_symbols(struct module* module)
530 {
531     int                         nsym = 0;
532     void*                       ptr;
533     struct symt_ht*             sym;
534     struct hash_table_iter      hti;
535
536     hash_table_iter_init(&module->ht_symbols, &hti, NULL);
537     while ((ptr = hash_table_iter_up(&hti)))
538         nsym++;
539
540     if (!(module->module.NumSyms = nsym)) return FALSE;
541     
542     if (module->addr_sorttab)
543         module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
544                                            module->addr_sorttab, 
545                                            nsym * sizeof(struct symt_ht*));
546     else
547         module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
548                                          nsym * sizeof(struct symt_ht*));
549     if (!module->addr_sorttab) return FALSE;
550
551     nsym = 0;
552     hash_table_iter_init(&module->ht_symbols, &hti, NULL);
553     while ((ptr = hash_table_iter_up(&hti)))
554     {
555         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
556         assert(sym);
557         module->addr_sorttab[nsym++] = sym;
558     }
559     
560     qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
561     return module->sortlist_valid = TRUE;
562 }
563
564 /* assume addr is in module */
565 static int symt_find_nearest(struct module* module, DWORD addr)
566 {
567     int         mid, high, low;
568
569     if (!module->sortlist_valid && !resort_symbols(module)) return -1;
570
571     /*
572      * Binary search to find closest symbol.
573      */
574     low = 0;
575     high = module->module.NumSyms;
576     
577     while (high > low + 1)
578     {
579         mid = (high + low) / 2;
580         if (cmp_sorttab_addr(module, mid, addr) < 0)
581             low = mid;
582         else
583             high = mid;
584     }
585     if (low != high && high != module->module.NumSyms && 
586         cmp_sorttab_addr(module, high, addr) <= 0)
587         low = high;
588
589     /* If found symbol is a public symbol, check if there are any other entries that
590      * might also have the same address, but would get better information
591      */
592     if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
593     {   
594         DWORD   ref;
595
596         symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref);
597         if (low > 0 &&
598             module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
599             !cmp_sorttab_addr(module, low - 1, ref))
600             low--;
601         else if (low < module->module.NumSyms - 1 && 
602                  module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
603                  !cmp_sorttab_addr(module, low + 1, ref))
604             low++;
605     }
606
607     return low;
608 }
609
610 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
611                                     regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
612                                     PVOID user, SYMBOL_INFO* sym_info,
613                                     struct vector* v)
614 {
615     struct symt**       plsym = NULL;
616     struct symt*        lsym = NULL;
617     DWORD               pc = pcs->ctx_frame.InstructionOffset;
618
619     while ((plsym = vector_iter_up(v, plsym)))
620     {
621         lsym = *plsym;
622         switch (lsym->tag)
623         {
624         case SymTagBlock:
625             {
626                 struct symt_block*  block = (struct symt_block*)lsym;
627                 if (pc < block->address || block->address + block->size <= pc)
628                     continue;
629                 if (!symt_enum_locals_helper(pcs, module, preg, cb, user, 
630                                              sym_info, &block->vchildren))
631                     return FALSE;
632             }
633             break;
634         case SymTagData:
635             if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
636             {
637                 symt_fill_sym_info(module, lsym, sym_info);
638                 if (!cb(sym_info, sym_info->Size, user))
639                     return FALSE;
640             }
641             break;
642         case SymTagLabel:
643         case SymTagFuncDebugStart:
644         case SymTagFuncDebugEnd:
645             break;
646         default:
647             FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
648             assert(0);
649         }
650     }
651     return TRUE;
652 }
653
654 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
655                              PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
656                              PVOID UserContext)
657 {
658     struct module*      module;
659     struct symt_ht*     sym;
660     char                buffer[sizeof(SYMBOL_INFO) + 256];
661     SYMBOL_INFO*        sym_info = (SYMBOL_INFO*)buffer;
662     DWORD               pc = pcs->ctx_frame.InstructionOffset;
663     int                 idx;
664
665     sym_info->SizeOfStruct = sizeof(*sym_info);
666     sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
667
668     module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
669     if (!(module = module_get_debug(pcs, module))) return FALSE;
670     if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
671
672     sym = module->addr_sorttab[idx];
673     if (sym->symt.tag == SymTagFunction)
674     {
675         BOOL            ret;
676         regex_t         preg;
677
678         compile_regex(mask ? mask : "*", &preg);
679         ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback, 
680                                       UserContext, sym_info, 
681                                       &((struct symt_function*)sym)->vchildren);
682         regfree(&preg);
683         return ret;
684         
685     }
686     symt_fill_sym_info(module, &sym->symt, sym_info);
687     return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
688 }
689
690 /******************************************************************
691  *              SymEnumSymbols (DBGHELP.@)
692  *
693  */
694 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG BaseOfDll, PCSTR Mask,
695                            PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
696                            PVOID UserContext)
697 {
698     struct process*     pcs = process_find_by_handle(hProcess);
699     struct module*      module;
700
701     TRACE("(%p %08lx %s %p %p)\n", 
702           hProcess, BaseOfDll, debugstr_a(Mask), EnumSymbolsCallback, UserContext);
703
704     if (!pcs) return FALSE;
705
706     if (BaseOfDll == 0)
707     {
708         if (Mask && Mask[0] == '!')
709         {
710             if (!Mask[1])
711             {
712                 /* FIXME: is this really what's intended ??? */
713                 for (module = pcs->lmodules; module; module = module->next)
714                 {
715                     if (module->module.SymType != SymNone &&
716                         !symt_enum_module(module, "*", EnumSymbolsCallback, UserContext))
717                         break;
718                 }
719                 return TRUE;
720             }
721             module = module_find_by_name(pcs, &Mask[1], DMT_UNKNOWN);
722             Mask++;
723         }
724         else return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
725     }
726     else
727     {
728         module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
729         if (Mask && Mask[0] == '!')
730         {
731             if (!Mask[1] ||
732                 strcmp(&Mask[1], module->module.ModuleName))
733             {
734                 FIXME("Strange call mode\n");
735                 return FALSE;
736             }
737             Mask = "*";
738         }
739         else if (!Mask) Mask = "*";
740     }
741     if ((module = module_get_debug(pcs, module)))
742         symt_enum_module(module, Mask, EnumSymbolsCallback, UserContext);
743     return TRUE;
744 }
745
746 struct sym_enumerate
747 {
748     void*                       ctx;
749     PSYM_ENUMSYMBOLS_CALLBACK   cb;
750 };
751
752 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
753 {
754     struct sym_enumerate*       se = (struct sym_enumerate*)ctx;
755     return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
756 }
757
758 /***********************************************************************
759  *              SymEnumerateSymbols (DBGHELP.@)
760  */
761 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
762                                 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback, 
763                                 PVOID UserContext)
764 {
765     struct sym_enumerate        se;
766
767     se.ctx = UserContext;
768     se.cb  = EnumSymbolsCallback;
769     
770     return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
771 }
772
773 /******************************************************************
774  *              SymFromAddr (DBGHELP.@)
775  *
776  */
777 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD Address, 
778                         DWORD* Displacement, PSYMBOL_INFO Symbol)
779 {
780     struct process*     pcs = process_find_by_handle(hProcess);
781     struct module*      module;
782     struct symt_ht*     sym;
783     int                 idx;
784
785     if (!pcs) return FALSE;
786     module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
787     if (!(module = module_get_debug(pcs, module))) return FALSE;
788     if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
789
790     sym = module->addr_sorttab[idx];
791
792     symt_fill_sym_info(module, &sym->symt, Symbol);
793     if (Displacement) *Displacement = Address - Symbol->Address;
794     return TRUE;
795 }
796
797 /******************************************************************
798  *              SymGetSymFromAddr (DBGHELP.@)
799  *
800  */
801 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
802                               PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
803 {
804     char        buffer[sizeof(SYMBOL_INFO) + 256];
805     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
806     size_t      len;
807
808     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
809     si->SizeOfStruct = sizeof(*si);
810     si->MaxNameLen = 256;
811     if (!SymFromAddr(hProcess, Address, Displacement, si))
812         return FALSE;
813
814     Symbol->Address = si->Address;
815     Symbol->Size    = si->Size;
816     Symbol->Flags   = si->Flags;
817     len = min(Symbol->MaxNameLength, si->MaxNameLen);
818     strncpy(Symbol->Name, si->Name, len);
819     Symbol->Name[len - 1] = '\0';
820     return TRUE;
821 }
822
823 /******************************************************************
824  *              SymFromName (DBGHELP.@)
825  *
826  */
827 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
828 {
829     struct process*             pcs = process_find_by_handle(hProcess);
830     struct module*              module;
831     struct hash_table_iter      hti;
832     void*                       ptr;
833     struct symt_ht*             sym = NULL;
834
835     TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
836     if (!pcs) return FALSE;
837     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
838     for (module = pcs->lmodules; module; module = module->next)
839     {
840         if (module->module.SymType != SymNone)
841         {
842             if (module->module.SymType == SymDeferred)
843             {
844                 struct module*  xmodule = module_get_debug(pcs, module);
845                 if (!xmodule) continue;
846                 module = xmodule;
847             }
848             hash_table_iter_init(&module->ht_symbols, &hti, Name);
849             while ((ptr = hash_table_iter_up(&hti)))
850             {
851                 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
852
853                 if (!strcmp(sym->hash_elt.name, Name))
854                 {
855                     symt_fill_sym_info(module, &sym->symt, Symbol);
856                     return TRUE;
857                 }
858             }
859         }
860     }
861     return FALSE;
862 }
863
864 /***********************************************************************
865  *              SymGetSymFromName (DBGHELP.@)
866  */
867 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
868 {
869     char        buffer[sizeof(SYMBOL_INFO) + 256];
870     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
871     size_t      len;
872
873     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
874     si->SizeOfStruct = sizeof(*si);
875     si->MaxNameLen = 256;
876     if (!SymFromName(hProcess, Name, si)) return FALSE;
877
878     Symbol->Address = si->Address;
879     Symbol->Size    = si->Size;
880     Symbol->Flags   = si->Flags;
881     len = min(Symbol->MaxNameLength, si->MaxNameLen);
882     strncpy(Symbol->Name, si->Name, len);
883     Symbol->Name[len - 1] = '\0';
884     return TRUE;
885 }
886
887 /******************************************************************
888  *              sym_fill_func_line_info
889  *
890  * fills information about a file
891  */
892 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func, 
893                               DWORD addr, IMAGEHLP_LINE* line)
894 {
895     struct line_info*   dli = NULL;
896     BOOL                found = FALSE;
897
898     assert(func->symt.tag == SymTagFunction);
899
900     while ((dli = vector_iter_down(&func->vlines, dli)))
901     {
902         if (!(dli->cookie & DLIT_SOURCEFILE))
903         {
904             if (found || dli->u.pc_offset > addr) continue;
905             line->LineNumber = dli->line_number;
906             line->Address    = dli->u.pc_offset;
907             line->Key        = dli;
908             found = TRUE;
909             continue;
910         }
911         if (found)
912         {
913             line->FileName = (char*)source_get(module, dli->u.source_file);
914             return TRUE;
915         }
916     }
917     return FALSE;
918 }
919
920 /***********************************************************************
921  *              SymGetSymNext (DBGHELP.@)
922  */
923 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
924 {
925     /* algo:
926      * get module from Symbol.Address
927      * get index in module.addr_sorttab of Symbol.Address
928      * increment index
929      * if out of module bounds, move to next module in process address space
930      */
931     FIXME("(%p, %p): stub\n", hProcess, Symbol);
932     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
933     return FALSE;
934 }
935
936 /***********************************************************************
937  *              SymGetSymPrev (DBGHELP.@)
938  */
939
940 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
941 {
942     FIXME("(%p, %p): stub\n", hProcess, Symbol);
943     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
944     return FALSE;
945 }
946
947 /******************************************************************
948  *              SymGetLineFromAddr (DBGHELP.@)
949  *
950  */
951 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr, 
952                                PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
953 {
954     struct process*     pcs = process_find_by_handle(hProcess);
955     struct module*      module;
956     int                 idx;
957
958     TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
959
960     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
961
962     if (!pcs) return FALSE;
963     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
964     if (!(module = module_get_debug(pcs, module))) return FALSE;
965     if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
966
967     if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
968     if (!symt_fill_func_line_info(module, 
969                                   (struct symt_function*)module->addr_sorttab[idx],
970                                   dwAddr, Line)) return FALSE;
971     if (pdwDisplacement) *pdwDisplacement = dwAddr - Line->Address;
972     return TRUE;
973 }
974
975 /******************************************************************
976  *              SymGetLinePrev (DBGHELP.@)
977  *
978  */
979 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
980 {
981     struct process*     pcs = process_find_by_handle(hProcess);
982     struct module*      module;
983     struct line_info*   li;
984     BOOL                in_search = FALSE;
985
986     TRACE("(%p %p)\n", hProcess, Line);
987
988     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
989
990     if (!pcs) return FALSE;
991     module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
992     if (!(module = module_get_debug(pcs, module))) return FALSE;
993
994     if (Line->Key == 0) return FALSE;
995     li = (struct line_info*)Line->Key;
996     /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
997      * element we have to go back until we find the prev one to get the real
998      * source file name for the DLIT_OFFSET element just before 
999      * the first DLIT_SOURCEFILE
1000      */
1001     while (!(li->cookie & DLIT_FIRST))
1002     {
1003         li--;
1004         if (!(li->cookie & DLIT_SOURCEFILE))
1005         {
1006             Line->LineNumber = li->line_number;
1007             Line->Address    = li->u.pc_offset;
1008             Line->Key        = li;
1009             if (!in_search) return TRUE;
1010         }
1011         else
1012         {
1013             if (in_search)
1014             {
1015                 Line->FileName = (char*)source_get(module, li->u.source_file);
1016                 return TRUE;
1017             }
1018             in_search = TRUE;
1019         }
1020     }
1021     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1022     return FALSE;
1023 }
1024
1025 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1026 {
1027     struct line_info*   li;
1028
1029     if (line->Key == 0) return FALSE;
1030     li = (struct line_info*)line->Key;
1031     while (!(li->cookie & DLIT_LAST))
1032     {
1033         li++;
1034         if (!(li->cookie & DLIT_SOURCEFILE))
1035         {
1036             line->LineNumber = li->line_number;
1037             line->Address    = li->u.pc_offset;
1038             line->Key        = li;
1039             return TRUE;
1040         }
1041         line->FileName = (char*)source_get(module, li->u.source_file);
1042     }
1043     return FALSE;
1044 }
1045
1046 /******************************************************************
1047  *              SymGetLineNext (DBGHELP.@)
1048  *
1049  */
1050 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1051 {
1052     struct process*     pcs = process_find_by_handle(hProcess);
1053     struct module*      module;
1054
1055     TRACE("(%p %p)\n", hProcess, Line);
1056
1057     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1058     if (!pcs) return FALSE;
1059     module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1060     if (!(module = module_get_debug(pcs, module))) return FALSE;
1061
1062     if (symt_get_func_line_next(module, Line)) return TRUE;
1063     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1064     return FALSE;
1065 }
1066
1067 /***********************************************************************
1068  *              SymFunctionTableAccess (DBGHELP.@)
1069  */
1070 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1071 {
1072     FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1073     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1074     return FALSE;
1075 }
1076
1077 /***********************************************************************
1078  *              SymUnDName (DBGHELP.@)
1079  */
1080 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1081 {
1082     FIXME("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1083     return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength, 
1084                                 UNDNAME_COMPLETE);
1085 }
1086
1087 /***********************************************************************
1088  *              UnDecorateSymbolName (DBGHELP.@)
1089  */
1090 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1091                                   DWORD UndecoratedLength, DWORD Flags)
1092 {
1093     FIXME("(%s, %p, %ld, 0x%08lx): stub\n",
1094           debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1095
1096     strncpy(UnDecoratedName, DecoratedName, UndecoratedLength);
1097     UnDecoratedName[UndecoratedLength - 1] = '\0';
1098     return TRUE;
1099 }