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