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