wininet: Use a semicolon to delimit registered proxy servers.
[wine/multimedia.git] / dlls / dbghelp / symbol.c
blob3e7877a95bfdf54b5dab2978bd8a1428cf856260
1 /*
2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004, Eric Pouech
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.
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.
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
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
25 #include "config.h"
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>
34 #include "wine/debug.h"
35 #include "dbghelp_private.h"
36 #include "winnls.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
39 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
41 static const WCHAR starW[] = {'*','\0'};
43 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
45 if (a1 > a2) return 1;
46 if (a1 < a2) return -1;
47 return 0;
50 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
52 ULONG64 ref;
53 symt_get_address(&module->addr_sorttab[idx]->symt, &ref);
54 return cmp_addr(ref, addr);
57 int symt_cmp_addr(const void* p1, const void* p2)
59 const struct symt* sym1 = *(const struct symt* const *)p1;
60 const struct symt* sym2 = *(const struct symt* const *)p2;
61 ULONG64 a1, a2;
63 symt_get_address(sym1, &a1);
64 symt_get_address(sym2, &a2);
65 return cmp_addr(a1, a2);
68 DWORD symt_ptr2index(struct module* module, const struct symt* sym)
70 #ifdef _WIN64
71 const struct symt** c;
72 int len = vector_length(&module->vsymt), i;
74 /* FIXME: this is inefficient */
75 for (i = 0; i < len; i++)
77 if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
78 return i + 1;
80 /* not found */
81 c = vector_add(&module->vsymt, &module->pool);
82 if (c) *c = sym;
83 return len + 1;
84 #else
85 return (DWORD)sym;
86 #endif
89 struct symt* symt_index2ptr(struct module* module, DWORD id)
91 #ifdef _WIN64
92 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
93 return *(struct symt**)vector_at(&module->vsymt, id);
94 #else
95 return (struct symt*)id;
96 #endif
99 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
101 struct symt_ht** new;
102 unsigned int size;
104 if (sz <= module->sorttab_size) return TRUE;
105 if (module->addr_sorttab)
107 size = module->sorttab_size * 2;
108 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
109 size * sizeof(struct symt_ht*));
111 else
113 size = 64;
114 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
116 if (!new) return FALSE;
117 module->sorttab_size = size;
118 module->addr_sorttab = new;
119 return TRUE;
122 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
124 ULONG64 addr;
126 hash_table_add(&module->ht_symbols, &ht->hash_elt);
127 /* Don't store in sorttab a symbol without address, they are of
128 * no use here (e.g. constant values)
130 if (symt_get_address(&ht->symt, &addr) &&
131 symt_grow_sorttab(module, module->num_symbols + 1))
133 module->addr_sorttab[module->num_symbols++] = ht;
134 module->sortlist_valid = FALSE;
138 static WCHAR* file_regex(const char* srcfile)
140 WCHAR* mask;
141 WCHAR* p;
143 if (!srcfile || !*srcfile)
145 if (!(p = mask = HeapAlloc(GetProcessHeap(), 0, 3 * sizeof(WCHAR)))) return NULL;
146 *p++ = '?';
147 *p++ = '#';
149 else
151 DWORD sz = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
152 WCHAR* srcfileW;
154 /* FIXME: we use here the largest conversion for every char... could be optimized */
155 p = mask = HeapAlloc(GetProcessHeap(), 0, (5 * strlen(srcfile) + 1 + sz) * sizeof(WCHAR));
156 if (!mask) return NULL;
157 srcfileW = mask + 5 * strlen(srcfile) + 1;
158 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, srcfileW, sz);
160 while (*srcfileW)
162 switch (*srcfileW)
164 case '\\':
165 case '/':
166 *p++ = '[';
167 *p++ = '\\';
168 *p++ = '\\';
169 *p++ = '/';
170 *p++ = ']';
171 break;
172 case '.':
173 *p++ = '?';
174 break;
175 default:
176 *p++ = *srcfileW;
177 break;
179 srcfileW++;
182 *p = 0;
183 return mask;
186 struct symt_compiland* symt_new_compiland(struct module* module,
187 unsigned long address, unsigned src_idx)
189 struct symt_compiland* sym;
191 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
192 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
193 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
195 sym->symt.tag = SymTagCompiland;
196 sym->address = address;
197 sym->source = src_idx;
198 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
200 return sym;
203 struct symt_public* symt_new_public(struct module* module,
204 struct symt_compiland* compiland,
205 const char* name,
206 unsigned long address, unsigned size)
208 struct symt_public* sym;
209 struct symt** p;
211 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
212 debugstr_w(module->module.ModuleName), name, address);
213 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
214 symt_find_nearest(module, address) != NULL)
215 return NULL;
216 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
218 sym->symt.tag = SymTagPublicSymbol;
219 sym->hash_elt.name = pool_strdup(&module->pool, name);
220 sym->container = compiland ? &compiland->symt : NULL;
221 sym->address = address;
222 sym->size = size;
223 symt_add_module_ht(module, (struct symt_ht*)sym);
224 if (compiland)
226 p = vector_add(&compiland->vchildren, &module->pool);
227 *p = &sym->symt;
230 return sym;
233 struct symt_data* symt_new_global_variable(struct module* module,
234 struct symt_compiland* compiland,
235 const char* name, unsigned is_static,
236 struct location loc, unsigned long size,
237 struct symt* type)
239 struct symt_data* sym;
240 struct symt** p;
241 DWORD64 tsz;
243 TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
244 debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
245 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
247 sym->symt.tag = SymTagData;
248 sym->hash_elt.name = pool_strdup(&module->pool, name);
249 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
250 sym->container = compiland ? &compiland->symt : NULL;
251 sym->type = type;
252 sym->u.var = loc;
253 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
255 if (tsz != size)
256 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
257 debugstr_w(module->module.ModuleName), name,
258 wine_dbgstr_longlong(tsz), size);
260 symt_add_module_ht(module, (struct symt_ht*)sym);
261 if (compiland)
263 p = vector_add(&compiland->vchildren, &module->pool);
264 *p = &sym->symt;
267 return sym;
270 struct symt_function* symt_new_function(struct module* module,
271 struct symt_compiland* compiland,
272 const char* name,
273 unsigned long addr, unsigned long size,
274 struct symt* sig_type)
276 struct symt_function* sym;
277 struct symt** p;
279 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
280 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
282 assert(!sig_type || sig_type->tag == SymTagFunctionType);
283 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
285 sym->symt.tag = SymTagFunction;
286 sym->hash_elt.name = pool_strdup(&module->pool, name);
287 sym->container = &compiland->symt;
288 sym->address = addr;
289 sym->type = sig_type;
290 sym->size = size;
291 vector_init(&sym->vlines, sizeof(struct line_info), 64);
292 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
293 symt_add_module_ht(module, (struct symt_ht*)sym);
294 if (compiland)
296 p = vector_add(&compiland->vchildren, &module->pool);
297 *p = &sym->symt;
300 return sym;
303 void symt_add_func_line(struct module* module, struct symt_function* func,
304 unsigned source_idx, int line_num, unsigned long offset)
306 struct line_info* dli;
307 BOOL last_matches = FALSE;
308 int i;
310 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
312 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
313 func, func->hash_elt.name, offset,
314 source_get(module, source_idx), line_num);
316 assert(func->symt.tag == SymTagFunction);
318 for (i=vector_length(&func->vlines)-1; i>=0; i--)
320 dli = vector_at(&func->vlines, i);
321 if (dli->is_source_file)
323 last_matches = (source_idx == dli->u.source_file);
324 break;
328 if (!last_matches)
330 /* we shouldn't have line changes on first line of function */
331 dli = vector_add(&func->vlines, &module->pool);
332 dli->is_source_file = 1;
333 dli->is_first = dli->is_last = 0;
334 dli->line_number = 0;
335 dli->u.source_file = source_idx;
337 dli = vector_add(&func->vlines, &module->pool);
338 dli->is_source_file = 0;
339 dli->is_first = dli->is_last = 0;
340 dli->line_number = line_num;
341 dli->u.pc_offset = func->address + offset;
344 /******************************************************************
345 * symt_add_func_local
347 * Adds a new local/parameter to a given function:
348 * In any cases, dt tells whether it's a local variable or a parameter
349 * If regno it's not 0:
350 * - then variable is stored in a register
351 * - otherwise, value is referenced by register + offset
352 * Otherwise, the variable is stored on the stack:
353 * - offset is then the offset from the frame register
355 struct symt_data* symt_add_func_local(struct module* module,
356 struct symt_function* func,
357 enum DataKind dt,
358 const struct location* loc,
359 struct symt_block* block,
360 struct symt* type, const char* name)
362 struct symt_data* locsym;
363 struct symt** p;
365 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
366 debugstr_w(module->module.ModuleName), func->hash_elt.name,
367 name, type);
369 assert(func);
370 assert(func->symt.tag == SymTagFunction);
371 assert(dt == DataIsParam || dt == DataIsLocal);
373 locsym = pool_alloc(&module->pool, sizeof(*locsym));
374 locsym->symt.tag = SymTagData;
375 locsym->hash_elt.name = pool_strdup(&module->pool, name);
376 locsym->hash_elt.next = NULL;
377 locsym->kind = dt;
378 locsym->container = block ? &block->symt : &func->symt;
379 locsym->type = type;
380 locsym->u.var = *loc;
381 if (block)
382 p = vector_add(&block->vchildren, &module->pool);
383 else
384 p = vector_add(&func->vchildren, &module->pool);
385 *p = &locsym->symt;
386 return locsym;
390 struct symt_block* symt_open_func_block(struct module* module,
391 struct symt_function* func,
392 struct symt_block* parent_block,
393 unsigned pc, unsigned len)
395 struct symt_block* block;
396 struct symt** p;
398 assert(func);
399 assert(func->symt.tag == SymTagFunction);
401 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
402 block = pool_alloc(&module->pool, sizeof(*block));
403 block->symt.tag = SymTagBlock;
404 block->address = func->address + pc;
405 block->size = len;
406 block->container = parent_block ? &parent_block->symt : &func->symt;
407 vector_init(&block->vchildren, sizeof(struct symt*), 4);
408 if (parent_block)
409 p = vector_add(&parent_block->vchildren, &module->pool);
410 else
411 p = vector_add(&func->vchildren, &module->pool);
412 *p = &block->symt;
414 return block;
417 struct symt_block* symt_close_func_block(struct module* module,
418 const struct symt_function* func,
419 struct symt_block* block, unsigned pc)
421 assert(func);
422 assert(func->symt.tag == SymTagFunction);
424 if (pc) block->size = func->address + pc - block->address;
425 return (block->container->tag == SymTagBlock) ?
426 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
429 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
430 struct symt_function* func,
431 enum SymTagEnum point,
432 const struct location* loc,
433 const char* name)
435 struct symt_hierarchy_point*sym;
436 struct symt** p;
438 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
440 sym->symt.tag = point;
441 sym->parent = &func->symt;
442 sym->loc = *loc;
443 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
444 p = vector_add(&func->vchildren, &module->pool);
445 *p = &sym->symt;
447 return sym;
450 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
452 unsigned len;
453 struct line_info* dli;
455 assert(func);
456 /* We aren't adding any more locals or line numbers to this function.
457 * Free any spare memory that we might have allocated.
459 assert(func->symt.tag == SymTagFunction);
461 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
462 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
464 len = vector_length(&func->vlines);
465 if (len--)
467 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
468 dli = vector_at(&func->vlines, len); dli->is_last = 1;
470 return TRUE;
473 struct symt_thunk* symt_new_thunk(struct module* module,
474 struct symt_compiland* compiland,
475 const char* name, THUNK_ORDINAL ord,
476 unsigned long addr, unsigned long size)
478 struct symt_thunk* sym;
480 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
481 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
483 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
485 sym->symt.tag = SymTagThunk;
486 sym->hash_elt.name = pool_strdup(&module->pool, name);
487 sym->container = &compiland->symt;
488 sym->address = addr;
489 sym->size = size;
490 sym->ordinal = ord;
491 symt_add_module_ht(module, (struct symt_ht*)sym);
492 if (compiland)
494 struct symt** p;
495 p = vector_add(&compiland->vchildren, &module->pool);
496 *p = &sym->symt;
499 return sym;
502 struct symt_data* symt_new_constant(struct module* module,
503 struct symt_compiland* compiland,
504 const char* name, struct symt* type,
505 const VARIANT* v)
507 struct symt_data* sym;
509 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
510 debugstr_w(module->module.ModuleName), name);
512 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
514 sym->symt.tag = SymTagData;
515 sym->hash_elt.name = pool_strdup(&module->pool, name);
516 sym->kind = DataIsConstant;
517 sym->container = compiland ? &compiland->symt : NULL;
518 sym->type = type;
519 sym->u.value = *v;
520 symt_add_module_ht(module, (struct symt_ht*)sym);
521 if (compiland)
523 struct symt** p;
524 p = vector_add(&compiland->vchildren, &module->pool);
525 *p = &sym->symt;
528 return sym;
531 struct symt_hierarchy_point* symt_new_label(struct module* module,
532 struct symt_compiland* compiland,
533 const char* name, unsigned long address)
535 struct symt_hierarchy_point* sym;
537 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
538 debugstr_w(module->module.ModuleName), name);
540 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
542 sym->symt.tag = SymTagLabel;
543 sym->hash_elt.name = pool_strdup(&module->pool, name);
544 sym->loc.kind = loc_absolute;
545 sym->loc.offset = address;
546 sym->parent = compiland ? &compiland->symt : NULL;
547 symt_add_module_ht(module, (struct symt_ht*)sym);
548 if (compiland)
550 struct symt** p;
551 p = vector_add(&compiland->vchildren, &module->pool);
552 *p = &sym->symt;
555 return sym;
558 /* expect sym_info->MaxNameLen to be set before being called */
559 static void symt_fill_sym_info(struct module_pair* pair,
560 const struct symt_function* func,
561 const struct symt* sym, SYMBOL_INFO* sym_info)
563 const char* name;
564 DWORD64 size;
566 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
567 sym_info->TypeIndex = 0;
568 sym_info->info = symt_ptr2index(pair->effective, sym);
569 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
570 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
571 (!sym_info->TypeIndex ||
572 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
573 TI_GET_LENGTH, &size)))
574 size = 0;
575 sym_info->Size = (DWORD)size;
576 sym_info->ModBase = pair->requested->module.BaseOfImage;
577 sym_info->Flags = 0;
578 sym_info->Value = 0;
580 switch (sym->tag)
582 case SymTagData:
584 const struct symt_data* data = (const struct symt_data*)sym;
585 switch (data->kind)
587 case DataIsParam:
588 sym_info->Flags |= SYMFLAG_PARAMETER;
589 /* fall through */
590 case DataIsLocal:
591 sym_info->Flags |= SYMFLAG_LOCAL;
593 struct location loc = data->u.var;
595 if (loc.kind >= loc_user)
597 unsigned i;
598 struct module_format* modfmt;
600 for (i = 0; i < DFI_LAST; i++)
602 modfmt = pair->effective->format_info[i];
603 if (modfmt && modfmt->loc_compute)
605 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
606 break;
610 switch (loc.kind)
612 case loc_error:
613 /* for now we report error cases as a negative register number */
614 /* fall through */
615 case loc_register:
616 sym_info->Flags |= SYMFLAG_REGISTER;
617 sym_info->Register = loc.reg;
618 sym_info->Address = 0;
619 break;
620 case loc_regrel:
621 sym_info->Flags |= SYMFLAG_REGREL;
622 sym_info->Register = loc.reg;
623 if (loc.reg == CV_REG_NONE || (int)loc.reg < 0 /* error */)
624 FIXME("suspicious register value %x\n", loc.reg);
625 sym_info->Address = loc.offset;
626 break;
627 case loc_absolute:
628 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
629 sym_info->Value = loc.offset;
630 break;
631 default:
632 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
633 assert(0);
636 break;
637 case DataIsGlobal:
638 case DataIsFileStatic:
639 switch (data->u.var.kind)
641 case loc_tlsrel:
642 sym_info->Flags |= SYMFLAG_TLSREL;
643 /* fall through */
644 case loc_absolute:
645 symt_get_address(sym, &sym_info->Address);
646 sym_info->Register = 0;
647 break;
648 default:
649 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
650 assert(0);
652 break;
653 case DataIsConstant:
654 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
655 switch (data->u.value.n1.n2.vt)
657 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
658 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
659 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
660 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
661 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
662 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
663 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
664 case VT_EMPTY: sym_info->Value = 0; break;
665 default:
666 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
667 sym_info->Value = 0;
668 break;
670 break;
671 default:
672 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
675 break;
676 case SymTagPublicSymbol:
677 sym_info->Flags |= SYMFLAG_EXPORT;
678 symt_get_address(sym, &sym_info->Address);
679 break;
680 case SymTagFunction:
681 sym_info->Flags |= SYMFLAG_FUNCTION;
682 symt_get_address(sym, &sym_info->Address);
683 break;
684 case SymTagThunk:
685 sym_info->Flags |= SYMFLAG_THUNK;
686 symt_get_address(sym, &sym_info->Address);
687 break;
688 default:
689 symt_get_address(sym, &sym_info->Address);
690 sym_info->Register = 0;
691 break;
693 sym_info->Scope = 0; /* FIXME */
694 sym_info->Tag = sym->tag;
695 name = symt_get_name(sym);
696 if (sym_info->MaxNameLen)
698 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
699 ((sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
700 sym_info->MaxNameLen, UNDNAME_NAME_ONLY)) == 0))
702 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
703 memcpy(sym_info->Name, name, sym_info->NameLen);
704 sym_info->Name[sym_info->NameLen] = '\0';
707 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
708 sym, sym_info->Name, sym_info->Size,
709 wine_dbgstr_longlong(sym_info->Address));
712 struct sym_enum
714 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
715 PVOID user;
716 SYMBOL_INFO* sym_info;
717 DWORD index;
718 DWORD tag;
719 DWORD64 addr;
720 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
723 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
724 const struct symt_function* func, const struct symt* sym)
726 symt_fill_sym_info(pair, func, sym, se->sym_info);
727 if (se->index && se->sym_info->info != se->index) return FALSE;
728 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
729 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
730 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
733 static BOOL symt_enum_module(struct module_pair* pair, const WCHAR* match,
734 const struct sym_enum* se)
736 void* ptr;
737 struct symt_ht* sym = NULL;
738 struct hash_table_iter hti;
739 WCHAR* nameW;
740 BOOL ret;
742 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
743 while ((ptr = hash_table_iter_up(&hti)))
745 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
746 nameW = symt_get_nameW(&sym->symt);
747 ret = SymMatchStringW(nameW, match, FALSE);
748 HeapFree(GetProcessHeap(), 0, nameW);
749 if (ret)
751 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
752 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
753 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
756 return FALSE;
759 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
761 unsigned low = 0, mid = high / 2;
762 ULONG64 addr;
764 if (!high) return 0;
765 symt_get_address(&elt->symt, &addr);
768 switch (cmp_sorttab_addr(module, mid, addr))
770 case 0: return mid;
771 case -1: low = mid + 1; break;
772 case 1: high = mid; break;
774 mid = low + (high - low) / 2;
775 } while (low < high);
776 return mid;
779 /***********************************************************************
780 * resort_symbols
782 * Rebuild sorted list of symbols for a module.
784 static BOOL resort_symbols(struct module* module)
786 int delta;
788 if (!(module->module.NumSyms = module->num_symbols))
789 return FALSE;
791 /* we know that set from 0 up to num_sorttab is already sorted
792 * so sort the remaining (new) symbols, and merge the two sets
793 * (unless the first set is empty)
795 delta = module->num_symbols - module->num_sorttab;
796 qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
797 if (module->num_sorttab)
799 int i, ins_idx = module->num_sorttab, prev_ins_idx;
800 static struct symt_ht** tmp;
801 static unsigned num_tmp;
803 if (num_tmp < delta)
805 static struct symt_ht** new;
806 if (tmp)
807 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
808 else
809 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
810 if (!new)
812 module->num_sorttab = 0;
813 return resort_symbols(module);
815 tmp = new;
816 num_tmp = delta;
818 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
819 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
821 for (i = delta - 1; i >= 0; i--)
823 prev_ins_idx = ins_idx;
824 ins_idx = where_to_insert(module, ins_idx, tmp[i]);
825 memmove(&module->addr_sorttab[ins_idx + i + 1],
826 &module->addr_sorttab[ins_idx],
827 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
828 module->addr_sorttab[ins_idx + i] = tmp[i];
831 module->num_sorttab = module->num_symbols;
832 return module->sortlist_valid = TRUE;
835 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
837 DWORD type_index;
839 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
840 return;
842 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
843 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
844 *size = 0x1000; /* arbitrary value */
847 /* assume addr is in module */
848 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
850 int mid, high, low;
851 ULONG64 ref_addr, ref_size;
853 if (!module->sortlist_valid || !module->addr_sorttab)
855 if (!resort_symbols(module)) return NULL;
859 * Binary search to find closest symbol.
861 low = 0;
862 high = module->num_sorttab;
864 symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
865 if (addr < ref_addr) return NULL;
866 if (high)
868 symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
869 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
870 if (addr >= ref_addr + ref_size) return NULL;
873 while (high > low + 1)
875 mid = (high + low) / 2;
876 if (cmp_sorttab_addr(module, mid, addr) < 0)
877 low = mid;
878 else
879 high = mid;
881 if (low != high && high != module->num_sorttab &&
882 cmp_sorttab_addr(module, high, addr) <= 0)
883 low = high;
885 /* If found symbol is a public symbol, check if there are any other entries that
886 * might also have the same address, but would get better information
888 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
890 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
891 if (low > 0 &&
892 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
893 !cmp_sorttab_addr(module, low - 1, ref_addr))
894 low--;
895 else if (low < module->num_sorttab - 1 &&
896 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
897 !cmp_sorttab_addr(module, low + 1, ref_addr))
898 low++;
900 /* finally check that we fit into the found symbol */
901 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
902 if (addr < ref_addr) return NULL;
903 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
904 if (addr >= ref_addr + ref_size) return NULL;
906 return module->addr_sorttab[low];
909 static BOOL symt_enum_locals_helper(struct module_pair* pair,
910 const WCHAR* match, const struct sym_enum* se,
911 struct symt_function* func, const struct vector* v)
913 struct symt* lsym = NULL;
914 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
915 unsigned int i;
916 WCHAR* nameW;
917 BOOL ret;
919 for (i=0; i<vector_length(v); i++)
921 lsym = *(struct symt**)vector_at(v, i);
922 switch (lsym->tag)
924 case SymTagBlock:
926 struct symt_block* block = (struct symt_block*)lsym;
927 if (pc < block->address || block->address + block->size <= pc)
928 continue;
929 if (!symt_enum_locals_helper(pair, match, se, func, &block->vchildren))
930 return FALSE;
932 break;
933 case SymTagData:
934 nameW = symt_get_nameW(lsym);
935 ret = SymMatchStringW(nameW, match,
936 !(dbghelp_options & SYMOPT_CASE_INSENSITIVE));
937 HeapFree(GetProcessHeap(), 0, nameW);
938 if (ret)
940 if (send_symbol(se, pair, func, lsym)) return FALSE;
942 break;
943 case SymTagLabel:
944 case SymTagFuncDebugStart:
945 case SymTagFuncDebugEnd:
946 case SymTagCustom:
947 break;
948 default:
949 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
950 assert(0);
953 return TRUE;
956 static BOOL symt_enum_locals(struct process* pcs, const WCHAR* mask,
957 const struct sym_enum* se)
959 struct module_pair pair;
960 struct symt_ht* sym;
961 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
963 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
964 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
966 pair.pcs = pcs;
967 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
968 if (!module_get_debug(&pair)) return FALSE;
969 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
971 if (sym->symt.tag == SymTagFunction)
973 return symt_enum_locals_helper(&pair, mask ? mask : starW, se, (struct symt_function*)sym,
974 &((struct symt_function*)sym)->vchildren);
976 return FALSE;
979 /******************************************************************
980 * copy_symbolW
982 * Helper for transforming an ANSI symbol info into a UNICODE one.
983 * Assume that MaxNameLen is the same for both version (A & W).
985 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
987 siw->SizeOfStruct = si->SizeOfStruct;
988 siw->TypeIndex = si->TypeIndex;
989 siw->Reserved[0] = si->Reserved[0];
990 siw->Reserved[1] = si->Reserved[1];
991 siw->Index = si->info; /* FIXME: see dbghelp.h */
992 siw->Size = si->Size;
993 siw->ModBase = si->ModBase;
994 siw->Flags = si->Flags;
995 siw->Value = si->Value;
996 siw->Address = si->Address;
997 siw->Register = si->Register;
998 siw->Scope = si->Scope;
999 siw->Tag = si->Tag;
1000 siw->NameLen = si->NameLen;
1001 siw->MaxNameLen = si->MaxNameLen;
1002 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1005 /******************************************************************
1006 * sym_enum
1008 * Core routine for most of the enumeration of symbols
1010 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1011 const struct sym_enum* se)
1013 struct module_pair pair;
1014 const WCHAR* bang;
1015 WCHAR* mod;
1017 pair.pcs = process_find_by_handle(hProcess);
1018 if (!pair.pcs) return FALSE;
1019 if (BaseOfDll == 0)
1021 /* do local variables ? */
1022 if (!Mask || !(bang = strchrW(Mask, '!')))
1023 return symt_enum_locals(pair.pcs, Mask, se);
1025 if (bang == Mask) return FALSE;
1027 mod = HeapAlloc(GetProcessHeap(), 0, (bang - Mask + 1) * sizeof(WCHAR));
1028 if (!mod) return FALSE;
1029 memcpy(mod, Mask, (bang - Mask) * sizeof(WCHAR));
1030 mod[bang - Mask] = 0;
1032 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1034 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1036 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1037 symt_enum_module(&pair, bang + 1, se))
1038 break;
1041 /* not found in PE modules, retry on the ELF ones
1043 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1045 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1047 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1048 !module_get_containee(pair.pcs, pair.requested) &&
1049 module_get_debug(&pair))
1051 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1052 symt_enum_module(&pair, bang + 1, se))
1053 break;
1057 HeapFree(GetProcessHeap(), 0, mod);
1058 return TRUE;
1060 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1061 if (!module_get_debug(&pair))
1062 return FALSE;
1064 /* we always ignore module name from Mask when BaseOfDll is defined */
1065 if (Mask && (bang = strchrW(Mask, '!')))
1067 if (bang == Mask) return FALSE;
1068 Mask = bang + 1;
1071 symt_enum_module(&pair, Mask ? Mask : starW, se);
1073 return TRUE;
1076 static inline BOOL doSymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1077 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1078 PVOID UserContext)
1080 struct sym_enum se;
1082 se.cb = EnumSymbolsCallback;
1083 se.user = UserContext;
1084 se.index = 0;
1085 se.tag = 0;
1086 se.addr = 0;
1087 se.sym_info = (PSYMBOL_INFO)se.buffer;
1089 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1092 /******************************************************************
1093 * SymEnumSymbols (DBGHELP.@)
1095 * cases BaseOfDll = 0
1096 * !foo fails always (despite what MSDN states)
1097 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1098 * no ! in Mask, lookup in local Context
1099 * cases BaseOfDll != 0
1100 * !foo fails always (despite what MSDN states)
1101 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1103 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1104 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1105 PVOID UserContext)
1107 BOOL ret;
1108 PWSTR maskW = NULL;
1110 TRACE("(%p %s %s %p %p)\n",
1111 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1112 EnumSymbolsCallback, UserContext);
1114 if (Mask)
1116 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
1117 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1118 return FALSE;
1119 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
1121 ret = doSymEnumSymbols(hProcess, BaseOfDll, maskW, EnumSymbolsCallback, UserContext);
1122 HeapFree(GetProcessHeap(), 0, maskW);
1123 return ret;
1126 struct sym_enumW
1128 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1129 void* ctx;
1130 PSYMBOL_INFOW sym_info;
1131 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1135 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1137 struct sym_enumW* sew = ctx;
1139 copy_symbolW(sew->sym_info, si);
1141 return (sew->cb)(sew->sym_info, size, sew->ctx);
1144 /******************************************************************
1145 * SymEnumSymbolsW (DBGHELP.@)
1148 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1149 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1150 PVOID UserContext)
1152 struct sym_enumW sew;
1154 sew.ctx = UserContext;
1155 sew.cb = EnumSymbolsCallback;
1156 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1158 return doSymEnumSymbols(hProcess, BaseOfDll, Mask, sym_enumW, &sew);
1161 struct sym_enumerate
1163 void* ctx;
1164 PSYM_ENUMSYMBOLS_CALLBACK cb;
1167 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1169 struct sym_enumerate* se = ctx;
1170 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1173 /***********************************************************************
1174 * SymEnumerateSymbols (DBGHELP.@)
1176 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1177 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1178 PVOID UserContext)
1180 struct sym_enumerate se;
1182 se.ctx = UserContext;
1183 se.cb = EnumSymbolsCallback;
1185 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1188 struct sym_enumerate64
1190 void* ctx;
1191 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1194 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1196 struct sym_enumerate64* se = ctx;
1197 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1200 /***********************************************************************
1201 * SymEnumerateSymbols64 (DBGHELP.@)
1203 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1204 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1205 PVOID UserContext)
1207 struct sym_enumerate64 se;
1209 se.ctx = UserContext;
1210 se.cb = EnumSymbolsCallback;
1212 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1215 /******************************************************************
1216 * SymFromAddr (DBGHELP.@)
1219 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1220 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1222 struct module_pair pair;
1223 struct symt_ht* sym;
1225 pair.pcs = process_find_by_handle(hProcess);
1226 if (!pair.pcs) return FALSE;
1227 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1228 if (!module_get_debug(&pair)) return FALSE;
1229 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1231 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1232 if (Displacement)
1233 *Displacement = Address - Symbol->Address;
1234 return TRUE;
1237 /******************************************************************
1238 * SymFromAddrW (DBGHELP.@)
1241 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1242 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1244 PSYMBOL_INFO si;
1245 unsigned len;
1246 BOOL ret;
1248 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1249 si = HeapAlloc(GetProcessHeap(), 0, len);
1250 if (!si) return FALSE;
1252 si->SizeOfStruct = sizeof(*si);
1253 si->MaxNameLen = Symbol->MaxNameLen;
1254 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1256 copy_symbolW(Symbol, si);
1258 HeapFree(GetProcessHeap(), 0, si);
1259 return ret;
1262 /******************************************************************
1263 * SymGetSymFromAddr (DBGHELP.@)
1266 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1267 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1269 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1270 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1271 size_t len;
1272 DWORD64 Displacement64;
1274 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1275 si->SizeOfStruct = sizeof(*si);
1276 si->MaxNameLen = MAX_SYM_NAME;
1277 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1278 return FALSE;
1280 if (Displacement)
1281 *Displacement = Displacement64;
1282 Symbol->Address = si->Address;
1283 Symbol->Size = si->Size;
1284 Symbol->Flags = si->Flags;
1285 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1286 lstrcpynA(Symbol->Name, si->Name, len);
1287 return TRUE;
1290 /******************************************************************
1291 * SymGetSymFromAddr64 (DBGHELP.@)
1294 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1295 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1297 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1298 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1299 size_t len;
1300 DWORD64 Displacement64;
1302 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1303 si->SizeOfStruct = sizeof(*si);
1304 si->MaxNameLen = MAX_SYM_NAME;
1305 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1306 return FALSE;
1308 if (Displacement)
1309 *Displacement = Displacement64;
1310 Symbol->Address = si->Address;
1311 Symbol->Size = si->Size;
1312 Symbol->Flags = si->Flags;
1313 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1314 lstrcpynA(Symbol->Name, si->Name, len);
1315 return TRUE;
1318 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1319 SYMBOL_INFO* symbol)
1321 struct hash_table_iter hti;
1322 void* ptr;
1323 struct symt_ht* sym = NULL;
1324 struct module_pair pair;
1326 pair.pcs = pcs;
1327 if (!(pair.requested = module)) return FALSE;
1328 if (!module_get_debug(&pair)) return FALSE;
1330 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1331 while ((ptr = hash_table_iter_up(&hti)))
1333 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1335 if (!strcmp(sym->hash_elt.name, name))
1337 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1338 return TRUE;
1341 return FALSE;
1344 /******************************************************************
1345 * SymFromName (DBGHELP.@)
1348 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1350 struct process* pcs = process_find_by_handle(hProcess);
1351 struct module* module;
1352 const char* name;
1354 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1355 if (!pcs) return FALSE;
1356 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1357 name = strchr(Name, '!');
1358 if (name)
1360 char tmp[128];
1361 assert(name - Name < sizeof(tmp));
1362 memcpy(tmp, Name, name - Name);
1363 tmp[name - Name] = '\0';
1364 module = module_find_by_nameA(pcs, tmp);
1365 return find_name(pcs, module, name + 1, Symbol);
1367 for (module = pcs->lmodules; module; module = module->next)
1369 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1370 return TRUE;
1372 /* not found in PE modules, retry on the ELF ones
1374 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1376 for (module = pcs->lmodules; module; module = module->next)
1378 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1379 !module_get_containee(pcs, module) &&
1380 find_name(pcs, module, Name, Symbol))
1381 return TRUE;
1384 return FALSE;
1387 /***********************************************************************
1388 * SymGetSymFromName64 (DBGHELP.@)
1390 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1392 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1393 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1394 size_t len;
1396 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1397 si->SizeOfStruct = sizeof(*si);
1398 si->MaxNameLen = MAX_SYM_NAME;
1399 if (!SymFromName(hProcess, Name, si)) return FALSE;
1401 Symbol->Address = si->Address;
1402 Symbol->Size = si->Size;
1403 Symbol->Flags = si->Flags;
1404 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1405 lstrcpynA(Symbol->Name, si->Name, len);
1406 return TRUE;
1409 /***********************************************************************
1410 * SymGetSymFromName (DBGHELP.@)
1412 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1414 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1415 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1416 size_t len;
1418 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1419 si->SizeOfStruct = sizeof(*si);
1420 si->MaxNameLen = MAX_SYM_NAME;
1421 if (!SymFromName(hProcess, Name, si)) return FALSE;
1423 Symbol->Address = si->Address;
1424 Symbol->Size = si->Size;
1425 Symbol->Flags = si->Flags;
1426 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1427 lstrcpynA(Symbol->Name, si->Name, len);
1428 return TRUE;
1431 /******************************************************************
1432 * sym_fill_func_line_info
1434 * fills information about a file
1436 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1437 DWORD64 addr, IMAGEHLP_LINE64* line)
1439 struct line_info* dli = NULL;
1440 BOOL found = FALSE;
1441 int i;
1443 assert(func->symt.tag == SymTagFunction);
1445 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1447 dli = vector_at(&func->vlines, i);
1448 if (!dli->is_source_file)
1450 if (found || dli->u.pc_offset > addr) continue;
1451 line->LineNumber = dli->line_number;
1452 line->Address = dli->u.pc_offset;
1453 line->Key = dli;
1454 found = TRUE;
1455 continue;
1457 if (found)
1459 line->FileName = (char*)source_get(module, dli->u.source_file);
1460 return TRUE;
1463 return FALSE;
1466 /***********************************************************************
1467 * SymGetSymNext64 (DBGHELP.@)
1469 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1471 /* algo:
1472 * get module from Symbol.Address
1473 * get index in module.addr_sorttab of Symbol.Address
1474 * increment index
1475 * if out of module bounds, move to next module in process address space
1477 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1478 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1479 return FALSE;
1482 /***********************************************************************
1483 * SymGetSymNext (DBGHELP.@)
1485 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1487 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1488 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1489 return FALSE;
1492 /***********************************************************************
1493 * SymGetSymPrev64 (DBGHELP.@)
1495 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1497 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1498 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1499 return FALSE;
1502 /***********************************************************************
1503 * SymGetSymPrev (DBGHELP.@)
1505 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1507 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1508 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1509 return FALSE;
1512 /******************************************************************
1513 * copy_line_64_from_32 (internal)
1516 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1519 l64->Key = l32->Key;
1520 l64->LineNumber = l32->LineNumber;
1521 l64->FileName = l32->FileName;
1522 l64->Address = l32->Address;
1525 /******************************************************************
1526 * copy_line_W64_from_32 (internal)
1529 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1531 unsigned len;
1533 l64w->Key = l64->Key;
1534 l64w->LineNumber = l64->LineNumber;
1535 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1536 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1537 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1538 l64w->Address = l64->Address;
1541 /******************************************************************
1542 * copy_line_32_from_64 (internal)
1545 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1548 l32->Key = l64->Key;
1549 l32->LineNumber = l64->LineNumber;
1550 l32->FileName = l64->FileName;
1551 l32->Address = l64->Address;
1554 /******************************************************************
1555 * SymGetLineFromAddr (DBGHELP.@)
1558 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1559 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1561 IMAGEHLP_LINE64 il64;
1563 il64.SizeOfStruct = sizeof(il64);
1564 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1565 return FALSE;
1566 copy_line_32_from_64(Line, &il64);
1567 return TRUE;
1570 /******************************************************************
1571 * SymGetLineFromAddr64 (DBGHELP.@)
1574 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1575 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1577 struct module_pair pair;
1578 struct symt_ht* symt;
1580 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1582 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1584 pair.pcs = process_find_by_handle(hProcess);
1585 if (!pair.pcs) return FALSE;
1586 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1587 if (!module_get_debug(&pair)) return FALSE;
1588 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1590 if (symt->symt.tag != SymTagFunction) return FALSE;
1591 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1592 dwAddr, Line)) return FALSE;
1593 *pdwDisplacement = dwAddr - Line->Address;
1594 return TRUE;
1597 /******************************************************************
1598 * SymGetLineFromAddrW64 (DBGHELP.@)
1601 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1602 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1604 IMAGEHLP_LINE64 il64;
1606 il64.SizeOfStruct = sizeof(il64);
1607 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1608 return FALSE;
1609 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1610 return TRUE;
1613 /******************************************************************
1614 * SymGetLinePrev64 (DBGHELP.@)
1617 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1619 struct module_pair pair;
1620 struct line_info* li;
1621 BOOL in_search = FALSE;
1623 TRACE("(%p %p)\n", hProcess, Line);
1625 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1627 pair.pcs = process_find_by_handle(hProcess);
1628 if (!pair.pcs) return FALSE;
1629 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1630 if (!module_get_debug(&pair)) return FALSE;
1632 if (Line->Key == 0) return FALSE;
1633 li = Line->Key;
1634 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1635 * element we have to go back until we find the prev one to get the real
1636 * source file name for the DLIT_OFFSET element just before
1637 * the first DLIT_SOURCEFILE
1639 while (!li->is_first)
1641 li--;
1642 if (!li->is_source_file)
1644 Line->LineNumber = li->line_number;
1645 Line->Address = li->u.pc_offset;
1646 Line->Key = li;
1647 if (!in_search) return TRUE;
1649 else
1651 if (in_search)
1653 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1654 return TRUE;
1656 in_search = TRUE;
1659 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1660 return FALSE;
1663 /******************************************************************
1664 * SymGetLinePrev (DBGHELP.@)
1667 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1669 IMAGEHLP_LINE64 line64;
1671 line64.SizeOfStruct = sizeof(line64);
1672 copy_line_64_from_32(&line64, Line);
1673 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1674 copy_line_32_from_64(Line, &line64);
1675 return TRUE;
1678 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1680 struct line_info* li;
1682 if (line->Key == 0) return FALSE;
1683 li = line->Key;
1684 while (!li->is_last)
1686 li++;
1687 if (!li->is_source_file)
1689 line->LineNumber = li->line_number;
1690 line->Address = li->u.pc_offset;
1691 line->Key = li;
1692 return TRUE;
1694 line->FileName = (char*)source_get(module, li->u.source_file);
1696 return FALSE;
1699 /******************************************************************
1700 * SymGetLineNext64 (DBGHELP.@)
1703 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1705 struct module_pair pair;
1707 TRACE("(%p %p)\n", hProcess, Line);
1709 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1710 pair.pcs = process_find_by_handle(hProcess);
1711 if (!pair.pcs) return FALSE;
1712 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1713 if (!module_get_debug(&pair)) return FALSE;
1715 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1716 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1717 return FALSE;
1720 /******************************************************************
1721 * SymGetLineNext (DBGHELP.@)
1724 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1726 IMAGEHLP_LINE64 line64;
1728 line64.SizeOfStruct = sizeof(line64);
1729 copy_line_64_from_32(&line64, Line);
1730 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1731 copy_line_32_from_64(Line, &line64);
1732 return TRUE;
1735 /***********************************************************************
1736 * SymUnDName (DBGHELP.@)
1738 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1740 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1741 UNDNAME_COMPLETE) != 0;
1744 /***********************************************************************
1745 * SymUnDName64 (DBGHELP.@)
1747 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1749 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1750 UNDNAME_COMPLETE) != 0;
1753 static void * CDECL und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1754 static void CDECL und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1756 /***********************************************************************
1757 * UnDecorateSymbolName (DBGHELP.@)
1759 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1760 DWORD UndecoratedLength, DWORD Flags)
1762 /* undocumented from msvcrt */
1763 static HANDLE hMsvcrt;
1764 static char* (CDECL *p_undname)(char*, const char*, int, void* (CDECL*)(size_t), void (CDECL*)(void*), unsigned short);
1765 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1767 TRACE("(%s, %p, %d, 0x%08x)\n",
1768 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1770 if (!p_undname)
1772 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1773 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1774 if (!p_undname) return 0;
1777 if (!UnDecoratedName) return 0;
1778 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1779 und_alloc, und_free, Flags))
1780 return 0;
1781 return strlen(UnDecoratedName);
1784 #define WILDCHAR(x) (-(x))
1786 static int re_fetch_char(const WCHAR** re)
1788 switch (**re)
1790 case '\\': (*re)++; return *(*re)++;
1791 case '*': case '[': case '?': case '+': case '#': case ']': return WILDCHAR(*(*re)++);
1792 default: return *(*re)++;
1796 static inline int re_match_char(WCHAR ch1, WCHAR ch2, BOOL _case)
1798 return _case ? ch1 - ch2 : toupperW(ch1) - toupperW(ch2);
1801 static const WCHAR* re_match_one(const WCHAR* string, const WCHAR* elt, BOOL _case)
1803 int ch1, prev = 0;
1804 unsigned state = 0;
1806 switch (ch1 = re_fetch_char(&elt))
1808 default:
1809 return (ch1 >= 0 && re_match_char(*string, ch1, _case) == 0) ? ++string : NULL;
1810 case WILDCHAR('?'): return *string ? ++string : NULL;
1811 case WILDCHAR('*'): assert(0);
1812 case WILDCHAR('['): break;
1815 for (;;)
1817 ch1 = re_fetch_char(&elt);
1818 if (ch1 == WILDCHAR(']')) return NULL;
1819 if (state == 1 && ch1 == '-') state = 2;
1820 else
1822 if (re_match_char(*string, ch1, _case) == 0) return ++string;
1823 switch (state)
1825 case 0:
1826 state = 1;
1827 prev = ch1;
1828 break;
1829 case 1:
1830 state = 0;
1831 break;
1832 case 2:
1833 if (prev >= 0 && ch1 >= 0 && re_match_char(prev, *string, _case) <= 0 &&
1834 re_match_char(*string, ch1, _case) <= 0)
1835 return ++string;
1836 state = 0;
1837 break;
1843 /******************************************************************
1844 * re_match_multi
1846 * match a substring of *pstring according to *pre regular expression
1847 * pstring and pre are only updated in case of successful match
1849 static BOOL re_match_multi(const WCHAR** pstring, const WCHAR** pre, BOOL _case)
1851 const WCHAR* re_end = *pre;
1852 const WCHAR* string_end = *pstring;
1853 const WCHAR* re_beg;
1854 const WCHAR* string_beg;
1855 const WCHAR* next;
1856 int ch;
1858 while (*re_end && *string_end)
1860 string_beg = string_end;
1861 re_beg = re_end;
1862 switch (ch = re_fetch_char(&re_end))
1864 case WILDCHAR(']'): case WILDCHAR('+'): case WILDCHAR('#'): return FALSE;
1865 case WILDCHAR('*'):
1866 /* transform '*' into '?#' */
1867 {static const WCHAR qmW[] = {'?',0}; re_beg = qmW;}
1868 goto closure;
1869 case WILDCHAR('['):
1872 if (!(ch = re_fetch_char(&re_end))) return FALSE;
1873 } while (ch != WILDCHAR(']'));
1874 /* fall through */
1875 case WILDCHAR('?'):
1876 default:
1877 break;
1880 switch (*re_end)
1882 case '+':
1883 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1884 string_beg++;
1885 /* fall through */
1886 case '#':
1887 re_end++;
1888 closure:
1889 while ((next = re_match_one(string_end, re_beg, _case))) string_end = next;
1890 for ( ; string_end >= string_beg; string_end--)
1892 if (re_match_multi(&string_end, &re_end, _case)) goto found;
1894 return FALSE;
1895 default:
1896 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1897 string_end = next;
1899 re_beg = re_end;
1902 if (*re_end || *string_end) return FALSE;
1904 found:
1905 *pre = re_end;
1906 *pstring = string_end;
1907 return TRUE;
1910 /******************************************************************
1911 * SymMatchStringA (DBGHELP.@)
1914 BOOL WINAPI SymMatchStringA(PCSTR string, PCSTR re, BOOL _case)
1916 WCHAR* strW;
1917 WCHAR* reW;
1918 BOOL ret = FALSE;
1919 DWORD sz;
1921 if (!string || !re)
1923 SetLastError(ERROR_INVALID_HANDLE);
1924 return FALSE;
1926 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1928 sz = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);
1929 if ((strW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1930 MultiByteToWideChar(CP_ACP, 0, string, -1, strW, sz);
1931 sz = MultiByteToWideChar(CP_ACP, 0, re, -1, NULL, 0);
1932 if ((reW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1933 MultiByteToWideChar(CP_ACP, 0, re, -1, reW, sz);
1935 if (strW && reW)
1936 ret = SymMatchStringW(strW, reW, _case);
1937 HeapFree(GetProcessHeap(), 0, strW);
1938 HeapFree(GetProcessHeap(), 0, reW);
1939 return ret;
1942 /******************************************************************
1943 * SymMatchStringW (DBGHELP.@)
1946 BOOL WINAPI SymMatchStringW(PCWSTR string, PCWSTR re, BOOL _case)
1948 TRACE("%s %s %c\n", debugstr_w(string), debugstr_w(re), _case ? 'Y' : 'N');
1950 if (!string || !re)
1952 SetLastError(ERROR_INVALID_HANDLE);
1953 return FALSE;
1955 return re_match_multi(&string, &re, _case);
1958 static inline BOOL doSymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1959 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1960 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1961 PVOID UserContext, DWORD Options)
1963 struct sym_enum se;
1965 if (Options != SYMSEARCH_GLOBALSONLY)
1967 FIXME("Unsupported searching with options (%x)\n", Options);
1968 SetLastError(ERROR_INVALID_PARAMETER);
1969 return FALSE;
1972 se.cb = EnumSymbolsCallback;
1973 se.user = UserContext;
1974 se.index = Index;
1975 se.tag = SymTag;
1976 se.addr = Address;
1977 se.sym_info = (PSYMBOL_INFO)se.buffer;
1979 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1982 /******************************************************************
1983 * SymSearch (DBGHELP.@)
1985 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1986 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1987 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1988 PVOID UserContext, DWORD Options)
1990 LPWSTR maskW = NULL;
1991 BOOLEAN ret;
1993 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1994 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1995 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1996 UserContext, Options);
1998 if (Mask)
2000 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
2002 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2003 return FALSE;
2004 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
2006 ret = doSymSearch(hProcess, BaseOfDll, Index, SymTag, maskW, Address,
2007 EnumSymbolsCallback, UserContext, Options);
2008 HeapFree(GetProcessHeap(), 0, maskW);
2009 return ret;
2012 /******************************************************************
2013 * SymSearchW (DBGHELP.@)
2015 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2016 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
2017 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
2018 PVOID UserContext, DWORD Options)
2020 struct sym_enumW sew;
2022 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2023 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
2024 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
2025 UserContext, Options);
2027 sew.ctx = UserContext;
2028 sew.cb = EnumSymbolsCallback;
2029 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
2031 return doSymSearch(hProcess, BaseOfDll, Index, SymTag, Mask, Address,
2032 sym_enumW, &sew, Options);
2035 /******************************************************************
2036 * SymAddSymbol (DBGHELP.@)
2039 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
2040 DWORD64 addr, DWORD size, DWORD flags)
2042 WCHAR nameW[MAX_SYM_NAME];
2044 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
2045 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
2048 /******************************************************************
2049 * SymAddSymbolW (DBGHELP.@)
2052 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
2053 DWORD64 addr, DWORD size, DWORD flags)
2055 struct module_pair pair;
2057 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
2059 pair.pcs = process_find_by_handle(hProcess);
2060 if (!pair.pcs) return FALSE;
2061 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
2062 if (!module_get_debug(&pair)) return FALSE;
2064 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2065 return FALSE;
2068 /******************************************************************
2069 * SymSetScopeFromAddr (DBGHELP.@)
2071 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
2073 struct process* pcs;
2075 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2077 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2078 return TRUE;
2081 /******************************************************************
2082 * SymEnumLines (DBGHELP.@)
2085 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2086 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2088 struct module_pair pair;
2089 struct hash_table_iter hti;
2090 struct symt_ht* sym;
2091 WCHAR* srcmask;
2092 struct line_info* dli;
2093 void* ptr;
2094 SRCCODEINFO sci;
2095 const char* file;
2097 if (!cb) return FALSE;
2098 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2100 pair.pcs = process_find_by_handle(hProcess);
2101 if (!pair.pcs) return FALSE;
2102 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2103 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2104 if (!module_get_debug(&pair)) return FALSE;
2105 if (!(srcmask = file_regex(srcfile))) return FALSE;
2107 sci.SizeOfStruct = sizeof(sci);
2108 sci.ModBase = base;
2110 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2111 while ((ptr = hash_table_iter_up(&hti)))
2113 unsigned int i;
2115 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2116 if (sym->symt.tag != SymTagFunction) continue;
2118 sci.FileName[0] = '\0';
2119 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2121 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2122 if (dli->is_source_file)
2124 file = source_get(pair.effective, dli->u.source_file);
2125 if (!file) sci.FileName[0] = '\0';
2126 else
2128 DWORD sz = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
2129 WCHAR* fileW;
2131 if ((fileW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2132 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, sz);
2133 if (SymMatchStringW(fileW, srcmask, FALSE))
2134 strcpy(sci.FileName, file);
2135 else
2136 sci.FileName[0] = '\0';
2137 HeapFree(GetProcessHeap(), 0, fileW);
2140 else if (sci.FileName[0])
2142 sci.Key = dli;
2143 sci.Obj[0] = '\0'; /* FIXME */
2144 sci.LineNumber = dli->line_number;
2145 sci.Address = dli->u.pc_offset;
2146 if (!cb(&sci, user)) break;
2150 HeapFree(GetProcessHeap(), 0, srcmask);
2151 return TRUE;
2154 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2155 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2157 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2158 dwLineNumber, plDisplacement, Line);
2159 return FALSE;
2162 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2163 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2165 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2166 dwLineNumber, lpDisplacement, Line);
2167 return FALSE;
2170 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2171 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2173 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2174 dwLineNumber, plDisplacement, Line);
2175 return FALSE;
2178 /******************************************************************
2179 * SymFromIndex (DBGHELP.@)
2182 BOOL WINAPI SymFromIndex(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFO symbol)
2184 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2185 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2187 return FALSE;
2190 /******************************************************************
2191 * SymFromIndexW (DBGHELP.@)
2194 BOOL WINAPI SymFromIndexW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFOW symbol)
2196 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2197 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2199 return FALSE;
2202 /******************************************************************
2203 * SymSetHomeDirectory (DBGHELP.@)
2206 PCHAR WINAPI SymSetHomeDirectory(HANDLE hProcess, PCSTR dir)
2208 FIXME("(%p, %s): stub\n", hProcess, dir);
2210 return NULL;
2213 /******************************************************************
2214 * SymSetHomeDirectoryW (DBGHELP.@)
2217 PWSTR WINAPI SymSetHomeDirectoryW(HANDLE hProcess, PCWSTR dir)
2219 FIXME("(%p, %s): stub\n", hProcess, debugstr_w(dir));
2221 return NULL;