mshtml: Implement IHTMLDOMNode previousSibling.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include "config.h"
26
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
36
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
39 #include "winnls.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
43
44 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
45 {
46     if (a1 > a2) return 1;
47     if (a1 < a2) return -1;
48     return 0;
49 }
50
51 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
52 {
53     ULONG64     ref;
54     symt_get_address(&module->addr_sorttab[idx]->symt, &ref);
55     return cmp_addr(ref, addr);
56 }
57
58 int symt_cmp_addr(const void* p1, const void* p2)
59 {
60     const struct symt*  sym1 = *(const struct symt* const *)p1;
61     const struct symt*  sym2 = *(const struct symt* const *)p2;
62     ULONG64     a1, a2;
63
64     symt_get_address(sym1, &a1);
65     symt_get_address(sym2, &a2);
66     return cmp_addr(a1, a2);
67 }
68
69 DWORD             symt_ptr2index(struct module* module, const struct symt* sym)
70 {
71 #ifdef _WIN64
72     const struct symt** c;
73     int                 len = vector_length(&module->vsymt), i;
74
75     /* FIXME: this is inefficient */
76     for (i = 0; i < len; i++)
77     {
78         if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
79             return i + 1;
80     }
81     /* not found */
82     c = vector_add(&module->vsymt, &module->pool);
83     if (c) *c = sym;
84     return len + 1;
85 #else
86     return (DWORD)sym;
87 #endif
88 }
89
90 struct symt*      symt_index2ptr(struct module* module, DWORD id)
91 {
92 #ifdef _WIN64
93     if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
94     return *(struct symt**)vector_at(&module->vsymt, id);
95 #else
96     return (struct symt*)id;
97 #endif
98 }
99
100 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
101 {
102     struct symt_ht**    new;
103     unsigned int size;
104
105     if (sz <= module->sorttab_size) return TRUE;
106     if (module->addr_sorttab)
107     {
108         size = module->sorttab_size * 2;
109         new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
110                           size * sizeof(struct symt_ht*));
111     }
112     else
113     {
114         size = 64;
115         new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
116     }
117     if (!new) return FALSE;
118     module->sorttab_size = size;
119     module->addr_sorttab = new;
120     return TRUE;
121 }
122
123 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
124 {
125     ULONG64             addr;
126
127     hash_table_add(&module->ht_symbols, &ht->hash_elt);
128     /* Don't store in sorttab a symbol without address, they are of
129      * no use here (e.g. constant values)
130      */
131     if (symt_get_address(&ht->symt, &addr) &&
132         symt_grow_sorttab(module, module->num_symbols + 1))
133     {
134         module->addr_sorttab[module->num_symbols++] = ht;
135         module->sortlist_valid = FALSE;
136     }
137 }
138
139 #ifdef HAVE_REGEX_H
140
141 /* transforms a dbghelp's regular expression into a POSIX one
142  * Here are the valid dbghelp reg ex characters:
143  *      *       0 or more characters
144  *      ?       a single character
145  *      []      list
146  *      #       0 or more of preceding char
147  *      +       1 or more of preceding char
148  *      escapes \ on #, ?, [, ], *, +. don't work on -
149  */
150 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
151 {
152     char *mask, *p;
153     BOOL        in_escape = FALSE;
154     unsigned    flags = REG_NOSUB;
155
156     if (numchar == -1) numchar = strlen( str );
157
158     p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
159     *p++ = '^';
160
161     while (*str && numchar--)
162     {
163         /* FIXME: this shouldn't be valid on '-' */
164         if (in_escape)
165         {
166             *p++ = '\\';
167             *p++ = *str;
168             in_escape = FALSE;
169         }
170         else switch (*str)
171         {
172         case '\\': in_escape = TRUE; break;
173         case '*':  *p++ = '.'; *p++ = '*'; break;
174         case '?':  *p++ = '.'; break;
175         case '#':  *p++ = '*'; break;
176         /* escape some valid characters in dbghelp reg exp:s */
177         case '$':  *p++ = '\\'; *p++ = '$'; break;
178         /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
179         default:   *p++ = *str; break;
180         }
181         str++;
182     }
183     if (in_escape)
184     {
185         *p++ = '\\';
186         *p++ = '\\';
187     }
188     *p++ = '$';
189     *p = 0;
190     if (_case) flags |= REG_ICASE;
191     if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
192     HeapFree(GetProcessHeap(), 0, mask);
193 }
194
195 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
196 {
197     char *mask, *p;
198     BOOL ret;
199
200     if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
201
202     p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
203     *p++ = '^';
204     while (*srcfile)
205     {
206         switch (*srcfile)
207         {
208         case '\\':
209         case '/':
210             *p++ = '[';
211             *p++ = '\\';
212             *p++ = '\\';
213             *p++ = '/';
214             *p++ = ']';
215             break;
216         case '.':
217             *p++ = '\\';
218             *p++ = '.';
219             break;
220         case '*':
221             *p++ = '.';
222             *p++ = '*';
223             break;
224         default:
225             *p++ = *srcfile;
226             break;
227         }
228         srcfile++;
229     }
230     *p++ = '$';
231     *p = 0;
232     ret = !regcomp(re, mask, REG_NOSUB);
233     HeapFree(GetProcessHeap(), 0, mask);
234     if (!ret)
235     {
236         FIXME("Couldn't compile %s\n", mask);
237         SetLastError(ERROR_INVALID_PARAMETER);
238     }
239     return ret;
240 }
241
242 static int match_regexp( const regex_t *re, const char *str )
243 {
244     return !regexec( re, str, 0, NULL, 0 );
245 }
246
247 #else /* HAVE_REGEX_H */
248
249 /* if we don't have regexp support, fall back to a simple string comparison */
250
251 typedef struct
252 {
253     char *str;
254     BOOL  icase;
255 } regex_t;
256
257 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
258 {
259     if (numchar == -1) numchar = strlen( str );
260
261     re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
262     memcpy( re->str, str, numchar );
263     re->str[numchar] = 0;
264     re->icase = _case;
265 }
266
267 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
268 {
269     if (!srcfile || !*srcfile) re->str = NULL;
270     else compile_regex( srcfile, -1, re, FALSE );
271     return TRUE;
272 }
273
274 static int match_regexp( const regex_t *re, const char *str )
275 {
276     if (!re->str) return 1;
277     if (re->icase) return !lstrcmpiA( re->str, str );
278     return !strcmp( re->str, str );
279 }
280
281 static void regfree( regex_t *re )
282 {
283     HeapFree( GetProcessHeap(), 0, re->str );
284 }
285
286 #endif /* HAVE_REGEX_H */
287
288 struct symt_compiland* symt_new_compiland(struct module* module, 
289                                           unsigned long address, unsigned src_idx)
290 {
291     struct symt_compiland*    sym;
292
293     TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
294                          debugstr_w(module->module.ModuleName), source_get(module, src_idx));
295     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
296     {
297         sym->symt.tag = SymTagCompiland;
298         sym->address  = address;
299         sym->source   = src_idx;
300         vector_init(&sym->vchildren, sizeof(struct symt*), 32);
301     }
302     return sym;
303 }
304
305 struct symt_public* symt_new_public(struct module* module, 
306                                     struct symt_compiland* compiland,
307                                     const char* name,
308                                     unsigned long address, unsigned size)
309 {
310     struct symt_public* sym;
311     struct symt**       p;
312
313     TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
314                          debugstr_w(module->module.ModuleName), name, address);
315     if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
316         symt_find_nearest(module, address) != NULL)
317         return NULL;
318     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
319     {
320         sym->symt.tag      = SymTagPublicSymbol;
321         sym->hash_elt.name = pool_strdup(&module->pool, name);
322         sym->container     = compiland ? &compiland->symt : NULL;
323         sym->address       = address;
324         sym->size          = size;
325         symt_add_module_ht(module, (struct symt_ht*)sym);
326         if (compiland)
327         {
328             p = vector_add(&compiland->vchildren, &module->pool);
329             *p = &sym->symt;
330         }
331     }
332     return sym;
333 }
334
335 struct symt_data* symt_new_global_variable(struct module* module, 
336                                            struct symt_compiland* compiland, 
337                                            const char* name, unsigned is_static,
338                                            struct location loc, unsigned long size,
339                                            struct symt* type)
340 {
341     struct symt_data*   sym;
342     struct symt**       p;
343     DWORD64             tsz;
344
345     TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
346                          debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
347     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
348     {
349         sym->symt.tag      = SymTagData;
350         sym->hash_elt.name = pool_strdup(&module->pool, name);
351         sym->kind          = is_static ? DataIsFileStatic : DataIsGlobal;
352         sym->container     = compiland ? &compiland->symt : NULL;
353         sym->type          = type;
354         sym->u.var         = loc;
355         if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
356         {
357             if (tsz != size)
358                 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
359                       debugstr_w(module->module.ModuleName), name,
360                       wine_dbgstr_longlong(tsz), size);
361         }
362         symt_add_module_ht(module, (struct symt_ht*)sym);
363         if (compiland)
364         {
365             p = vector_add(&compiland->vchildren, &module->pool);
366             *p = &sym->symt;
367         }
368     }
369     return sym;
370 }
371
372 struct symt_function* symt_new_function(struct module* module, 
373                                         struct symt_compiland* compiland, 
374                                         const char* name,
375                                         unsigned long addr, unsigned long size,
376                                         struct symt* sig_type)
377 {
378     struct symt_function*       sym;
379     struct symt**               p;
380
381     TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
382                          debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
383
384     assert(!sig_type || sig_type->tag == SymTagFunctionType);
385     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
386     {
387         sym->symt.tag  = SymTagFunction;
388         sym->hash_elt.name = pool_strdup(&module->pool, name);
389         sym->container = &compiland->symt;
390         sym->address   = addr;
391         sym->type      = sig_type;
392         sym->size      = size;
393         vector_init(&sym->vlines,  sizeof(struct line_info), 64);
394         vector_init(&sym->vchildren, sizeof(struct symt*), 8);
395         symt_add_module_ht(module, (struct symt_ht*)sym);
396         if (compiland)
397         {
398             p = vector_add(&compiland->vchildren, &module->pool);
399             *p = &sym->symt;
400         }
401     }
402     return sym;
403 }
404
405 void symt_add_func_line(struct module* module, struct symt_function* func,
406                         unsigned source_idx, int line_num, unsigned long offset)
407 {
408     struct line_info*   dli;
409     BOOL                last_matches = FALSE;
410     int                 i;
411
412     if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
413
414     TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n", 
415                          func, func->hash_elt.name, offset, 
416                          source_get(module, source_idx), line_num);
417
418     assert(func->symt.tag == SymTagFunction);
419
420     for (i=vector_length(&func->vlines)-1; i>=0; i--)
421     {
422         dli = vector_at(&func->vlines, i);
423         if (dli->is_source_file)
424         {
425             last_matches = (source_idx == dli->u.source_file);
426             break;
427         }
428     }
429
430     if (!last_matches)
431     {
432         /* we shouldn't have line changes on first line of function */
433         dli = vector_add(&func->vlines, &module->pool);
434         dli->is_source_file = 1;
435         dli->is_first       = dli->is_last = 0;
436         dli->line_number    = 0;
437         dli->u.source_file  = source_idx;
438     }
439     dli = vector_add(&func->vlines, &module->pool);
440     dli->is_source_file = 0;
441     dli->is_first       = dli->is_last = 0;
442     dli->line_number    = line_num;
443     dli->u.pc_offset    = func->address + offset;
444 }
445
446 /******************************************************************
447  *             symt_add_func_local
448  *
449  * Adds a new local/parameter to a given function:
450  * In any cases, dt tells whether it's a local variable or a parameter
451  * If regno it's not 0:
452  *      - then variable is stored in a register
453  *      - otherwise, value is referenced by register + offset
454  * Otherwise, the variable is stored on the stack:
455  *      - offset is then the offset from the frame register
456  */
457 struct symt_data* symt_add_func_local(struct module* module, 
458                                       struct symt_function* func, 
459                                       enum DataKind dt,
460                                       const struct location* loc,
461                                       struct symt_block* block, 
462                                       struct symt* type, const char* name)
463 {
464     struct symt_data*   locsym;
465     struct symt**       p;
466
467     TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
468                          debugstr_w(module->module.ModuleName), func->hash_elt.name,
469                          name, type);
470
471     assert(func);
472     assert(func->symt.tag == SymTagFunction);
473     assert(dt == DataIsParam || dt == DataIsLocal);
474
475     locsym = pool_alloc(&module->pool, sizeof(*locsym));
476     locsym->symt.tag      = SymTagData;
477     locsym->hash_elt.name = pool_strdup(&module->pool, name);
478     locsym->hash_elt.next = NULL;
479     locsym->kind          = dt;
480     locsym->container     = block ? &block->symt : &func->symt;
481     locsym->type          = type;
482     locsym->u.var         = *loc;
483     if (block)
484         p = vector_add(&block->vchildren, &module->pool);
485     else
486         p = vector_add(&func->vchildren, &module->pool);
487     *p = &locsym->symt;
488     return locsym;
489 }
490
491
492 struct symt_block* symt_open_func_block(struct module* module, 
493                                         struct symt_function* func,
494                                         struct symt_block* parent_block, 
495                                         unsigned pc, unsigned len)
496 {
497     struct symt_block*  block;
498     struct symt**       p;
499
500     assert(func);
501     assert(func->symt.tag == SymTagFunction);
502
503     assert(!parent_block || parent_block->symt.tag == SymTagBlock);
504     block = pool_alloc(&module->pool, sizeof(*block));
505     block->symt.tag = SymTagBlock;
506     block->address  = func->address + pc;
507     block->size     = len;
508     block->container = parent_block ? &parent_block->symt : &func->symt;
509     vector_init(&block->vchildren, sizeof(struct symt*), 4);
510     if (parent_block)
511         p = vector_add(&parent_block->vchildren, &module->pool);
512     else
513         p = vector_add(&func->vchildren, &module->pool);
514     *p = &block->symt;
515
516     return block;
517 }
518
519 struct symt_block* symt_close_func_block(struct module* module, 
520                                          const struct symt_function* func,
521                                          struct symt_block* block, unsigned pc)
522 {
523     assert(func);
524     assert(func->symt.tag == SymTagFunction);
525
526     if (pc) block->size = func->address + pc - block->address;
527     return (block->container->tag == SymTagBlock) ? 
528         GET_ENTRY(block->container, struct symt_block, symt) : NULL;
529 }
530
531 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
532                                                      struct symt_function* func,
533                                                      enum SymTagEnum point,
534                                                      const struct location* loc,
535                                                      const char* name)
536 {
537     struct symt_hierarchy_point*sym;
538     struct symt**               p;
539
540     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
541     {
542         sym->symt.tag = point;
543         sym->parent   = &func->symt;
544         sym->loc      = *loc;
545         sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
546         p = vector_add(&func->vchildren, &module->pool);
547         *p = &sym->symt;
548     }
549     return sym;
550 }
551
552 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
553 {
554     unsigned            len;
555     struct line_info*   dli;
556
557     assert(func);
558     /* We aren't adding any more locals or line numbers to this function.
559      * Free any spare memory that we might have allocated.
560      */
561     assert(func->symt.tag == SymTagFunction);
562
563 /* EPP     vector_pool_normalize(&func->vlines,    &module->pool); */
564 /* EPP     vector_pool_normalize(&func->vchildren, &module->pool); */
565
566     len = vector_length(&func->vlines);
567     if (len--)
568     {
569         dli = vector_at(&func->vlines,   0);  dli->is_first = 1;
570         dli = vector_at(&func->vlines, len);  dli->is_last  = 1;
571     }
572     return TRUE;
573 }
574
575 struct symt_thunk* symt_new_thunk(struct module* module, 
576                                   struct symt_compiland* compiland, 
577                                   const char* name, THUNK_ORDINAL ord,
578                                   unsigned long addr, unsigned long size)
579 {
580     struct symt_thunk*  sym;
581
582     TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
583                          debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
584
585     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
586     {
587         sym->symt.tag  = SymTagThunk;
588         sym->hash_elt.name = pool_strdup(&module->pool, name);
589         sym->container = &compiland->symt;
590         sym->address   = addr;
591         sym->size      = size;
592         sym->ordinal   = ord;
593         symt_add_module_ht(module, (struct symt_ht*)sym);
594         if (compiland)
595         {
596             struct symt**       p;
597             p = vector_add(&compiland->vchildren, &module->pool);
598             *p = &sym->symt;
599         }
600     }
601     return sym;
602 }
603
604 struct symt_data* symt_new_constant(struct module* module,
605                                     struct symt_compiland* compiland,
606                                     const char* name, struct symt* type,
607                                     const VARIANT* v)
608 {
609     struct symt_data*  sym;
610
611     TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
612                          debugstr_w(module->module.ModuleName), name);
613
614     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
615     {
616         sym->symt.tag      = SymTagData;
617         sym->hash_elt.name = pool_strdup(&module->pool, name);
618         sym->kind          = DataIsConstant;
619         sym->container     = compiland ? &compiland->symt : NULL;
620         sym->type          = type;
621         sym->u.value       = *v;
622         symt_add_module_ht(module, (struct symt_ht*)sym);
623         if (compiland)
624         {
625             struct symt**       p;
626             p = vector_add(&compiland->vchildren, &module->pool);
627             *p = &sym->symt;
628         }
629     }
630     return sym;
631 }
632
633 struct symt_hierarchy_point* symt_new_label(struct module* module,
634                                             struct symt_compiland* compiland,
635                                             const char* name, unsigned long address)
636 {
637     struct symt_hierarchy_point*        sym;
638
639     TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
640                          debugstr_w(module->module.ModuleName), name);
641
642     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
643     {
644         sym->symt.tag      = SymTagLabel;
645         sym->hash_elt.name = pool_strdup(&module->pool, name);
646         sym->loc.kind      = loc_absolute;
647         sym->loc.offset    = address;
648         sym->parent        = compiland ? &compiland->symt : NULL;
649         symt_add_module_ht(module, (struct symt_ht*)sym);
650         if (compiland)
651         {
652             struct symt**       p;
653             p = vector_add(&compiland->vchildren, &module->pool);
654             *p = &sym->symt;
655         }
656     }
657     return sym;
658 }
659
660 /* expect sym_info->MaxNameLen to be set before being called */
661 static void symt_fill_sym_info(struct module_pair* pair,
662                                const struct symt_function* func,
663                                const struct symt* sym, SYMBOL_INFO* sym_info)
664 {
665     const char* name;
666     DWORD64 size;
667
668     if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
669         sym_info->TypeIndex = 0;
670     sym_info->info = symt_ptr2index(pair->effective, sym);
671     sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
672     if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
673         (!sym_info->TypeIndex ||
674          !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
675                          TI_GET_LENGTH, &size)))
676         size = 0;
677     sym_info->Size = (DWORD)size;
678     sym_info->ModBase = pair->requested->module.BaseOfImage;
679     sym_info->Flags = 0;
680     sym_info->Value = 0;
681
682     switch (sym->tag)
683     {
684     case SymTagData:
685         {
686             const struct symt_data*  data = (const struct symt_data*)sym;
687             switch (data->kind)
688             {
689             case DataIsParam:
690                 sym_info->Flags |= SYMFLAG_PARAMETER;
691                 /* fall through */
692             case DataIsLocal:
693                 {
694                     struct location loc = data->u.var;
695
696                     if (loc.kind >= loc_user)
697                     {
698                         unsigned                i;
699                         struct module_format*   modfmt;
700
701                         for (i = 0; i < DFI_LAST; i++)
702                         {
703                             modfmt = pair->effective->format_info[i];
704                             if (modfmt && modfmt->loc_compute)
705                             {
706                                 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
707                                 break;
708                             }
709                         }
710                     }
711                     switch (loc.kind)
712                     {
713                     case loc_error:
714                         /* for now we report error cases as a negative register number */
715                         sym_info->Flags |= SYMFLAG_LOCAL;
716                         /* fall through */
717                     case loc_register:
718                         sym_info->Flags |= SYMFLAG_REGISTER;
719                         sym_info->Register = loc.reg;
720                         sym_info->Address = 0;
721                         break;
722                     case loc_regrel:
723                         sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
724                         /* FIXME: it's i386 dependent !!! */
725                         sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
726                         sym_info->Address = loc.offset;
727                         break;
728                     case loc_absolute:
729                         sym_info->Flags |= SYMFLAG_VALUEPRESENT;
730                         sym_info->Value = loc.offset;
731                         break;
732                     default:
733                         FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
734                         assert(0);
735                     }
736                 }
737                 break;
738             case DataIsGlobal:
739             case DataIsFileStatic:
740                 switch (data->u.var.kind)
741                 {
742                 case loc_tlsrel:
743                     sym_info->Flags |= SYMFLAG_TLSREL;
744                     /* fall through */
745                 case loc_absolute:
746                     symt_get_address(sym, &sym_info->Address);
747                     sym_info->Register = 0;
748                     break;
749                 default:
750                     FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
751                     assert(0);
752                 }
753                 break;
754             case DataIsConstant:
755                 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
756                 switch (data->u.value.n1.n2.vt)
757                 {
758                 case VT_I4:  sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
759                 case VT_I2:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
760                 case VT_I1:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
761                 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
762                 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
763                 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
764                 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
765                 case VT_EMPTY: sym_info->Value = 0; break;
766                 default:
767                     FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
768                     sym_info->Value = 0;
769                     break;
770                 }
771                 break;
772             default:
773                 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
774             }
775         }
776         break;
777     case SymTagPublicSymbol:
778         sym_info->Flags |= SYMFLAG_EXPORT;
779         symt_get_address(sym, &sym_info->Address);
780         break;
781     case SymTagFunction:
782         sym_info->Flags |= SYMFLAG_FUNCTION;
783         symt_get_address(sym, &sym_info->Address);
784         break;
785     case SymTagThunk:
786         sym_info->Flags |= SYMFLAG_THUNK;
787         symt_get_address(sym, &sym_info->Address);
788         break;
789     default:
790         symt_get_address(sym, &sym_info->Address);
791         sym_info->Register = 0;
792         break;
793     }
794     sym_info->Scope = 0; /* FIXME */
795     sym_info->Tag = sym->tag;
796     name = symt_get_name(sym);
797     if (sym_info->MaxNameLen)
798     {
799         if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
800             (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
801                                                       sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
802         {
803             sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
804             memcpy(sym_info->Name, name, sym_info->NameLen);
805             sym_info->Name[sym_info->NameLen] = '\0';
806         }
807     }
808     TRACE_(dbghelp_symt)("%p => %s %u %s\n",
809                          sym, sym_info->Name, sym_info->Size,
810                          wine_dbgstr_longlong(sym_info->Address));
811 }
812
813 struct sym_enum
814 {
815     PSYM_ENUMERATESYMBOLS_CALLBACK      cb;
816     PVOID                               user;
817     SYMBOL_INFO*                        sym_info;
818     DWORD                               index;
819     DWORD                               tag;
820     DWORD64                             addr;
821     char                                buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
822 };
823
824 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
825                         const struct symt_function* func, const struct symt* sym)
826 {
827     symt_fill_sym_info(pair, func, sym, se->sym_info);
828     if (se->index && se->sym_info->info != se->index) return FALSE;
829     if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
830     if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
831     return !se->cb(se->sym_info, se->sym_info->Size, se->user);
832 }
833
834 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
835                              const struct sym_enum* se)
836 {
837     void*                       ptr;
838     struct symt_ht*             sym = NULL;
839     struct hash_table_iter      hti;
840
841     hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
842     while ((ptr = hash_table_iter_up(&hti)))
843     {
844         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
845         if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
846         {
847             se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
848             se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
849             if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
850         }
851     }   
852     return FALSE;
853 }
854
855 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
856 {
857     unsigned    low = 0, mid = high / 2;
858     ULONG64     addr;
859
860     if (!high) return 0;
861     symt_get_address(&elt->symt, &addr);
862     do
863     {
864         switch (cmp_sorttab_addr(module, mid, addr))
865         {
866         case 0: return mid;
867         case -1: low = mid + 1; break;
868         case 1: high = mid; break;
869         }
870         mid = low + (high - low) / 2;
871     } while (low < high);
872     return mid;
873 }
874
875 /***********************************************************************
876  *              resort_symbols
877  *
878  * Rebuild sorted list of symbols for a module.
879  */
880 static BOOL resort_symbols(struct module* module)
881 {
882     int delta;
883
884     if (!(module->module.NumSyms = module->num_symbols))
885         return FALSE;
886
887     /* we know that set from 0 up to num_sorttab is already sorted
888      * so sort the remaining (new) symbols, and merge the two sets
889      * (unless the first set is empty)
890      */
891     delta = module->num_symbols - module->num_sorttab;
892     qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
893     if (module->num_sorttab)
894     {
895         int     i, ins_idx = module->num_sorttab, prev_ins_idx;
896         static struct symt_ht** tmp;
897         static unsigned num_tmp;
898
899         if (num_tmp < delta)
900         {
901             static struct symt_ht** new;
902             if (tmp)
903                 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
904             else
905                 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
906             if (!new)
907             {
908                 module->num_sorttab = 0;
909                 return resort_symbols(module);
910             }
911             tmp = new;
912             num_tmp = delta;
913         }
914         memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
915         qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
916
917         for (i = delta - 1; i >= 0; i--)
918         {
919             prev_ins_idx = ins_idx;
920             ins_idx = where_to_insert(module, ins_idx, tmp[i]);
921             memmove(&module->addr_sorttab[ins_idx + i + 1],
922                     &module->addr_sorttab[ins_idx],
923                     (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
924             module->addr_sorttab[ins_idx + i] = tmp[i];
925         }
926     }
927     module->num_sorttab = module->num_symbols;
928     return module->sortlist_valid = TRUE;
929 }
930
931 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
932 {
933     DWORD       type_index;
934
935     if (symt_get_info(module,  symt, TI_GET_LENGTH, size) && *size)
936         return;
937
938     if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
939         symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
940     *size = 0x1000; /* arbitrary value */
941 }
942
943 /* assume addr is in module */
944 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
945 {
946     int         mid, high, low;
947     ULONG64     ref_addr, ref_size;
948
949     if (!module->sortlist_valid || !module->addr_sorttab)
950     {
951         if (!resort_symbols(module)) return NULL;
952     }
953
954     /*
955      * Binary search to find closest symbol.
956      */
957     low = 0;
958     high = module->num_sorttab;
959
960     symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
961     if (addr < ref_addr) return NULL;
962     if (high)
963     {
964         symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
965         symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
966         if (addr >= ref_addr + ref_size) return NULL;
967     }
968     
969     while (high > low + 1)
970     {
971         mid = (high + low) / 2;
972         if (cmp_sorttab_addr(module, mid, addr) < 0)
973             low = mid;
974         else
975             high = mid;
976     }
977     if (low != high && high != module->num_sorttab &&
978         cmp_sorttab_addr(module, high, addr) <= 0)
979         low = high;
980
981     /* If found symbol is a public symbol, check if there are any other entries that
982      * might also have the same address, but would get better information
983      */
984     if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
985     {
986         symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
987         if (low > 0 &&
988             module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
989             !cmp_sorttab_addr(module, low - 1, ref_addr))
990             low--;
991         else if (low < module->num_sorttab - 1 &&
992                  module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
993                  !cmp_sorttab_addr(module, low + 1, ref_addr))
994             low++;
995     }
996     /* finally check that we fit into the found symbol */
997     symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
998     if (addr < ref_addr) return NULL;
999     symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
1000     if (addr >= ref_addr + ref_size) return NULL;
1001
1002     return module->addr_sorttab[low];
1003 }
1004
1005 static BOOL symt_enum_locals_helper(struct module_pair* pair,
1006                                     regex_t* preg, const struct sym_enum* se,
1007                                     struct symt_function* func, const struct vector* v)
1008 {
1009     struct symt*        lsym = NULL;
1010     DWORD               pc = pair->pcs->ctx_frame.InstructionOffset;
1011     unsigned int        i;
1012
1013     for (i=0; i<vector_length(v); i++)
1014     {
1015         lsym = *(struct symt**)vector_at(v, i);
1016         switch (lsym->tag)
1017         {
1018         case SymTagBlock:
1019             {
1020                 struct symt_block*  block = (struct symt_block*)lsym;
1021                 if (pc < block->address || block->address + block->size <= pc)
1022                     continue;
1023                 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
1024                     return FALSE;
1025             }
1026             break;
1027         case SymTagData:
1028             if (match_regexp(preg, symt_get_name(lsym)))
1029             {
1030                 if (send_symbol(se, pair, func, lsym)) return FALSE;
1031             }
1032             break;
1033         case SymTagLabel:
1034         case SymTagFuncDebugStart:
1035         case SymTagFuncDebugEnd:
1036         case SymTagCustom:
1037             break;
1038         default:
1039             FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1040             assert(0);
1041         }
1042     }
1043     return TRUE;
1044 }
1045
1046 static BOOL symt_enum_locals(struct process* pcs, const char* mask, 
1047                              const struct sym_enum* se)
1048 {
1049     struct module_pair  pair;
1050     struct symt_ht*     sym;
1051     DWORD_PTR           pc = pcs->ctx_frame.InstructionOffset;
1052
1053     se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1054     se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1055
1056     pair.pcs = pcs;
1057     pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1058     if (!module_get_debug(&pair)) return FALSE;
1059     if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1060
1061     if (sym->symt.tag == SymTagFunction)
1062     {
1063         BOOL            ret;
1064         regex_t         preg;
1065
1066         compile_regex(mask ? mask : "*", -1, &preg,
1067                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1068         ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1069                                       &((struct symt_function*)sym)->vchildren);
1070         regfree(&preg);
1071         return ret;
1072     }
1073     return FALSE;
1074 }
1075
1076 /******************************************************************
1077  *              copy_symbolW
1078  *
1079  * Helper for transforming an ANSI symbol info into a UNICODE one.
1080  * Assume that MaxNameLen is the same for both version (A & W).
1081  */
1082 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1083 {
1084     siw->SizeOfStruct = si->SizeOfStruct;
1085     siw->TypeIndex = si->TypeIndex; 
1086     siw->Reserved[0] = si->Reserved[0];
1087     siw->Reserved[1] = si->Reserved[1];
1088     siw->Index = si->info; /* FIXME: see dbghelp.h */
1089     siw->Size = si->Size;
1090     siw->ModBase = si->ModBase;
1091     siw->Flags = si->Flags;
1092     siw->Value = si->Value;
1093     siw->Address = si->Address;
1094     siw->Register = si->Register;
1095     siw->Scope = si->Scope;
1096     siw->Tag = si->Tag;
1097     siw->NameLen = si->NameLen;
1098     siw->MaxNameLen = si->MaxNameLen;
1099     MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1100 }
1101
1102 /******************************************************************
1103  *              sym_enum
1104  *
1105  * Core routine for most of the enumeration of symbols
1106  */
1107 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1108                      const struct sym_enum* se)
1109 {
1110     struct module_pair  pair;
1111     const char*         bang;
1112     regex_t             mod_regex, sym_regex;
1113
1114     pair.pcs = process_find_by_handle(hProcess);
1115     if (!pair.pcs) return FALSE;
1116     if (BaseOfDll == 0)
1117     {
1118         /* do local variables ? */
1119         if (!Mask || !(bang = strchr(Mask, '!')))
1120             return symt_enum_locals(pair.pcs, Mask, se);
1121
1122         if (bang == Mask) return FALSE;
1123
1124         compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1125         compile_regex(bang + 1, -1, &sym_regex, 
1126                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1127         
1128         for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1129         {
1130             if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1131             {
1132                 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1133                     symt_enum_module(&pair, &sym_regex, se))
1134                     break;
1135             }
1136         }
1137         /* not found in PE modules, retry on the ELF ones
1138          */
1139         if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1140         {
1141             for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1142             {
1143                 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1144                     !module_get_containee(pair.pcs, pair.requested) &&
1145                     module_get_debug(&pair))
1146                 {
1147                     if (match_regexp(&mod_regex, pair.requested->module_name) &&
1148                         symt_enum_module(&pair, &sym_regex, se))
1149                     break;
1150                 }
1151             }
1152         }
1153         regfree(&mod_regex);
1154         regfree(&sym_regex);
1155         return TRUE;
1156     }
1157     pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1158     if (!module_get_debug(&pair))
1159         return FALSE;
1160
1161     /* we always ignore module name from Mask when BaseOfDll is defined */
1162     if (Mask && (bang = strchr(Mask, '!')))
1163     {
1164         if (bang == Mask) return FALSE;
1165         Mask = bang + 1;
1166     }
1167
1168     compile_regex(Mask ? Mask : "*", -1, &sym_regex, 
1169                   dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1170     symt_enum_module(&pair, &sym_regex, se);
1171     regfree(&sym_regex);
1172
1173     return TRUE;
1174 }
1175
1176 /******************************************************************
1177  *              SymEnumSymbols (DBGHELP.@)
1178  *
1179  * cases BaseOfDll = 0
1180  *      !foo fails always (despite what MSDN states)
1181  *      RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1182  *      no ! in Mask, lookup in local Context
1183  * cases BaseOfDll != 0
1184  *      !foo fails always (despite what MSDN states)
1185  *      RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1186  */
1187 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1188                            PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1189                            PVOID UserContext)
1190 {
1191     struct sym_enum     se;
1192
1193     TRACE("(%p %s %s %p %p)\n", 
1194           hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1195           EnumSymbolsCallback, UserContext);
1196
1197     se.cb = EnumSymbolsCallback;
1198     se.user = UserContext;
1199     se.index = 0;
1200     se.tag = 0;
1201     se.addr = 0;
1202     se.sym_info = (PSYMBOL_INFO)se.buffer;
1203
1204     return sym_enum(hProcess, BaseOfDll, Mask, &se);
1205 }
1206
1207 struct sym_enumW
1208 {
1209     PSYM_ENUMERATESYMBOLS_CALLBACKW     cb;
1210     void*                               ctx;
1211     PSYMBOL_INFOW                       sym_info;
1212     char                                buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1213
1214 };
1215     
1216 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1217 {
1218     struct sym_enumW*   sew = ctx;
1219
1220     copy_symbolW(sew->sym_info, si);
1221
1222     return (sew->cb)(sew->sym_info, size, sew->ctx);
1223 }
1224
1225 /******************************************************************
1226  *              SymEnumSymbolsW (DBGHELP.@)
1227  *
1228  */
1229 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1230                             PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1231                             PVOID UserContext)
1232 {
1233     struct sym_enumW    sew;
1234     BOOL                ret = FALSE;
1235     char*               maskA = NULL;
1236
1237     sew.ctx = UserContext;
1238     sew.cb = EnumSymbolsCallback;
1239     sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1240
1241     if (Mask)
1242     {
1243         unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1244         maskA = HeapAlloc(GetProcessHeap(), 0, len);
1245         if (!maskA) return FALSE;
1246         WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1247     }
1248     ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1249     HeapFree(GetProcessHeap(), 0, maskA);
1250
1251     return ret;
1252 }
1253
1254 struct sym_enumerate
1255 {
1256     void*                       ctx;
1257     PSYM_ENUMSYMBOLS_CALLBACK   cb;
1258 };
1259
1260 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1261 {
1262     struct sym_enumerate*       se = ctx;
1263     return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1264 }
1265
1266 /***********************************************************************
1267  *              SymEnumerateSymbols (DBGHELP.@)
1268  */
1269 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1270                                 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback, 
1271                                 PVOID UserContext)
1272 {
1273     struct sym_enumerate        se;
1274
1275     se.ctx = UserContext;
1276     se.cb  = EnumSymbolsCallback;
1277     
1278     return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1279 }
1280
1281 struct sym_enumerate64
1282 {
1283     void*                       ctx;
1284     PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1285 };
1286
1287 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1288 {
1289     struct sym_enumerate64*     se = ctx;
1290     return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1291 }
1292
1293 /***********************************************************************
1294  *              SymEnumerateSymbols64 (DBGHELP.@)
1295  */
1296 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1297                                   PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1298                                   PVOID UserContext)
1299 {
1300     struct sym_enumerate64      se;
1301
1302     se.ctx = UserContext;
1303     se.cb  = EnumSymbolsCallback;
1304
1305     return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1306 }
1307
1308 /******************************************************************
1309  *              SymFromAddr (DBGHELP.@)
1310  *
1311  */
1312 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, 
1313                         DWORD64* Displacement, PSYMBOL_INFO Symbol)
1314 {
1315     struct module_pair  pair;
1316     struct symt_ht*     sym;
1317
1318     pair.pcs = process_find_by_handle(hProcess);
1319     if (!pair.pcs) return FALSE;
1320     pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1321     if (!module_get_debug(&pair)) return FALSE;
1322     if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1323
1324     symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1325     *Displacement = Address - Symbol->Address;
1326     return TRUE;
1327 }
1328
1329 /******************************************************************
1330  *              SymFromAddrW (DBGHELP.@)
1331  *
1332  */
1333 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address, 
1334                          DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1335 {
1336     PSYMBOL_INFO        si;
1337     unsigned            len;
1338     BOOL                ret;
1339
1340     len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1341     si = HeapAlloc(GetProcessHeap(), 0, len);
1342     if (!si) return FALSE;
1343
1344     si->SizeOfStruct = sizeof(*si);
1345     si->MaxNameLen = Symbol->MaxNameLen;
1346     if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1347     {
1348         copy_symbolW(Symbol, si);
1349     }
1350     HeapFree(GetProcessHeap(), 0, si);
1351     return ret;
1352 }
1353
1354 /******************************************************************
1355  *              SymGetSymFromAddr (DBGHELP.@)
1356  *
1357  */
1358 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1359                               PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1360 {
1361     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1362     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1363     size_t      len;
1364     DWORD64     Displacement64;
1365
1366     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1367     si->SizeOfStruct = sizeof(*si);
1368     si->MaxNameLen = MAX_SYM_NAME;
1369     if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1370         return FALSE;
1371
1372     if (Displacement)
1373         *Displacement = Displacement64;
1374     Symbol->Address = si->Address;
1375     Symbol->Size    = si->Size;
1376     Symbol->Flags   = si->Flags;
1377     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1378     lstrcpynA(Symbol->Name, si->Name, len);
1379     return TRUE;
1380 }
1381
1382 /******************************************************************
1383  *              SymGetSymFromAddr64 (DBGHELP.@)
1384  *
1385  */
1386 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1387                                 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1388 {
1389     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1390     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1391     size_t      len;
1392     DWORD64     Displacement64;
1393
1394     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1395     si->SizeOfStruct = sizeof(*si);
1396     si->MaxNameLen = MAX_SYM_NAME;
1397     if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1398         return FALSE;
1399
1400     if (Displacement)
1401         *Displacement = Displacement64;
1402     Symbol->Address = si->Address;
1403     Symbol->Size    = si->Size;
1404     Symbol->Flags   = si->Flags;
1405     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1406     lstrcpynA(Symbol->Name, si->Name, len);
1407     return TRUE;
1408 }
1409
1410 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1411                       SYMBOL_INFO* symbol)
1412 {
1413     struct hash_table_iter      hti;
1414     void*                       ptr;
1415     struct symt_ht*             sym = NULL;
1416     struct module_pair          pair;
1417
1418     pair.pcs = pcs;
1419     if (!(pair.requested = module)) return FALSE;
1420     if (!module_get_debug(&pair)) return FALSE;
1421
1422     hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1423     while ((ptr = hash_table_iter_up(&hti)))
1424     {
1425         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1426
1427         if (!strcmp(sym->hash_elt.name, name))
1428         {
1429             symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1430             return TRUE;
1431         }
1432     }
1433     return FALSE;
1434
1435 }
1436 /******************************************************************
1437  *              SymFromName (DBGHELP.@)
1438  *
1439  */
1440 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1441 {
1442     struct process*             pcs = process_find_by_handle(hProcess);
1443     struct module*              module;
1444     const char*                 name;
1445
1446     TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1447     if (!pcs) return FALSE;
1448     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1449     name = strchr(Name, '!');
1450     if (name)
1451     {
1452         char    tmp[128];
1453         assert(name - Name < sizeof(tmp));
1454         memcpy(tmp, Name, name - Name);
1455         tmp[name - Name] = '\0';
1456         module = module_find_by_nameA(pcs, tmp);
1457         return find_name(pcs, module, name + 1, Symbol);
1458     }
1459     for (module = pcs->lmodules; module; module = module->next)
1460     {
1461         if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1462             return TRUE;
1463     }
1464     /* not found in PE modules, retry on the ELF ones
1465      */
1466     if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1467     {
1468         for (module = pcs->lmodules; module; module = module->next)
1469         {
1470             if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1471                 !module_get_containee(pcs, module) &&
1472                 find_name(pcs, module, Name, Symbol))
1473                 return TRUE;
1474         }
1475     }
1476     return FALSE;
1477 }
1478
1479 /***********************************************************************
1480  *              SymGetSymFromName64 (DBGHELP.@)
1481  */
1482 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1483 {
1484     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1485     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1486     size_t      len;
1487
1488     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1489     si->SizeOfStruct = sizeof(*si);
1490     si->MaxNameLen = MAX_SYM_NAME;
1491     if (!SymFromName(hProcess, Name, si)) return FALSE;
1492
1493     Symbol->Address = si->Address;
1494     Symbol->Size    = si->Size;
1495     Symbol->Flags   = si->Flags;
1496     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1497     lstrcpynA(Symbol->Name, si->Name, len);
1498     return TRUE;
1499 }
1500
1501 /***********************************************************************
1502  *              SymGetSymFromName (DBGHELP.@)
1503  */
1504 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1505 {
1506     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1507     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1508     size_t      len;
1509
1510     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1511     si->SizeOfStruct = sizeof(*si);
1512     si->MaxNameLen = MAX_SYM_NAME;
1513     if (!SymFromName(hProcess, Name, si)) return FALSE;
1514
1515     Symbol->Address = si->Address;
1516     Symbol->Size    = si->Size;
1517     Symbol->Flags   = si->Flags;
1518     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1519     lstrcpynA(Symbol->Name, si->Name, len);
1520     return TRUE;
1521 }
1522
1523 /******************************************************************
1524  *              sym_fill_func_line_info
1525  *
1526  * fills information about a file
1527  */
1528 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1529                               DWORD64 addr, IMAGEHLP_LINE64* line)
1530 {
1531     struct line_info*   dli = NULL;
1532     BOOL                found = FALSE;
1533     int                 i;
1534
1535     assert(func->symt.tag == SymTagFunction);
1536
1537     for (i=vector_length(&func->vlines)-1; i>=0; i--)
1538     {
1539         dli = vector_at(&func->vlines, i);
1540         if (!dli->is_source_file)
1541         {
1542             if (found || dli->u.pc_offset > addr) continue;
1543             line->LineNumber = dli->line_number;
1544             line->Address    = dli->u.pc_offset;
1545             line->Key        = dli;
1546             found = TRUE;
1547             continue;
1548         }
1549         if (found)
1550         {
1551             line->FileName = (char*)source_get(module, dli->u.source_file);
1552             return TRUE;
1553         }
1554     }
1555     return FALSE;
1556 }
1557
1558 /***********************************************************************
1559  *              SymGetSymNext64 (DBGHELP.@)
1560  */
1561 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1562 {
1563     /* algo:
1564      * get module from Symbol.Address
1565      * get index in module.addr_sorttab of Symbol.Address
1566      * increment index
1567      * if out of module bounds, move to next module in process address space
1568      */
1569     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1570     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1571     return FALSE;
1572 }
1573
1574 /***********************************************************************
1575  *              SymGetSymNext (DBGHELP.@)
1576  */
1577 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1578 {
1579     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1580     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1581     return FALSE;
1582 }
1583
1584 /***********************************************************************
1585  *              SymGetSymPrev64 (DBGHELP.@)
1586  */
1587 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1588 {
1589     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1590     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1591     return FALSE;
1592 }
1593
1594 /***********************************************************************
1595  *              SymGetSymPrev (DBGHELP.@)
1596  */
1597 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1598 {
1599     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1600     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1601     return FALSE;
1602 }
1603
1604 /******************************************************************
1605  *              copy_line_64_from_32 (internal)
1606  *
1607  */
1608 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1609
1610 {
1611     l64->Key = l32->Key;
1612     l64->LineNumber = l32->LineNumber;
1613     l64->FileName = l32->FileName;
1614     l64->Address = l32->Address;
1615 }
1616
1617 /******************************************************************
1618  *              copy_line_W64_from_32 (internal)
1619  *
1620  */
1621 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1622 {
1623     unsigned len;
1624
1625     l64w->Key = l64->Key;
1626     l64w->LineNumber = l64->LineNumber;
1627     len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1628     if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1629         MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1630     l64w->Address = l64->Address;
1631 }
1632
1633 /******************************************************************
1634  *              copy_line_32_from_64 (internal)
1635  *
1636  */
1637 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1638
1639 {
1640     l32->Key = l64->Key;
1641     l32->LineNumber = l64->LineNumber;
1642     l32->FileName = l64->FileName;
1643     l32->Address = l64->Address;
1644 }
1645
1646 /******************************************************************
1647  *              SymGetLineFromAddr (DBGHELP.@)
1648  *
1649  */
1650 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1651                                PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1652 {
1653     IMAGEHLP_LINE64     il64;
1654
1655     il64.SizeOfStruct = sizeof(il64);
1656     if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1657         return FALSE;
1658     copy_line_32_from_64(Line, &il64);
1659     return TRUE;
1660 }
1661
1662 /******************************************************************
1663  *              SymGetLineFromAddr64 (DBGHELP.@)
1664  *
1665  */
1666 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr, 
1667                                  PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1668 {
1669     struct module_pair  pair;
1670     struct symt_ht*     symt;
1671
1672     TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1673
1674     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1675
1676     pair.pcs = process_find_by_handle(hProcess);
1677     if (!pair.pcs) return FALSE;
1678     pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1679     if (!module_get_debug(&pair)) return FALSE;
1680     if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1681
1682     if (symt->symt.tag != SymTagFunction) return FALSE;
1683     if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1684                                   dwAddr, Line)) return FALSE;
1685     *pdwDisplacement = dwAddr - Line->Address;
1686     return TRUE;
1687 }
1688
1689 /******************************************************************
1690  *              SymGetLineFromAddrW64 (DBGHELP.@)
1691  *
1692  */
1693 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr, 
1694                                   PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1695 {
1696     IMAGEHLP_LINE64     il64;
1697
1698     il64.SizeOfStruct = sizeof(il64);
1699     if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1700         return FALSE;
1701     copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1702     return TRUE;
1703 }
1704
1705 /******************************************************************
1706  *              SymGetLinePrev64 (DBGHELP.@)
1707  *
1708  */
1709 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1710 {
1711     struct module_pair  pair;
1712     struct line_info*   li;
1713     BOOL                in_search = FALSE;
1714
1715     TRACE("(%p %p)\n", hProcess, Line);
1716
1717     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1718
1719     pair.pcs = process_find_by_handle(hProcess);
1720     if (!pair.pcs) return FALSE;
1721     pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1722     if (!module_get_debug(&pair)) return FALSE;
1723
1724     if (Line->Key == 0) return FALSE;
1725     li = Line->Key;
1726     /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1727      * element we have to go back until we find the prev one to get the real
1728      * source file name for the DLIT_OFFSET element just before 
1729      * the first DLIT_SOURCEFILE
1730      */
1731     while (!li->is_first)
1732     {
1733         li--;
1734         if (!li->is_source_file)
1735         {
1736             Line->LineNumber = li->line_number;
1737             Line->Address    = li->u.pc_offset;
1738             Line->Key        = li;
1739             if (!in_search) return TRUE;
1740         }
1741         else
1742         {
1743             if (in_search)
1744             {
1745                 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1746                 return TRUE;
1747             }
1748             in_search = TRUE;
1749         }
1750     }
1751     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1752     return FALSE;
1753 }
1754
1755 /******************************************************************
1756  *              SymGetLinePrev (DBGHELP.@)
1757  *
1758  */
1759 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1760 {
1761     IMAGEHLP_LINE64     line64;
1762
1763     line64.SizeOfStruct = sizeof(line64);
1764     copy_line_64_from_32(&line64, Line);
1765     if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1766     copy_line_32_from_64(Line, &line64);
1767     return TRUE;
1768 }
1769
1770 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1771 {
1772     struct line_info*   li;
1773
1774     if (line->Key == 0) return FALSE;
1775     li = line->Key;
1776     while (!li->is_last)
1777     {
1778         li++;
1779         if (!li->is_source_file)
1780         {
1781             line->LineNumber = li->line_number;
1782             line->Address    = li->u.pc_offset;
1783             line->Key        = li;
1784             return TRUE;
1785         }
1786         line->FileName = (char*)source_get(module, li->u.source_file);
1787     }
1788     return FALSE;
1789 }
1790
1791 /******************************************************************
1792  *              SymGetLineNext64 (DBGHELP.@)
1793  *
1794  */
1795 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1796 {
1797     struct module_pair  pair;
1798
1799     TRACE("(%p %p)\n", hProcess, Line);
1800
1801     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1802     pair.pcs = process_find_by_handle(hProcess);
1803     if (!pair.pcs) return FALSE;
1804     pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1805     if (!module_get_debug(&pair)) return FALSE;
1806
1807     if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1808     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1809     return FALSE;
1810 }
1811
1812 /******************************************************************
1813  *              SymGetLineNext (DBGHELP.@)
1814  *
1815  */
1816 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1817 {
1818     IMAGEHLP_LINE64     line64;
1819
1820     line64.SizeOfStruct = sizeof(line64);
1821     copy_line_64_from_32(&line64, Line);
1822     if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1823     copy_line_32_from_64(Line, &line64);
1824     return TRUE;
1825 }
1826
1827 /***********************************************************************
1828  *              SymUnDName (DBGHELP.@)
1829  */
1830 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1831 {
1832     return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1833                                 UNDNAME_COMPLETE) != 0;
1834 }
1835
1836 /***********************************************************************
1837  *              SymUnDName64 (DBGHELP.@)
1838  */
1839 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1840 {
1841     return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1842                                 UNDNAME_COMPLETE) != 0;
1843 }
1844
1845 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1846 static void  und_free (void* ptr)  { HeapFree(GetProcessHeap(), 0, ptr); }
1847
1848 /***********************************************************************
1849  *              UnDecorateSymbolName (DBGHELP.@)
1850  */
1851 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1852                                   DWORD UndecoratedLength, DWORD Flags)
1853 {
1854     /* undocumented from msvcrt */
1855     static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1856     static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1857
1858     TRACE("(%s, %p, %d, 0x%08x)\n",
1859           debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1860
1861     if (!p_undname)
1862     {
1863         if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1864         if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1865         if (!p_undname) return 0;
1866     }
1867
1868     if (!UnDecoratedName) return 0;
1869     if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength, 
1870                    und_alloc, und_free, Flags))
1871         return 0;
1872     return strlen(UnDecoratedName);
1873 }
1874
1875 /******************************************************************
1876  *              SymMatchString (DBGHELP.@)
1877  *
1878  */
1879 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1880 {
1881     regex_t     preg;
1882     BOOL        ret;
1883
1884     TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1885
1886     compile_regex(re, -1, &preg, _case);
1887     ret = match_regexp(&preg, string);
1888     regfree(&preg);
1889     return ret;
1890 }
1891
1892 /******************************************************************
1893  *              SymSearch (DBGHELP.@)
1894  */
1895 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1896                       DWORD SymTag, PCSTR Mask, DWORD64 Address,
1897                       PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1898                       PVOID UserContext, DWORD Options)
1899 {
1900     struct sym_enum     se;
1901
1902     TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1903           hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1904           wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1905           UserContext, Options);
1906
1907     if (Options != SYMSEARCH_GLOBALSONLY)
1908     {
1909         FIXME("Unsupported searching with options (%x)\n", Options);
1910         SetLastError(ERROR_INVALID_PARAMETER);
1911         return FALSE;
1912     }
1913
1914     se.cb = EnumSymbolsCallback;
1915     se.user = UserContext;
1916     se.index = Index;
1917     se.tag = SymTag;
1918     se.addr = Address;
1919     se.sym_info = (PSYMBOL_INFO)se.buffer;
1920
1921     return sym_enum(hProcess, BaseOfDll, Mask, &se);
1922 }
1923
1924 /******************************************************************
1925  *              SymSearchW (DBGHELP.@)
1926  */
1927 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1928                        DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1929                        PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1930                        PVOID UserContext, DWORD Options)
1931 {
1932     struct sym_enumW    sew;
1933     BOOL                ret = FALSE;
1934     char*               maskA = NULL;
1935
1936     TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1937           hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1938           wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1939           UserContext, Options);
1940
1941     sew.ctx = UserContext;
1942     sew.cb = EnumSymbolsCallback;
1943     sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1944
1945     if (Mask)
1946     {
1947         unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1948         maskA = HeapAlloc(GetProcessHeap(), 0, len);
1949         if (!maskA) return FALSE;
1950         WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1951     }
1952     ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1953                     sym_enumW, &sew, Options);
1954     HeapFree(GetProcessHeap(), 0, maskA);
1955
1956     return ret;
1957 }
1958
1959 /******************************************************************
1960  *              SymAddSymbol (DBGHELP.@)
1961  *
1962  */
1963 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1964                          DWORD64 addr, DWORD size, DWORD flags)
1965 {
1966     WCHAR       nameW[MAX_SYM_NAME];
1967
1968     MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1969     return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1970 }
1971
1972 /******************************************************************
1973  *              SymAddSymbolW (DBGHELP.@)
1974  *
1975  */
1976 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1977                           DWORD64 addr, DWORD size, DWORD flags)
1978 {
1979     struct module_pair  pair;
1980
1981     TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1982
1983     pair.pcs = process_find_by_handle(hProcess);
1984     if (!pair.pcs) return FALSE;
1985     pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1986     if (!module_get_debug(&pair)) return FALSE;
1987
1988     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1989     return FALSE;
1990 }
1991
1992 /******************************************************************
1993  *              SymSetScopeFromAddr (DBGHELP.@)
1994  */
1995 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1996 {
1997     struct process*     pcs;
1998
1999     FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2000
2001     if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2002     return TRUE;
2003 }
2004
2005 /******************************************************************
2006  *              SymEnumLines (DBGHELP.@)
2007  *
2008  */
2009 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2010                          PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2011 {
2012     struct module_pair          pair;
2013     struct hash_table_iter      hti;
2014     struct symt_ht*             sym;
2015     regex_t                     re;
2016     struct line_info*           dli;
2017     void*                       ptr;
2018     SRCCODEINFO                 sci;
2019     const char*                 file;
2020
2021     if (!cb) return FALSE;
2022     if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2023
2024     pair.pcs = process_find_by_handle(hProcess);
2025     if (!pair.pcs) return FALSE;
2026     if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2027     pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2028     if (!module_get_debug(&pair)) return FALSE;
2029     if (!compile_file_regex(&re, srcfile)) return FALSE;
2030
2031     sci.SizeOfStruct = sizeof(sci);
2032     sci.ModBase      = base;
2033
2034     hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2035     while ((ptr = hash_table_iter_up(&hti)))
2036     {
2037         unsigned int    i;
2038
2039         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2040         if (sym->symt.tag != SymTagFunction) continue;
2041
2042         sci.FileName[0] = '\0';
2043         for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2044         {
2045             dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2046             if (dli->is_source_file)
2047             {
2048                 file = source_get(pair.effective, dli->u.source_file);
2049                 if (!match_regexp(&re, file)) sci.FileName[0] = '\0';
2050                 else strcpy(sci.FileName, file);
2051             }
2052             else if (sci.FileName[0])
2053             {
2054                 sci.Key = dli;
2055                 sci.Obj[0] = '\0'; /* FIXME */
2056                 sci.LineNumber = dli->line_number;
2057                 sci.Address = dli->u.pc_offset;
2058                 if (!cb(&sci, user)) break;
2059             }
2060         }
2061     }
2062     regfree(&re);
2063     return TRUE;
2064 }
2065
2066 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2067                 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2068 {
2069     FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2070                 dwLineNumber, plDisplacement, Line);
2071     return FALSE;
2072 }
2073
2074 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2075                 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2076 {
2077     FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2078                 dwLineNumber, lpDisplacement, Line);
2079     return FALSE;
2080 }
2081
2082 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2083                 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2084 {
2085     FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2086                 dwLineNumber, plDisplacement, Line);
2087     return FALSE;
2088 }