- Sync with trunk r58248 to bring the latest changes from Amine (headers) and others...
[reactos.git] / dll / win32 / 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 sym_info->Flags |= SYMFLAG_LOCAL;
694 {
695 struct location loc = data->u.var;
696
697 if (loc.kind >= loc_user)
698 {
699 unsigned i;
700 struct module_format* modfmt;
701
702 for (i = 0; i < DFI_LAST; i++)
703 {
704 modfmt = pair->effective->format_info[i];
705 if (modfmt && modfmt->loc_compute)
706 {
707 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
708 break;
709 }
710 }
711 }
712 switch (loc.kind)
713 {
714 case loc_error:
715 /* for now we report error cases as a negative register number */
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_REGREL;
724 sym_info->Register = loc.reg;
725 if (loc.reg == CV_REG_NONE || (int)loc.reg < 0 /* error */)
726 FIXME("suspicious register value %x\n", loc.reg);
727 sym_info->Address = loc.offset;
728 break;
729 case loc_absolute:
730 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
731 sym_info->Value = loc.offset;
732 break;
733 default:
734 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
735 assert(0);
736 }
737 }
738 break;
739 case DataIsGlobal:
740 case DataIsFileStatic:
741 switch (data->u.var.kind)
742 {
743 case loc_tlsrel:
744 sym_info->Flags |= SYMFLAG_TLSREL;
745 /* fall through */
746 case loc_absolute:
747 symt_get_address(sym, &sym_info->Address);
748 sym_info->Register = 0;
749 break;
750 default:
751 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
752 assert(0);
753 }
754 break;
755 case DataIsConstant:
756 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
757 switch (data->u.value.n1.n2.vt)
758 {
759 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
760 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
761 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
762 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
763 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
764 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
765 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
766 case VT_EMPTY: sym_info->Value = 0; break;
767 default:
768 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
769 sym_info->Value = 0;
770 break;
771 }
772 break;
773 default:
774 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
775 }
776 }
777 break;
778 case SymTagPublicSymbol:
779 sym_info->Flags |= SYMFLAG_EXPORT;
780 symt_get_address(sym, &sym_info->Address);
781 break;
782 case SymTagFunction:
783 sym_info->Flags |= SYMFLAG_FUNCTION;
784 symt_get_address(sym, &sym_info->Address);
785 break;
786 case SymTagThunk:
787 sym_info->Flags |= SYMFLAG_THUNK;
788 symt_get_address(sym, &sym_info->Address);
789 break;
790 default:
791 symt_get_address(sym, &sym_info->Address);
792 sym_info->Register = 0;
793 break;
794 }
795 sym_info->Scope = 0; /* FIXME */
796 sym_info->Tag = sym->tag;
797 name = symt_get_name(sym);
798 if (sym_info->MaxNameLen)
799 {
800 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
801 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
802 sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
803 {
804 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
805 memcpy(sym_info->Name, name, sym_info->NameLen);
806 sym_info->Name[sym_info->NameLen] = '\0';
807 }
808 }
809 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
810 sym, sym_info->Name, sym_info->Size,
811 wine_dbgstr_longlong(sym_info->Address));
812 }
813
814 struct sym_enum
815 {
816 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
817 PVOID user;
818 SYMBOL_INFO* sym_info;
819 DWORD index;
820 DWORD tag;
821 DWORD64 addr;
822 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
823 };
824
825 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
826 const struct symt_function* func, const struct symt* sym)
827 {
828 symt_fill_sym_info(pair, func, sym, se->sym_info);
829 if (se->index && se->sym_info->info != se->index) return FALSE;
830 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
831 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
832 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
833 }
834
835 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
836 const struct sym_enum* se)
837 {
838 void* ptr;
839 struct symt_ht* sym = NULL;
840 struct hash_table_iter hti;
841
842 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
843 while ((ptr = hash_table_iter_up(&hti)))
844 {
845 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
846 if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
847 {
848 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
849 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
850 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
851 }
852 }
853 return FALSE;
854 }
855
856 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
857 {
858 unsigned low = 0, mid = high / 2;
859 ULONG64 addr;
860
861 if (!high) return 0;
862 symt_get_address(&elt->symt, &addr);
863 do
864 {
865 switch (cmp_sorttab_addr(module, mid, addr))
866 {
867 case 0: return mid;
868 case -1: low = mid + 1; break;
869 case 1: high = mid; break;
870 }
871 mid = low + (high - low) / 2;
872 } while (low < high);
873 return mid;
874 }
875
876 /***********************************************************************
877 * resort_symbols
878 *
879 * Rebuild sorted list of symbols for a module.
880 */
881 static BOOL resort_symbols(struct module* module)
882 {
883 int delta;
884
885 if (!(module->module.NumSyms = module->num_symbols))
886 return FALSE;
887
888 /* we know that set from 0 up to num_sorttab is already sorted
889 * so sort the remaining (new) symbols, and merge the two sets
890 * (unless the first set is empty)
891 */
892 delta = module->num_symbols - module->num_sorttab;
893 qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
894 if (module->num_sorttab)
895 {
896 int i, ins_idx = module->num_sorttab, prev_ins_idx;
897 static struct symt_ht** tmp;
898 static unsigned num_tmp;
899
900 if (num_tmp < delta)
901 {
902 static struct symt_ht** new;
903 if (tmp)
904 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
905 else
906 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
907 if (!new)
908 {
909 module->num_sorttab = 0;
910 return resort_symbols(module);
911 }
912 tmp = new;
913 num_tmp = delta;
914 }
915 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
916 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
917
918 for (i = delta - 1; i >= 0; i--)
919 {
920 prev_ins_idx = ins_idx;
921 ins_idx = where_to_insert(module, ins_idx, tmp[i]);
922 memmove(&module->addr_sorttab[ins_idx + i + 1],
923 &module->addr_sorttab[ins_idx],
924 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
925 module->addr_sorttab[ins_idx + i] = tmp[i];
926 }
927 }
928 module->num_sorttab = module->num_symbols;
929 return module->sortlist_valid = TRUE;
930 }
931
932 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
933 {
934 DWORD type_index;
935
936 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
937 return;
938
939 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
940 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
941 *size = 0x1000; /* arbitrary value */
942 }
943
944 /* assume addr is in module */
945 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
946 {
947 int mid, high, low;
948 ULONG64 ref_addr, ref_size;
949
950 if (!module->sortlist_valid || !module->addr_sorttab)
951 {
952 if (!resort_symbols(module)) return NULL;
953 }
954
955 /*
956 * Binary search to find closest symbol.
957 */
958 low = 0;
959 high = module->num_sorttab;
960
961 symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
962 if (addr < ref_addr) return NULL;
963 if (high)
964 {
965 symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
966 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
967 if (addr >= ref_addr + ref_size) return NULL;
968 }
969
970 while (high > low + 1)
971 {
972 mid = (high + low) / 2;
973 if (cmp_sorttab_addr(module, mid, addr) < 0)
974 low = mid;
975 else
976 high = mid;
977 }
978 if (low != high && high != module->num_sorttab &&
979 cmp_sorttab_addr(module, high, addr) <= 0)
980 low = high;
981
982 /* If found symbol is a public symbol, check if there are any other entries that
983 * might also have the same address, but would get better information
984 */
985 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
986 {
987 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
988 if (low > 0 &&
989 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
990 !cmp_sorttab_addr(module, low - 1, ref_addr))
991 low--;
992 else if (low < module->num_sorttab - 1 &&
993 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
994 !cmp_sorttab_addr(module, low + 1, ref_addr))
995 low++;
996 }
997 /* finally check that we fit into the found symbol */
998 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
999 if (addr < ref_addr) return NULL;
1000 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
1001 if (addr >= ref_addr + ref_size) return NULL;
1002
1003 return module->addr_sorttab[low];
1004 }
1005
1006 static BOOL symt_enum_locals_helper(struct module_pair* pair,
1007 regex_t* preg, const struct sym_enum* se,
1008 struct symt_function* func, const struct vector* v)
1009 {
1010 struct symt* lsym = NULL;
1011 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
1012 unsigned int i;
1013
1014 for (i=0; i<vector_length(v); i++)
1015 {
1016 lsym = *(struct symt**)vector_at(v, i);
1017 switch (lsym->tag)
1018 {
1019 case SymTagBlock:
1020 {
1021 struct symt_block* block = (struct symt_block*)lsym;
1022 if (pc < block->address || block->address + block->size <= pc)
1023 continue;
1024 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
1025 return FALSE;
1026 }
1027 break;
1028 case SymTagData:
1029 if (match_regexp(preg, symt_get_name(lsym)))
1030 {
1031 if (send_symbol(se, pair, func, lsym)) return FALSE;
1032 }
1033 break;
1034 case SymTagLabel:
1035 case SymTagFuncDebugStart:
1036 case SymTagFuncDebugEnd:
1037 case SymTagCustom:
1038 break;
1039 default:
1040 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1041 assert(0);
1042 }
1043 }
1044 return TRUE;
1045 }
1046
1047 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
1048 const struct sym_enum* se)
1049 {
1050 struct module_pair pair;
1051 struct symt_ht* sym;
1052 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
1053
1054 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1055 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1056
1057 pair.pcs = pcs;
1058 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1059 if (!module_get_debug(&pair)) return FALSE;
1060 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1061
1062 if (sym->symt.tag == SymTagFunction)
1063 {
1064 BOOL ret;
1065 regex_t preg;
1066
1067 compile_regex(mask ? mask : "*", -1, &preg,
1068 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1069 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1070 &((struct symt_function*)sym)->vchildren);
1071 regfree(&preg);
1072 return ret;
1073 }
1074 return FALSE;
1075 }
1076
1077 /******************************************************************
1078 * copy_symbolW
1079 *
1080 * Helper for transforming an ANSI symbol info into a UNICODE one.
1081 * Assume that MaxNameLen is the same for both version (A & W).
1082 */
1083 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1084 {
1085 siw->SizeOfStruct = si->SizeOfStruct;
1086 siw->TypeIndex = si->TypeIndex;
1087 siw->Reserved[0] = si->Reserved[0];
1088 siw->Reserved[1] = si->Reserved[1];
1089 siw->Index = si->info; /* FIXME: see dbghelp.h */
1090 siw->Size = si->Size;
1091 siw->ModBase = si->ModBase;
1092 siw->Flags = si->Flags;
1093 siw->Value = si->Value;
1094 siw->Address = si->Address;
1095 siw->Register = si->Register;
1096 siw->Scope = si->Scope;
1097 siw->Tag = si->Tag;
1098 siw->NameLen = si->NameLen;
1099 siw->MaxNameLen = si->MaxNameLen;
1100 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1101 }
1102
1103 /******************************************************************
1104 * sym_enum
1105 *
1106 * Core routine for most of the enumeration of symbols
1107 */
1108 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1109 const struct sym_enum* se)
1110 {
1111 struct module_pair pair;
1112 const char* bang;
1113 regex_t mod_regex, sym_regex;
1114
1115 pair.pcs = process_find_by_handle(hProcess);
1116 if (!pair.pcs) return FALSE;
1117 if (BaseOfDll == 0)
1118 {
1119 /* do local variables ? */
1120 if (!Mask || !(bang = strchr(Mask, '!')))
1121 return symt_enum_locals(pair.pcs, Mask, se);
1122
1123 if (bang == Mask) return FALSE;
1124
1125 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1126 compile_regex(bang + 1, -1, &sym_regex,
1127 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1128
1129 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1130 {
1131 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1132 {
1133 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1134 symt_enum_module(&pair, &sym_regex, se))
1135 break;
1136 }
1137 }
1138 /* not found in PE modules, retry on the ELF ones
1139 */
1140 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1141 {
1142 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1143 {
1144 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1145 !module_get_containee(pair.pcs, pair.requested) &&
1146 module_get_debug(&pair))
1147 {
1148 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1149 symt_enum_module(&pair, &sym_regex, se))
1150 break;
1151 }
1152 }
1153 }
1154 regfree(&mod_regex);
1155 regfree(&sym_regex);
1156 return TRUE;
1157 }
1158 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1159 if (!module_get_debug(&pair))
1160 return FALSE;
1161
1162 /* we always ignore module name from Mask when BaseOfDll is defined */
1163 if (Mask && (bang = strchr(Mask, '!')))
1164 {
1165 if (bang == Mask) return FALSE;
1166 Mask = bang + 1;
1167 }
1168
1169 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
1170 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1171 symt_enum_module(&pair, &sym_regex, se);
1172 regfree(&sym_regex);
1173
1174 return TRUE;
1175 }
1176
1177 /******************************************************************
1178 * SymEnumSymbols (DBGHELP.@)
1179 *
1180 * cases BaseOfDll = 0
1181 * !foo fails always (despite what MSDN states)
1182 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1183 * no ! in Mask, lookup in local Context
1184 * cases BaseOfDll != 0
1185 * !foo fails always (despite what MSDN states)
1186 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1187 */
1188 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1189 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1190 PVOID UserContext)
1191 {
1192 struct sym_enum se;
1193
1194 TRACE("(%p %s %s %p %p)\n",
1195 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1196 EnumSymbolsCallback, UserContext);
1197
1198 se.cb = EnumSymbolsCallback;
1199 se.user = UserContext;
1200 se.index = 0;
1201 se.tag = 0;
1202 se.addr = 0;
1203 se.sym_info = (PSYMBOL_INFO)se.buffer;
1204
1205 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1206 }
1207
1208 struct sym_enumW
1209 {
1210 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1211 void* ctx;
1212 PSYMBOL_INFOW sym_info;
1213 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1214
1215 };
1216
1217 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1218 {
1219 struct sym_enumW* sew = ctx;
1220
1221 copy_symbolW(sew->sym_info, si);
1222
1223 return (sew->cb)(sew->sym_info, size, sew->ctx);
1224 }
1225
1226 /******************************************************************
1227 * SymEnumSymbolsW (DBGHELP.@)
1228 *
1229 */
1230 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1231 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1232 PVOID UserContext)
1233 {
1234 struct sym_enumW sew;
1235 BOOL ret = FALSE;
1236 char* maskA = NULL;
1237
1238 sew.ctx = UserContext;
1239 sew.cb = EnumSymbolsCallback;
1240 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1241
1242 if (Mask)
1243 {
1244 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1245 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1246 if (!maskA) return FALSE;
1247 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1248 }
1249 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1250 HeapFree(GetProcessHeap(), 0, maskA);
1251
1252 return ret;
1253 }
1254
1255 struct sym_enumerate
1256 {
1257 void* ctx;
1258 PSYM_ENUMSYMBOLS_CALLBACK cb;
1259 };
1260
1261 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1262 {
1263 struct sym_enumerate* se = ctx;
1264 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1265 }
1266
1267 /***********************************************************************
1268 * SymEnumerateSymbols (DBGHELP.@)
1269 */
1270 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1271 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1272 PVOID UserContext)
1273 {
1274 struct sym_enumerate se;
1275
1276 se.ctx = UserContext;
1277 se.cb = EnumSymbolsCallback;
1278
1279 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1280 }
1281
1282 struct sym_enumerate64
1283 {
1284 void* ctx;
1285 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1286 };
1287
1288 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1289 {
1290 struct sym_enumerate64* se = ctx;
1291 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1292 }
1293
1294 /***********************************************************************
1295 * SymEnumerateSymbols64 (DBGHELP.@)
1296 */
1297 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1298 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1299 PVOID UserContext)
1300 {
1301 struct sym_enumerate64 se;
1302
1303 se.ctx = UserContext;
1304 se.cb = EnumSymbolsCallback;
1305
1306 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1307 }
1308
1309 /******************************************************************
1310 * SymFromAddr (DBGHELP.@)
1311 *
1312 */
1313 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1314 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1315 {
1316 struct module_pair pair;
1317 struct symt_ht* sym;
1318
1319 pair.pcs = process_find_by_handle(hProcess);
1320 if (!pair.pcs) return FALSE;
1321 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1322 if (!module_get_debug(&pair)) return FALSE;
1323 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1324
1325 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1326 *Displacement = Address - Symbol->Address;
1327 return TRUE;
1328 }
1329
1330 /******************************************************************
1331 * SymFromAddrW (DBGHELP.@)
1332 *
1333 */
1334 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1335 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1336 {
1337 PSYMBOL_INFO si;
1338 unsigned len;
1339 BOOL ret;
1340
1341 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1342 si = HeapAlloc(GetProcessHeap(), 0, len);
1343 if (!si) return FALSE;
1344
1345 si->SizeOfStruct = sizeof(*si);
1346 si->MaxNameLen = Symbol->MaxNameLen;
1347 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1348 {
1349 copy_symbolW(Symbol, si);
1350 }
1351 HeapFree(GetProcessHeap(), 0, si);
1352 return ret;
1353 }
1354
1355 /******************************************************************
1356 * SymGetSymFromAddr (DBGHELP.@)
1357 *
1358 */
1359 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1360 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1361 {
1362 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1363 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1364 size_t len;
1365 DWORD64 Displacement64;
1366
1367 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1368 si->SizeOfStruct = sizeof(*si);
1369 si->MaxNameLen = MAX_SYM_NAME;
1370 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1371 return FALSE;
1372
1373 if (Displacement)
1374 *Displacement = Displacement64;
1375 Symbol->Address = si->Address;
1376 Symbol->Size = si->Size;
1377 Symbol->Flags = si->Flags;
1378 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1379 lstrcpynA(Symbol->Name, si->Name, len);
1380 return TRUE;
1381 }
1382
1383 /******************************************************************
1384 * SymGetSymFromAddr64 (DBGHELP.@)
1385 *
1386 */
1387 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1388 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1389 {
1390 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1391 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1392 size_t len;
1393 DWORD64 Displacement64;
1394
1395 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1396 si->SizeOfStruct = sizeof(*si);
1397 si->MaxNameLen = MAX_SYM_NAME;
1398 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1399 return FALSE;
1400
1401 if (Displacement)
1402 *Displacement = Displacement64;
1403 Symbol->Address = si->Address;
1404 Symbol->Size = si->Size;
1405 Symbol->Flags = si->Flags;
1406 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1407 lstrcpynA(Symbol->Name, si->Name, len);
1408 return TRUE;
1409 }
1410
1411 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1412 SYMBOL_INFO* symbol)
1413 {
1414 struct hash_table_iter hti;
1415 void* ptr;
1416 struct symt_ht* sym = NULL;
1417 struct module_pair pair;
1418
1419 pair.pcs = pcs;
1420 if (!(pair.requested = module)) return FALSE;
1421 if (!module_get_debug(&pair)) return FALSE;
1422
1423 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1424 while ((ptr = hash_table_iter_up(&hti)))
1425 {
1426 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1427
1428 if (!strcmp(sym->hash_elt.name, name))
1429 {
1430 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1431 return TRUE;
1432 }
1433 }
1434 return FALSE;
1435
1436 }
1437 /******************************************************************
1438 * SymFromName (DBGHELP.@)
1439 *
1440 */
1441 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1442 {
1443 struct process* pcs = process_find_by_handle(hProcess);
1444 struct module* module;
1445 const char* name;
1446
1447 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1448 if (!pcs) return FALSE;
1449 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1450 name = strchr(Name, '!');
1451 if (name)
1452 {
1453 char tmp[128];
1454 assert(name - Name < sizeof(tmp));
1455 memcpy(tmp, Name, name - Name);
1456 tmp[name - Name] = '\0';
1457 module = module_find_by_nameA(pcs, tmp);
1458 return find_name(pcs, module, name + 1, Symbol);
1459 }
1460 for (module = pcs->lmodules; module; module = module->next)
1461 {
1462 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1463 return TRUE;
1464 }
1465 /* not found in PE modules, retry on the ELF ones
1466 */
1467 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1468 {
1469 for (module = pcs->lmodules; module; module = module->next)
1470 {
1471 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1472 !module_get_containee(pcs, module) &&
1473 find_name(pcs, module, Name, Symbol))
1474 return TRUE;
1475 }
1476 }
1477 return FALSE;
1478 }
1479
1480 /***********************************************************************
1481 * SymGetSymFromName64 (DBGHELP.@)
1482 */
1483 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1484 {
1485 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1486 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1487 size_t len;
1488
1489 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1490 si->SizeOfStruct = sizeof(*si);
1491 si->MaxNameLen = MAX_SYM_NAME;
1492 if (!SymFromName(hProcess, Name, si)) return FALSE;
1493
1494 Symbol->Address = si->Address;
1495 Symbol->Size = si->Size;
1496 Symbol->Flags = si->Flags;
1497 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1498 lstrcpynA(Symbol->Name, si->Name, len);
1499 return TRUE;
1500 }
1501
1502 /***********************************************************************
1503 * SymGetSymFromName (DBGHELP.@)
1504 */
1505 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1506 {
1507 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1508 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1509 size_t len;
1510
1511 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1512 si->SizeOfStruct = sizeof(*si);
1513 si->MaxNameLen = MAX_SYM_NAME;
1514 if (!SymFromName(hProcess, Name, si)) return FALSE;
1515
1516 Symbol->Address = si->Address;
1517 Symbol->Size = si->Size;
1518 Symbol->Flags = si->Flags;
1519 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1520 lstrcpynA(Symbol->Name, si->Name, len);
1521 return TRUE;
1522 }
1523
1524 /******************************************************************
1525 * sym_fill_func_line_info
1526 *
1527 * fills information about a file
1528 */
1529 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1530 DWORD64 addr, IMAGEHLP_LINE64* line)
1531 {
1532 struct line_info* dli = NULL;
1533 BOOL found = FALSE;
1534 int i;
1535
1536 assert(func->symt.tag == SymTagFunction);
1537
1538 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1539 {
1540 dli = vector_at(&func->vlines, i);
1541 if (!dli->is_source_file)
1542 {
1543 if (found || dli->u.pc_offset > addr) continue;
1544 line->LineNumber = dli->line_number;
1545 line->Address = dli->u.pc_offset;
1546 line->Key = dli;
1547 found = TRUE;
1548 continue;
1549 }
1550 if (found)
1551 {
1552 line->FileName = (char*)source_get(module, dli->u.source_file);
1553 return TRUE;
1554 }
1555 }
1556 return FALSE;
1557 }
1558
1559 /***********************************************************************
1560 * SymGetSymNext64 (DBGHELP.@)
1561 */
1562 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1563 {
1564 /* algo:
1565 * get module from Symbol.Address
1566 * get index in module.addr_sorttab of Symbol.Address
1567 * increment index
1568 * if out of module bounds, move to next module in process address space
1569 */
1570 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1571 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1572 return FALSE;
1573 }
1574
1575 /***********************************************************************
1576 * SymGetSymNext (DBGHELP.@)
1577 */
1578 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1579 {
1580 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1581 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1582 return FALSE;
1583 }
1584
1585 /***********************************************************************
1586 * SymGetSymPrev64 (DBGHELP.@)
1587 */
1588 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1589 {
1590 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1591 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1592 return FALSE;
1593 }
1594
1595 /***********************************************************************
1596 * SymGetSymPrev (DBGHELP.@)
1597 */
1598 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1599 {
1600 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1601 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1602 return FALSE;
1603 }
1604
1605 /******************************************************************
1606 * copy_line_64_from_32 (internal)
1607 *
1608 */
1609 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1610
1611 {
1612 l64->Key = l32->Key;
1613 l64->LineNumber = l32->LineNumber;
1614 l64->FileName = l32->FileName;
1615 l64->Address = l32->Address;
1616 }
1617
1618 /******************************************************************
1619 * copy_line_W64_from_32 (internal)
1620 *
1621 */
1622 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1623 {
1624 unsigned len;
1625
1626 l64w->Key = l64->Key;
1627 l64w->LineNumber = l64->LineNumber;
1628 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1629 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1630 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1631 l64w->Address = l64->Address;
1632 }
1633
1634 /******************************************************************
1635 * copy_line_32_from_64 (internal)
1636 *
1637 */
1638 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1639
1640 {
1641 l32->Key = l64->Key;
1642 l32->LineNumber = l64->LineNumber;
1643 l32->FileName = l64->FileName;
1644 l32->Address = l64->Address;
1645 }
1646
1647 /******************************************************************
1648 * SymGetLineFromAddr (DBGHELP.@)
1649 *
1650 */
1651 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1652 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1653 {
1654 IMAGEHLP_LINE64 il64;
1655
1656 il64.SizeOfStruct = sizeof(il64);
1657 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1658 return FALSE;
1659 copy_line_32_from_64(Line, &il64);
1660 return TRUE;
1661 }
1662
1663 /******************************************************************
1664 * SymGetLineFromAddr64 (DBGHELP.@)
1665 *
1666 */
1667 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1668 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1669 {
1670 struct module_pair pair;
1671 struct symt_ht* symt;
1672
1673 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1674
1675 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1676
1677 pair.pcs = process_find_by_handle(hProcess);
1678 if (!pair.pcs) return FALSE;
1679 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1680 if (!module_get_debug(&pair)) return FALSE;
1681 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1682
1683 if (symt->symt.tag != SymTagFunction) return FALSE;
1684 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1685 dwAddr, Line)) return FALSE;
1686 *pdwDisplacement = dwAddr - Line->Address;
1687 return TRUE;
1688 }
1689
1690 /******************************************************************
1691 * SymGetLineFromAddrW64 (DBGHELP.@)
1692 *
1693 */
1694 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1695 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1696 {
1697 IMAGEHLP_LINE64 il64;
1698
1699 il64.SizeOfStruct = sizeof(il64);
1700 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1701 return FALSE;
1702 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1703 return TRUE;
1704 }
1705
1706 /******************************************************************
1707 * SymGetLinePrev64 (DBGHELP.@)
1708 *
1709 */
1710 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1711 {
1712 struct module_pair pair;
1713 struct line_info* li;
1714 BOOL in_search = FALSE;
1715
1716 TRACE("(%p %p)\n", hProcess, Line);
1717
1718 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1719
1720 pair.pcs = process_find_by_handle(hProcess);
1721 if (!pair.pcs) return FALSE;
1722 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1723 if (!module_get_debug(&pair)) return FALSE;
1724
1725 if (Line->Key == 0) return FALSE;
1726 li = Line->Key;
1727 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1728 * element we have to go back until we find the prev one to get the real
1729 * source file name for the DLIT_OFFSET element just before
1730 * the first DLIT_SOURCEFILE
1731 */
1732 while (!li->is_first)
1733 {
1734 li--;
1735 if (!li->is_source_file)
1736 {
1737 Line->LineNumber = li->line_number;
1738 Line->Address = li->u.pc_offset;
1739 Line->Key = li;
1740 if (!in_search) return TRUE;
1741 }
1742 else
1743 {
1744 if (in_search)
1745 {
1746 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1747 return TRUE;
1748 }
1749 in_search = TRUE;
1750 }
1751 }
1752 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1753 return FALSE;
1754 }
1755
1756 /******************************************************************
1757 * SymGetLinePrev (DBGHELP.@)
1758 *
1759 */
1760 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1761 {
1762 IMAGEHLP_LINE64 line64;
1763
1764 line64.SizeOfStruct = sizeof(line64);
1765 copy_line_64_from_32(&line64, Line);
1766 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1767 copy_line_32_from_64(Line, &line64);
1768 return TRUE;
1769 }
1770
1771 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1772 {
1773 struct line_info* li;
1774
1775 if (line->Key == 0) return FALSE;
1776 li = line->Key;
1777 while (!li->is_last)
1778 {
1779 li++;
1780 if (!li->is_source_file)
1781 {
1782 line->LineNumber = li->line_number;
1783 line->Address = li->u.pc_offset;
1784 line->Key = li;
1785 return TRUE;
1786 }
1787 line->FileName = (char*)source_get(module, li->u.source_file);
1788 }
1789 return FALSE;
1790 }
1791
1792 /******************************************************************
1793 * SymGetLineNext64 (DBGHELP.@)
1794 *
1795 */
1796 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1797 {
1798 struct module_pair pair;
1799
1800 TRACE("(%p %p)\n", hProcess, Line);
1801
1802 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1803 pair.pcs = process_find_by_handle(hProcess);
1804 if (!pair.pcs) return FALSE;
1805 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1806 if (!module_get_debug(&pair)) return FALSE;
1807
1808 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1809 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1810 return FALSE;
1811 }
1812
1813 /******************************************************************
1814 * SymGetLineNext (DBGHELP.@)
1815 *
1816 */
1817 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1818 {
1819 IMAGEHLP_LINE64 line64;
1820
1821 line64.SizeOfStruct = sizeof(line64);
1822 copy_line_64_from_32(&line64, Line);
1823 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1824 copy_line_32_from_64(Line, &line64);
1825 return TRUE;
1826 }
1827
1828 /***********************************************************************
1829 * SymUnDName (DBGHELP.@)
1830 */
1831 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1832 {
1833 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1834 UNDNAME_COMPLETE) != 0;
1835 }
1836
1837 /***********************************************************************
1838 * SymUnDName64 (DBGHELP.@)
1839 */
1840 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1841 {
1842 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1843 UNDNAME_COMPLETE) != 0;
1844 }
1845
1846 static void * CDECL und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1847 static void CDECL und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1848
1849 /***********************************************************************
1850 * UnDecorateSymbolName (DBGHELP.@)
1851 */
1852 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1853 DWORD UndecoratedLength, DWORD Flags)
1854 {
1855 /* undocumented from msvcrt */
1856 static char* (* CDECL p_undname)(char*, const char*, int, void* (* CDECL)(size_t), void (* CDECL)(void*), unsigned short);
1857 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1858
1859 TRACE("(%s, %p, %d, 0x%08x)\n",
1860 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1861
1862 if (!p_undname)
1863 {
1864 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1865 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1866 if (!p_undname) return 0;
1867 }
1868
1869 if (!UnDecoratedName) return 0;
1870 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1871 und_alloc, und_free, Flags))
1872 return 0;
1873 return strlen(UnDecoratedName);
1874 }
1875
1876 /******************************************************************
1877 * SymMatchStringA (DBGHELP.@)
1878 *
1879 */
1880 BOOL WINAPI SymMatchStringA(PCSTR string, PCSTR re, BOOL _case)
1881 {
1882 regex_t preg;
1883 BOOL ret;
1884
1885 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1886
1887 compile_regex(re, -1, &preg, _case);
1888 ret = match_regexp(&preg, string);
1889 regfree(&preg);
1890 return ret;
1891 }
1892
1893 /******************************************************************
1894 * SymMatchStringW (DBGHELP.@)
1895 *
1896 * FIXME: SymMatchStringA should convert and pass the strings to SymMatchStringW,
1897 * but that needs a unicode RE library.
1898 */
1899 BOOL WINAPI SymMatchStringW(PCWSTR string, PCWSTR re, BOOL _case)
1900 {
1901 BOOL ret;
1902 LPSTR s, r;
1903 DWORD len;
1904
1905 TRACE("%s %s %c\n", debugstr_w(string), debugstr_w(re), _case ? 'Y' : 'N');
1906
1907 len = WideCharToMultiByte( CP_ACP, 0, string, -1, NULL, 0, NULL, NULL );
1908 s = HeapAlloc( GetProcessHeap(), 0, len );
1909 WideCharToMultiByte( CP_ACP, 0, string, -1, s, len, NULL, NULL );
1910
1911 len = WideCharToMultiByte( CP_ACP, 0, re, -1, NULL, 0, NULL, NULL );
1912 r = HeapAlloc( GetProcessHeap(), 0, len );
1913 WideCharToMultiByte( CP_ACP, 0, re, -1, r, len, NULL, NULL );
1914
1915 ret = SymMatchStringA(s, r, _case);
1916
1917 HeapFree( GetProcessHeap(), 0, r );
1918 HeapFree( GetProcessHeap(), 0, s );
1919 return ret;
1920 }
1921
1922 /******************************************************************
1923 * SymSearch (DBGHELP.@)
1924 */
1925 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1926 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1927 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1928 PVOID UserContext, DWORD Options)
1929 {
1930 struct sym_enum se;
1931
1932 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1933 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1934 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1935 UserContext, Options);
1936
1937 if (Options != SYMSEARCH_GLOBALSONLY)
1938 {
1939 FIXME("Unsupported searching with options (%x)\n", Options);
1940 SetLastError(ERROR_INVALID_PARAMETER);
1941 return FALSE;
1942 }
1943
1944 se.cb = EnumSymbolsCallback;
1945 se.user = UserContext;
1946 se.index = Index;
1947 se.tag = SymTag;
1948 se.addr = Address;
1949 se.sym_info = (PSYMBOL_INFO)se.buffer;
1950
1951 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1952 }
1953
1954 /******************************************************************
1955 * SymSearchW (DBGHELP.@)
1956 */
1957 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1958 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1959 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1960 PVOID UserContext, DWORD Options)
1961 {
1962 struct sym_enumW sew;
1963 BOOL ret = FALSE;
1964 char* maskA = NULL;
1965
1966 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1967 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1968 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1969 UserContext, Options);
1970
1971 sew.ctx = UserContext;
1972 sew.cb = EnumSymbolsCallback;
1973 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1974
1975 if (Mask)
1976 {
1977 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1978 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1979 if (!maskA) return FALSE;
1980 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1981 }
1982 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1983 sym_enumW, &sew, Options);
1984 HeapFree(GetProcessHeap(), 0, maskA);
1985
1986 return ret;
1987 }
1988
1989 /******************************************************************
1990 * SymAddSymbol (DBGHELP.@)
1991 *
1992 */
1993 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1994 DWORD64 addr, DWORD size, DWORD flags)
1995 {
1996 WCHAR nameW[MAX_SYM_NAME];
1997
1998 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1999 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
2000 }
2001
2002 /******************************************************************
2003 * SymAddSymbolW (DBGHELP.@)
2004 *
2005 */
2006 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
2007 DWORD64 addr, DWORD size, DWORD flags)
2008 {
2009 struct module_pair pair;
2010
2011 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
2012
2013 pair.pcs = process_find_by_handle(hProcess);
2014 if (!pair.pcs) return FALSE;
2015 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
2016 if (!module_get_debug(&pair)) return FALSE;
2017
2018 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2019 return FALSE;
2020 }
2021
2022 /******************************************************************
2023 * SymSetScopeFromAddr (DBGHELP.@)
2024 */
2025 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
2026 {
2027 struct process* pcs;
2028
2029 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2030
2031 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2032 return TRUE;
2033 }
2034
2035 /******************************************************************
2036 * SymEnumLines (DBGHELP.@)
2037 *
2038 */
2039 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2040 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2041 {
2042 struct module_pair pair;
2043 struct hash_table_iter hti;
2044 struct symt_ht* sym;
2045 regex_t re;
2046 struct line_info* dli;
2047 void* ptr;
2048 SRCCODEINFO sci;
2049 const char* file;
2050
2051 if (!cb) return FALSE;
2052 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2053
2054 pair.pcs = process_find_by_handle(hProcess);
2055 if (!pair.pcs) return FALSE;
2056 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2057 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2058 if (!module_get_debug(&pair)) return FALSE;
2059 if (!compile_file_regex(&re, srcfile)) return FALSE;
2060
2061 sci.SizeOfStruct = sizeof(sci);
2062 sci.ModBase = base;
2063
2064 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2065 while ((ptr = hash_table_iter_up(&hti)))
2066 {
2067 unsigned int i;
2068
2069 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2070 if (sym->symt.tag != SymTagFunction) continue;
2071
2072 sci.FileName[0] = '\0';
2073 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2074 {
2075 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2076 if (dli->is_source_file)
2077 {
2078 file = source_get(pair.effective, dli->u.source_file);
2079 if (!match_regexp(&re, file)) sci.FileName[0] = '\0';
2080 else strcpy(sci.FileName, file);
2081 }
2082 else if (sci.FileName[0])
2083 {
2084 sci.Key = dli;
2085 sci.Obj[0] = '\0'; /* FIXME */
2086 sci.LineNumber = dli->line_number;
2087 sci.Address = dli->u.pc_offset;
2088 if (!cb(&sci, user)) break;
2089 }
2090 }
2091 }
2092 regfree(&re);
2093 return TRUE;
2094 }
2095
2096 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2097 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2098 {
2099 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2100 dwLineNumber, plDisplacement, Line);
2101 return FALSE;
2102 }
2103
2104 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2105 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2106 {
2107 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2108 dwLineNumber, lpDisplacement, Line);
2109 return FALSE;
2110 }
2111
2112 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2113 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2114 {
2115 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2116 dwLineNumber, plDisplacement, Line);
2117 return FALSE;
2118 }
2119
2120 /******************************************************************
2121 * SymFromIndex (DBGHELP.@)
2122 *
2123 */
2124 BOOL WINAPI SymFromIndex(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFO symbol)
2125 {
2126 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2127 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2128
2129 return FALSE;
2130 }
2131
2132 /******************************************************************
2133 * SymFromIndexW (DBGHELP.@)
2134 *
2135 */
2136 BOOL WINAPI SymFromIndexW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFOW symbol)
2137 {
2138 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2139 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2140
2141 return FALSE;
2142 }