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