user32/listbox: Resize the entire item array at once in SetCount.
[wine.git] / dlls / dbghelp / symbol.c
blob96a1f8743c4994f33b0d163c495172e9fa3501a1
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
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <assert.h>
33 #include "wine/debug.h"
34 #include "dbghelp_private.h"
35 #include "winnls.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
38 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
40 static const WCHAR starW[] = {'*','\0'};
42 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
44 if (a1 > a2) return 1;
45 if (a1 < a2) return -1;
46 return 0;
49 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
51 ULONG64 ref;
52 symt_get_address(&module->addr_sorttab[idx]->symt, &ref);
53 return cmp_addr(ref, addr);
56 int symt_cmp_addr(const void* p1, const void* p2)
58 const struct symt* sym1 = *(const struct symt* const *)p1;
59 const struct symt* sym2 = *(const struct symt* const *)p2;
60 ULONG64 a1, a2;
62 symt_get_address(sym1, &a1);
63 symt_get_address(sym2, &a2);
64 return cmp_addr(a1, a2);
67 DWORD symt_ptr2index(struct module* module, const struct symt* sym)
69 #ifdef _WIN64
70 const struct symt** c;
71 int len = vector_length(&module->vsymt), i;
73 /* FIXME: this is inefficient */
74 for (i = 0; i < len; i++)
76 if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
77 return i + 1;
79 /* not found */
80 c = vector_add(&module->vsymt, &module->pool);
81 if (c) *c = sym;
82 return len + 1;
83 #else
84 return (DWORD)sym;
85 #endif
88 struct symt* symt_index2ptr(struct module* module, DWORD id)
90 #ifdef _WIN64
91 if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
92 return *(struct symt**)vector_at(&module->vsymt, id);
93 #else
94 return (struct symt*)id;
95 #endif
98 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
100 struct symt_ht** new;
101 unsigned int size;
103 if (sz <= module->sorttab_size) return TRUE;
104 if (module->addr_sorttab)
106 size = module->sorttab_size * 2;
107 new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
108 size * sizeof(struct symt_ht*));
110 else
112 size = 64;
113 new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
115 if (!new) return FALSE;
116 module->sorttab_size = size;
117 module->addr_sorttab = new;
118 return TRUE;
121 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
123 ULONG64 addr;
125 hash_table_add(&module->ht_symbols, &ht->hash_elt);
126 /* Don't store in sorttab a symbol without address, they are of
127 * no use here (e.g. constant values)
129 if (symt_get_address(&ht->symt, &addr) &&
130 symt_grow_sorttab(module, module->num_symbols + 1))
132 module->addr_sorttab[module->num_symbols++] = ht;
133 module->sortlist_valid = FALSE;
137 static WCHAR* file_regex(const char* srcfile)
139 WCHAR* mask;
140 WCHAR* p;
142 if (!srcfile || !*srcfile)
144 if (!(p = mask = HeapAlloc(GetProcessHeap(), 0, 3 * sizeof(WCHAR)))) return NULL;
145 *p++ = '?';
146 *p++ = '#';
148 else
150 DWORD sz = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
151 WCHAR* srcfileW;
153 /* FIXME: we use here the largest conversion for every char... could be optimized */
154 p = mask = HeapAlloc(GetProcessHeap(), 0, (5 * strlen(srcfile) + 1 + sz) * sizeof(WCHAR));
155 if (!mask) return NULL;
156 srcfileW = mask + 5 * strlen(srcfile) + 1;
157 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, srcfileW, sz);
159 while (*srcfileW)
161 switch (*srcfileW)
163 case '\\':
164 case '/':
165 *p++ = '[';
166 *p++ = '\\';
167 *p++ = '\\';
168 *p++ = '/';
169 *p++ = ']';
170 break;
171 case '.':
172 *p++ = '?';
173 break;
174 default:
175 *p++ = *srcfileW;
176 break;
178 srcfileW++;
181 *p = 0;
182 return mask;
185 struct symt_compiland* symt_new_compiland(struct module* module,
186 unsigned long address, unsigned src_idx)
188 struct symt_compiland* sym;
190 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
191 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
192 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
194 sym->symt.tag = SymTagCompiland;
195 sym->address = address;
196 sym->source = src_idx;
197 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
199 return sym;
202 struct symt_public* symt_new_public(struct module* module,
203 struct symt_compiland* compiland,
204 const char* name,
205 BOOL is_function,
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->is_function = is_function;
222 sym->address = address;
223 sym->size = size;
224 symt_add_module_ht(module, (struct symt_ht*)sym);
225 if (compiland)
227 p = vector_add(&compiland->vchildren, &module->pool);
228 *p = &sym->symt;
231 return sym;
234 struct symt_data* symt_new_global_variable(struct module* module,
235 struct symt_compiland* compiland,
236 const char* name, unsigned is_static,
237 struct location loc, unsigned long size,
238 struct symt* type)
240 struct symt_data* sym;
241 struct symt** p;
242 DWORD64 tsz;
244 TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
245 debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
246 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
248 sym->symt.tag = SymTagData;
249 sym->hash_elt.name = pool_strdup(&module->pool, name);
250 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
251 sym->container = compiland ? &compiland->symt : NULL;
252 sym->type = type;
253 sym->u.var = loc;
254 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
256 if (tsz != size)
257 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
258 debugstr_w(module->module.ModuleName), name,
259 wine_dbgstr_longlong(tsz), size);
261 symt_add_module_ht(module, (struct symt_ht*)sym);
262 if (compiland)
264 p = vector_add(&compiland->vchildren, &module->pool);
265 *p = &sym->symt;
268 return sym;
271 struct symt_function* symt_new_function(struct module* module,
272 struct symt_compiland* compiland,
273 const char* name,
274 unsigned long addr, unsigned long size,
275 struct symt* sig_type)
277 struct symt_function* sym;
278 struct symt** p;
280 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
281 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
283 assert(!sig_type || sig_type->tag == SymTagFunctionType);
284 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
286 sym->symt.tag = SymTagFunction;
287 sym->hash_elt.name = pool_strdup(&module->pool, name);
288 sym->container = &compiland->symt;
289 sym->address = addr;
290 sym->type = sig_type;
291 sym->size = size;
292 vector_init(&sym->vlines, sizeof(struct line_info), 64);
293 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
294 symt_add_module_ht(module, (struct symt_ht*)sym);
295 if (compiland)
297 p = vector_add(&compiland->vchildren, &module->pool);
298 *p = &sym->symt;
301 return sym;
304 void symt_add_func_line(struct module* module, struct symt_function* func,
305 unsigned source_idx, int line_num, unsigned long offset)
307 struct line_info* dli;
308 BOOL last_matches = FALSE;
309 int i;
311 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
313 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
314 func, func->hash_elt.name, offset,
315 source_get(module, source_idx), line_num);
317 assert(func->symt.tag == SymTagFunction);
319 for (i=vector_length(&func->vlines)-1; i>=0; i--)
321 dli = vector_at(&func->vlines, i);
322 if (dli->is_source_file)
324 last_matches = (source_idx == dli->u.source_file);
325 break;
329 if (!last_matches)
331 /* we shouldn't have line changes on first line of function */
332 dli = vector_add(&func->vlines, &module->pool);
333 dli->is_source_file = 1;
334 dli->is_first = dli->is_last = 0;
335 dli->line_number = 0;
336 dli->u.source_file = source_idx;
338 dli = vector_add(&func->vlines, &module->pool);
339 dli->is_source_file = 0;
340 dli->is_first = dli->is_last = 0;
341 dli->line_number = line_num;
342 dli->u.pc_offset = func->address + offset;
345 /******************************************************************
346 * symt_add_func_local
348 * Adds a new local/parameter to a given function:
349 * In any cases, dt tells whether it's a local variable or a parameter
350 * If regno it's not 0:
351 * - then variable is stored in a register
352 * - otherwise, value is referenced by register + offset
353 * Otherwise, the variable is stored on the stack:
354 * - offset is then the offset from the frame register
356 struct symt_data* symt_add_func_local(struct module* module,
357 struct symt_function* func,
358 enum DataKind dt,
359 const struct location* loc,
360 struct symt_block* block,
361 struct symt* type, const char* name)
363 struct symt_data* locsym;
364 struct symt** p;
366 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
367 debugstr_w(module->module.ModuleName), func->hash_elt.name,
368 name, type);
370 assert(func);
371 assert(func->symt.tag == SymTagFunction);
372 assert(dt == DataIsParam || dt == DataIsLocal);
374 locsym = pool_alloc(&module->pool, sizeof(*locsym));
375 locsym->symt.tag = SymTagData;
376 locsym->hash_elt.name = pool_strdup(&module->pool, name);
377 locsym->hash_elt.next = NULL;
378 locsym->kind = dt;
379 locsym->container = block ? &block->symt : &func->symt;
380 locsym->type = type;
381 locsym->u.var = *loc;
382 if (block)
383 p = vector_add(&block->vchildren, &module->pool);
384 else
385 p = vector_add(&func->vchildren, &module->pool);
386 *p = &locsym->symt;
387 return locsym;
391 struct symt_block* symt_open_func_block(struct module* module,
392 struct symt_function* func,
393 struct symt_block* parent_block,
394 unsigned pc, unsigned len)
396 struct symt_block* block;
397 struct symt** p;
399 assert(func);
400 assert(func->symt.tag == SymTagFunction);
402 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
403 block = pool_alloc(&module->pool, sizeof(*block));
404 block->symt.tag = SymTagBlock;
405 block->address = func->address + pc;
406 block->size = len;
407 block->container = parent_block ? &parent_block->symt : &func->symt;
408 vector_init(&block->vchildren, sizeof(struct symt*), 4);
409 if (parent_block)
410 p = vector_add(&parent_block->vchildren, &module->pool);
411 else
412 p = vector_add(&func->vchildren, &module->pool);
413 *p = &block->symt;
415 return block;
418 struct symt_block* symt_close_func_block(struct module* module,
419 const struct symt_function* func,
420 struct symt_block* block, unsigned pc)
422 assert(func);
423 assert(func->symt.tag == SymTagFunction);
425 if (pc) block->size = func->address + pc - block->address;
426 return (block->container->tag == SymTagBlock) ?
427 CONTAINING_RECORD(block->container, struct symt_block, symt) : NULL;
430 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
431 struct symt_function* func,
432 enum SymTagEnum point,
433 const struct location* loc,
434 const char* name)
436 struct symt_hierarchy_point*sym;
437 struct symt** p;
439 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
441 sym->symt.tag = point;
442 sym->parent = &func->symt;
443 sym->loc = *loc;
444 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
445 p = vector_add(&func->vchildren, &module->pool);
446 *p = &sym->symt;
448 return sym;
451 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
453 unsigned len;
454 struct line_info* dli;
456 assert(func);
457 /* We aren't adding any more locals or line numbers to this function.
458 * Free any spare memory that we might have allocated.
460 assert(func->symt.tag == SymTagFunction);
462 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
463 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
465 len = vector_length(&func->vlines);
466 if (len--)
468 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
469 dli = vector_at(&func->vlines, len); dli->is_last = 1;
471 return TRUE;
474 struct symt_thunk* symt_new_thunk(struct module* module,
475 struct symt_compiland* compiland,
476 const char* name, THUNK_ORDINAL ord,
477 unsigned long addr, unsigned long size)
479 struct symt_thunk* sym;
481 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
482 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
484 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
486 sym->symt.tag = SymTagThunk;
487 sym->hash_elt.name = pool_strdup(&module->pool, name);
488 sym->container = &compiland->symt;
489 sym->address = addr;
490 sym->size = size;
491 sym->ordinal = ord;
492 symt_add_module_ht(module, (struct symt_ht*)sym);
493 if (compiland)
495 struct symt** p;
496 p = vector_add(&compiland->vchildren, &module->pool);
497 *p = &sym->symt;
500 return sym;
503 struct symt_data* symt_new_constant(struct module* module,
504 struct symt_compiland* compiland,
505 const char* name, struct symt* type,
506 const VARIANT* v)
508 struct symt_data* sym;
510 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
511 debugstr_w(module->module.ModuleName), name);
513 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
515 sym->symt.tag = SymTagData;
516 sym->hash_elt.name = pool_strdup(&module->pool, name);
517 sym->kind = DataIsConstant;
518 sym->container = compiland ? &compiland->symt : NULL;
519 sym->type = type;
520 sym->u.value = *v;
521 symt_add_module_ht(module, (struct symt_ht*)sym);
522 if (compiland)
524 struct symt** p;
525 p = vector_add(&compiland->vchildren, &module->pool);
526 *p = &sym->symt;
529 return sym;
532 struct symt_hierarchy_point* symt_new_label(struct module* module,
533 struct symt_compiland* compiland,
534 const char* name, unsigned long address)
536 struct symt_hierarchy_point* sym;
538 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
539 debugstr_w(module->module.ModuleName), name);
541 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
543 sym->symt.tag = SymTagLabel;
544 sym->hash_elt.name = pool_strdup(&module->pool, name);
545 sym->loc.kind = loc_absolute;
546 sym->loc.offset = address;
547 sym->parent = compiland ? &compiland->symt : NULL;
548 symt_add_module_ht(module, (struct symt_ht*)sym);
549 if (compiland)
551 struct symt** p;
552 p = vector_add(&compiland->vchildren, &module->pool);
553 *p = &sym->symt;
556 return sym;
559 /* expect sym_info->MaxNameLen to be set before being called */
560 static void symt_fill_sym_info(struct module_pair* pair,
561 const struct symt_function* func,
562 const struct symt* sym, SYMBOL_INFO* sym_info)
564 const char* name;
565 DWORD64 size;
567 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
568 sym_info->TypeIndex = 0;
569 sym_info->Index = symt_ptr2index(pair->effective, sym);
570 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
571 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
572 (!sym_info->TypeIndex ||
573 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
574 TI_GET_LENGTH, &size)))
575 size = 0;
576 sym_info->Size = (DWORD)size;
577 sym_info->ModBase = pair->requested->module.BaseOfImage;
578 sym_info->Flags = 0;
579 sym_info->Value = 0;
581 switch (sym->tag)
583 case SymTagData:
585 const struct symt_data* data = (const struct symt_data*)sym;
586 switch (data->kind)
588 case DataIsParam:
589 sym_info->Flags |= SYMFLAG_PARAMETER;
590 /* fall through */
591 case DataIsLocal:
592 sym_info->Flags |= SYMFLAG_LOCAL;
594 struct location loc = data->u.var;
596 if (loc.kind >= loc_user)
598 unsigned i;
599 struct module_format* modfmt;
601 for (i = 0; i < DFI_LAST; i++)
603 modfmt = pair->effective->format_info[i];
604 if (modfmt && modfmt->loc_compute)
606 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
607 break;
611 switch (loc.kind)
613 case loc_error:
614 /* for now we report error cases as a negative register number */
615 /* fall through */
616 case loc_register:
617 sym_info->Flags |= SYMFLAG_REGISTER;
618 sym_info->Register = loc.reg;
619 sym_info->Address = 0;
620 break;
621 case loc_regrel:
622 sym_info->Flags |= SYMFLAG_REGREL;
623 sym_info->Register = loc.reg;
624 if (loc.reg == CV_REG_NONE || (int)loc.reg < 0 /* error */)
625 FIXME("suspicious register value %x\n", loc.reg);
626 sym_info->Address = loc.offset;
627 break;
628 case loc_absolute:
629 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
630 sym_info->Value = loc.offset;
631 break;
632 default:
633 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
634 assert(0);
637 break;
638 case DataIsGlobal:
639 case DataIsFileStatic:
640 switch (data->u.var.kind)
642 case loc_tlsrel:
643 sym_info->Flags |= SYMFLAG_TLSREL;
644 /* fall through */
645 case loc_absolute:
646 symt_get_address(sym, &sym_info->Address);
647 sym_info->Register = 0;
648 break;
649 default:
650 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
651 assert(0);
653 break;
654 case DataIsConstant:
655 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
656 switch (data->u.value.n1.n2.vt)
658 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
659 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
660 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
661 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
662 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
663 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
664 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
665 case VT_EMPTY: sym_info->Value = 0; break;
666 default:
667 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
668 sym_info->Value = 0;
669 break;
671 break;
672 default:
673 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
676 break;
677 case SymTagPublicSymbol:
679 const struct symt_public* pub = (const struct symt_public*)sym;
680 if (pub->is_function)
681 sym_info->Flags |= SYMFLAG_PUBLIC_CODE;
682 else
683 sym_info->Flags |= SYMFLAG_EXPORT;
684 symt_get_address(sym, &sym_info->Address);
686 break;
687 case SymTagFunction:
688 symt_get_address(sym, &sym_info->Address);
689 break;
690 case SymTagThunk:
691 sym_info->Flags |= SYMFLAG_THUNK;
692 symt_get_address(sym, &sym_info->Address);
693 break;
694 default:
695 symt_get_address(sym, &sym_info->Address);
696 sym_info->Register = 0;
697 break;
699 sym_info->Scope = 0; /* FIXME */
700 sym_info->Tag = sym->tag;
701 name = symt_get_name(sym);
702 if (sym_info->MaxNameLen)
704 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
705 ((sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
706 sym_info->MaxNameLen, UNDNAME_NAME_ONLY)) == 0))
708 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
709 memcpy(sym_info->Name, name, sym_info->NameLen);
710 sym_info->Name[sym_info->NameLen] = '\0';
713 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
714 sym, sym_info->Name, sym_info->Size,
715 wine_dbgstr_longlong(sym_info->Address));
718 struct sym_enum
720 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
721 PVOID user;
722 SYMBOL_INFO* sym_info;
723 DWORD index;
724 DWORD tag;
725 DWORD64 addr;
726 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
729 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
730 const struct symt_function* func, const struct symt* sym)
732 symt_fill_sym_info(pair, func, sym, se->sym_info);
733 if (se->index && se->sym_info->Index != se->index) return FALSE;
734 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
735 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
736 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
739 static BOOL symt_enum_module(struct module_pair* pair, const WCHAR* match,
740 const struct sym_enum* se)
742 void* ptr;
743 struct symt_ht* sym = NULL;
744 struct hash_table_iter hti;
745 WCHAR* nameW;
746 BOOL ret;
748 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
749 while ((ptr = hash_table_iter_up(&hti)))
751 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
752 nameW = symt_get_nameW(&sym->symt);
753 ret = SymMatchStringW(nameW, match, FALSE);
754 HeapFree(GetProcessHeap(), 0, nameW);
755 if (ret)
757 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
758 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
759 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
762 return FALSE;
765 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
767 unsigned low = 0, mid = high / 2;
768 ULONG64 addr;
770 if (!high) return 0;
771 symt_get_address(&elt->symt, &addr);
774 switch (cmp_sorttab_addr(module, mid, addr))
776 case 0: return mid;
777 case -1: low = mid + 1; break;
778 case 1: high = mid; break;
780 mid = low + (high - low) / 2;
781 } while (low < high);
782 return mid;
785 /***********************************************************************
786 * resort_symbols
788 * Rebuild sorted list of symbols for a module.
790 static BOOL resort_symbols(struct module* module)
792 int delta;
794 if (!(module->module.NumSyms = module->num_symbols))
795 return FALSE;
797 /* we know that set from 0 up to num_sorttab is already sorted
798 * so sort the remaining (new) symbols, and merge the two sets
799 * (unless the first set is empty)
801 delta = module->num_symbols - module->num_sorttab;
802 qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
803 if (module->num_sorttab)
805 int i, ins_idx = module->num_sorttab, prev_ins_idx;
806 static struct symt_ht** tmp;
807 static unsigned num_tmp;
809 if (num_tmp < delta)
811 static struct symt_ht** new;
812 if (tmp)
813 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
814 else
815 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
816 if (!new)
818 module->num_sorttab = 0;
819 return resort_symbols(module);
821 tmp = new;
822 num_tmp = delta;
824 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
825 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
827 for (i = delta - 1; i >= 0; i--)
829 prev_ins_idx = ins_idx;
830 ins_idx = where_to_insert(module, ins_idx, tmp[i]);
831 memmove(&module->addr_sorttab[ins_idx + i + 1],
832 &module->addr_sorttab[ins_idx],
833 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
834 module->addr_sorttab[ins_idx + i] = tmp[i];
837 module->num_sorttab = module->num_symbols;
838 return module->sortlist_valid = TRUE;
841 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
843 DWORD type_index;
845 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
846 return;
848 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
849 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
850 *size = 0x1000; /* arbitrary value */
853 /* neede by symt_find_nearest */
854 static int symt_get_best_at(struct module* module, int idx_sorttab)
856 ULONG64 ref_addr;
857 int idx_sorttab_orig = idx_sorttab;
858 if (module->addr_sorttab[idx_sorttab]->symt.tag == SymTagPublicSymbol)
860 symt_get_address(&module->addr_sorttab[idx_sorttab]->symt, &ref_addr);
861 while (idx_sorttab > 0 &&
862 module->addr_sorttab[idx_sorttab]->symt.tag == SymTagPublicSymbol &&
863 !cmp_sorttab_addr(module, idx_sorttab - 1, ref_addr))
864 idx_sorttab--;
865 if (module->addr_sorttab[idx_sorttab]->symt.tag == SymTagPublicSymbol)
867 idx_sorttab = idx_sorttab_orig;
868 while (idx_sorttab < module->num_sorttab - 1 &&
869 module->addr_sorttab[idx_sorttab]->symt.tag == SymTagPublicSymbol &&
870 !cmp_sorttab_addr(module, idx_sorttab + 1, ref_addr))
871 idx_sorttab++;
873 /* if no better symbol fond restore original */
874 if (module->addr_sorttab[idx_sorttab]->symt.tag == SymTagPublicSymbol)
875 idx_sorttab = idx_sorttab_orig;
877 return idx_sorttab;
880 /* assume addr is in module */
881 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
883 int mid, high, low;
884 ULONG64 ref_addr, ref_size;
886 if (!module->sortlist_valid || !module->addr_sorttab)
888 if (!resort_symbols(module)) return NULL;
892 * Binary search to find closest symbol.
894 low = 0;
895 high = module->num_sorttab;
897 symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
898 if (addr <= ref_addr)
900 low = symt_get_best_at(module, 0);
901 return module->addr_sorttab[low];
904 if (high)
906 symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
907 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
908 if (addr >= ref_addr + ref_size) return NULL;
911 while (high > low + 1)
913 mid = (high + low) / 2;
914 if (cmp_sorttab_addr(module, mid, addr) < 0)
915 low = mid;
916 else
917 high = mid;
919 if (low != high && high != module->num_sorttab &&
920 cmp_sorttab_addr(module, high, addr) <= 0)
921 low = high;
923 /* If found symbol is a public symbol, check if there are any other entries that
924 * might also have the same address, but would get better information
926 low = symt_get_best_at(module, low);
928 return module->addr_sorttab[low];
931 static BOOL symt_enum_locals_helper(struct module_pair* pair,
932 const WCHAR* match, const struct sym_enum* se,
933 struct symt_function* func, const struct vector* v)
935 struct symt* lsym = NULL;
936 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
937 unsigned int i;
938 WCHAR* nameW;
939 BOOL ret;
941 for (i=0; i<vector_length(v); i++)
943 lsym = *(struct symt**)vector_at(v, i);
944 switch (lsym->tag)
946 case SymTagBlock:
948 struct symt_block* block = (struct symt_block*)lsym;
949 if (pc < block->address || block->address + block->size <= pc)
950 continue;
951 if (!symt_enum_locals_helper(pair, match, se, func, &block->vchildren))
952 return FALSE;
954 break;
955 case SymTagData:
956 nameW = symt_get_nameW(lsym);
957 ret = SymMatchStringW(nameW, match,
958 !(dbghelp_options & SYMOPT_CASE_INSENSITIVE));
959 HeapFree(GetProcessHeap(), 0, nameW);
960 if (ret)
962 if (send_symbol(se, pair, func, lsym)) return FALSE;
964 break;
965 case SymTagLabel:
966 case SymTagFuncDebugStart:
967 case SymTagFuncDebugEnd:
968 case SymTagCustom:
969 break;
970 default:
971 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
972 assert(0);
975 return TRUE;
978 static BOOL symt_enum_locals(struct process* pcs, const WCHAR* mask,
979 const struct sym_enum* se)
981 struct module_pair pair;
982 struct symt_ht* sym;
983 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
985 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
986 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
988 pair.pcs = pcs;
989 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
990 if (!module_get_debug(&pair)) return FALSE;
991 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
993 if (sym->symt.tag == SymTagFunction)
995 return symt_enum_locals_helper(&pair, mask ? mask : starW, se, (struct symt_function*)sym,
996 &((struct symt_function*)sym)->vchildren);
998 return FALSE;
1001 /******************************************************************
1002 * copy_symbolW
1004 * Helper for transforming an ANSI symbol info into a UNICODE one.
1005 * Assume that MaxNameLen is the same for both version (A & W).
1007 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1009 siw->SizeOfStruct = si->SizeOfStruct;
1010 siw->TypeIndex = si->TypeIndex;
1011 siw->Reserved[0] = si->Reserved[0];
1012 siw->Reserved[1] = si->Reserved[1];
1013 siw->Index = si->Index;
1014 siw->Size = si->Size;
1015 siw->ModBase = si->ModBase;
1016 siw->Flags = si->Flags;
1017 siw->Value = si->Value;
1018 siw->Address = si->Address;
1019 siw->Register = si->Register;
1020 siw->Scope = si->Scope;
1021 siw->Tag = si->Tag;
1022 siw->NameLen = si->NameLen;
1023 siw->MaxNameLen = si->MaxNameLen;
1024 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1027 /******************************************************************
1028 * sym_enum
1030 * Core routine for most of the enumeration of symbols
1032 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1033 const struct sym_enum* se)
1035 struct module_pair pair;
1036 const WCHAR* bang;
1037 WCHAR* mod;
1039 pair.pcs = process_find_by_handle(hProcess);
1040 if (!pair.pcs) return FALSE;
1041 if (BaseOfDll == 0)
1043 /* do local variables ? */
1044 if (!Mask || !(bang = strchrW(Mask, '!')))
1045 return symt_enum_locals(pair.pcs, Mask, se);
1047 if (bang == Mask) return FALSE;
1049 mod = HeapAlloc(GetProcessHeap(), 0, (bang - Mask + 1) * sizeof(WCHAR));
1050 if (!mod) return FALSE;
1051 memcpy(mod, Mask, (bang - Mask) * sizeof(WCHAR));
1052 mod[bang - Mask] = 0;
1054 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1056 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1058 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1059 symt_enum_module(&pair, bang + 1, se))
1060 break;
1063 /* not found in PE modules, retry on the ELF ones
1065 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1067 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1069 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1070 !module_get_containee(pair.pcs, pair.requested) &&
1071 module_get_debug(&pair))
1073 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1074 symt_enum_module(&pair, bang + 1, se))
1075 break;
1079 HeapFree(GetProcessHeap(), 0, mod);
1080 return TRUE;
1082 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1083 if (!module_get_debug(&pair))
1084 return FALSE;
1086 /* we always ignore module name from Mask when BaseOfDll is defined */
1087 if (Mask && (bang = strchrW(Mask, '!')))
1089 if (bang == Mask) return FALSE;
1090 Mask = bang + 1;
1093 symt_enum_module(&pair, Mask ? Mask : starW, se);
1095 return TRUE;
1098 static inline BOOL doSymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1099 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1100 PVOID UserContext)
1102 struct sym_enum se;
1104 se.cb = EnumSymbolsCallback;
1105 se.user = UserContext;
1106 se.index = 0;
1107 se.tag = 0;
1108 se.addr = 0;
1109 se.sym_info = (PSYMBOL_INFO)se.buffer;
1111 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1114 /******************************************************************
1115 * SymEnumSymbols (DBGHELP.@)
1117 * cases BaseOfDll = 0
1118 * !foo fails always (despite what MSDN states)
1119 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1120 * no ! in Mask, lookup in local Context
1121 * cases BaseOfDll != 0
1122 * !foo fails always (despite what MSDN states)
1123 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1125 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1126 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1127 PVOID UserContext)
1129 BOOL ret;
1130 PWSTR maskW = NULL;
1132 TRACE("(%p %s %s %p %p)\n",
1133 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1134 EnumSymbolsCallback, UserContext);
1136 if (Mask)
1138 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
1139 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1140 return FALSE;
1141 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
1143 ret = doSymEnumSymbols(hProcess, BaseOfDll, maskW, EnumSymbolsCallback, UserContext);
1144 HeapFree(GetProcessHeap(), 0, maskW);
1145 return ret;
1148 struct sym_enumW
1150 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1151 void* ctx;
1152 PSYMBOL_INFOW sym_info;
1153 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1157 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1159 struct sym_enumW* sew = ctx;
1161 copy_symbolW(sew->sym_info, si);
1163 return (sew->cb)(sew->sym_info, size, sew->ctx);
1166 /******************************************************************
1167 * SymEnumSymbolsW (DBGHELP.@)
1170 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1171 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1172 PVOID UserContext)
1174 struct sym_enumW sew;
1176 sew.ctx = UserContext;
1177 sew.cb = EnumSymbolsCallback;
1178 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1180 return doSymEnumSymbols(hProcess, BaseOfDll, Mask, sym_enumW, &sew);
1183 struct sym_enumerate
1185 void* ctx;
1186 PSYM_ENUMSYMBOLS_CALLBACK cb;
1189 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1191 struct sym_enumerate* se = ctx;
1192 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1195 /***********************************************************************
1196 * SymEnumerateSymbols (DBGHELP.@)
1198 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1199 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1200 PVOID UserContext)
1202 struct sym_enumerate se;
1204 se.ctx = UserContext;
1205 se.cb = EnumSymbolsCallback;
1207 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1210 struct sym_enumerate64
1212 void* ctx;
1213 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1216 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1218 struct sym_enumerate64* se = ctx;
1219 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1222 /***********************************************************************
1223 * SymEnumerateSymbols64 (DBGHELP.@)
1225 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1226 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1227 PVOID UserContext)
1229 struct sym_enumerate64 se;
1231 se.ctx = UserContext;
1232 se.cb = EnumSymbolsCallback;
1234 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1237 /******************************************************************
1238 * SymFromAddr (DBGHELP.@)
1241 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1242 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1244 struct module_pair pair;
1245 struct symt_ht* sym;
1247 pair.pcs = process_find_by_handle(hProcess);
1248 if (!pair.pcs) return FALSE;
1249 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1250 if (!module_get_debug(&pair)) return FALSE;
1251 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1253 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1254 if (Displacement)
1255 *Displacement = (Address >= Symbol->Address) ? (Address - Symbol->Address) : (DWORD64)-1;
1256 return TRUE;
1259 /******************************************************************
1260 * SymFromAddrW (DBGHELP.@)
1263 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1264 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1266 PSYMBOL_INFO si;
1267 unsigned len;
1268 BOOL ret;
1270 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1271 si = HeapAlloc(GetProcessHeap(), 0, len);
1272 if (!si) return FALSE;
1274 si->SizeOfStruct = sizeof(*si);
1275 si->MaxNameLen = Symbol->MaxNameLen;
1276 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1278 copy_symbolW(Symbol, si);
1280 HeapFree(GetProcessHeap(), 0, si);
1281 return ret;
1284 /******************************************************************
1285 * SymGetSymFromAddr (DBGHELP.@)
1288 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1289 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1291 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1292 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1293 size_t len;
1294 DWORD64 Displacement64;
1296 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1297 si->SizeOfStruct = sizeof(*si);
1298 si->MaxNameLen = MAX_SYM_NAME;
1299 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1300 return FALSE;
1302 if (Displacement)
1303 *Displacement = Displacement64;
1304 Symbol->Address = si->Address;
1305 Symbol->Size = si->Size;
1306 Symbol->Flags = si->Flags;
1307 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1308 lstrcpynA(Symbol->Name, si->Name, len);
1309 return TRUE;
1312 /******************************************************************
1313 * SymGetSymFromAddr64 (DBGHELP.@)
1316 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1317 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1319 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1320 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1321 size_t len;
1322 DWORD64 Displacement64;
1324 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1325 si->SizeOfStruct = sizeof(*si);
1326 si->MaxNameLen = MAX_SYM_NAME;
1327 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1328 return FALSE;
1330 if (Displacement)
1331 *Displacement = Displacement64;
1332 Symbol->Address = si->Address;
1333 Symbol->Size = si->Size;
1334 Symbol->Flags = si->Flags;
1335 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1336 lstrcpynA(Symbol->Name, si->Name, len);
1337 return TRUE;
1340 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1341 SYMBOL_INFO* symbol)
1343 struct hash_table_iter hti;
1344 void* ptr;
1345 struct symt_ht* sym = NULL;
1346 struct module_pair pair;
1348 pair.pcs = pcs;
1349 if (!(pair.requested = module)) return FALSE;
1350 if (!module_get_debug(&pair)) return FALSE;
1352 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1353 while ((ptr = hash_table_iter_up(&hti)))
1355 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
1357 if (!strcmp(sym->hash_elt.name, name))
1359 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1360 return TRUE;
1363 return FALSE;
1366 /******************************************************************
1367 * SymFromName (DBGHELP.@)
1370 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1372 struct process* pcs = process_find_by_handle(hProcess);
1373 struct module* module;
1374 const char* name;
1376 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1377 if (!pcs) return FALSE;
1378 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1379 name = strchr(Name, '!');
1380 if (name)
1382 char tmp[128];
1383 assert(name - Name < sizeof(tmp));
1384 memcpy(tmp, Name, name - Name);
1385 tmp[name - Name] = '\0';
1386 module = module_find_by_nameA(pcs, tmp);
1387 return find_name(pcs, module, name + 1, Symbol);
1389 for (module = pcs->lmodules; module; module = module->next)
1391 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1392 return TRUE;
1394 /* not found in PE modules, retry on the ELF ones
1396 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1398 for (module = pcs->lmodules; module; module = module->next)
1400 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1401 !module_get_containee(pcs, module) &&
1402 find_name(pcs, module, Name, Symbol))
1403 return TRUE;
1406 return FALSE;
1409 /***********************************************************************
1410 * SymGetSymFromName64 (DBGHELP.@)
1412 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 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 * SymGetSymFromName (DBGHELP.@)
1434 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1436 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1437 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1438 size_t len;
1440 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1441 si->SizeOfStruct = sizeof(*si);
1442 si->MaxNameLen = MAX_SYM_NAME;
1443 if (!SymFromName(hProcess, Name, si)) return FALSE;
1445 Symbol->Address = si->Address;
1446 Symbol->Size = si->Size;
1447 Symbol->Flags = si->Flags;
1448 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1449 lstrcpynA(Symbol->Name, si->Name, len);
1450 return TRUE;
1453 /******************************************************************
1454 * sym_fill_func_line_info
1456 * fills information about a file
1458 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1459 DWORD64 addr, IMAGEHLP_LINE64* line)
1461 struct line_info* dli = NULL;
1462 BOOL found = FALSE;
1463 int i;
1465 assert(func->symt.tag == SymTagFunction);
1467 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1469 dli = vector_at(&func->vlines, i);
1470 if (!dli->is_source_file)
1472 if (found || dli->u.pc_offset > addr) continue;
1473 line->LineNumber = dli->line_number;
1474 line->Address = dli->u.pc_offset;
1475 line->Key = dli;
1476 found = TRUE;
1477 continue;
1479 if (found)
1481 line->FileName = (char*)source_get(module, dli->u.source_file);
1482 return TRUE;
1485 return FALSE;
1488 /***********************************************************************
1489 * SymGetSymNext64 (DBGHELP.@)
1491 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1493 /* algo:
1494 * get module from Symbol.Address
1495 * get index in module.addr_sorttab of Symbol.Address
1496 * increment index
1497 * if out of module bounds, move to next module in process address space
1499 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1500 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1501 return FALSE;
1504 /***********************************************************************
1505 * SymGetSymNext (DBGHELP.@)
1507 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1509 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1510 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1511 return FALSE;
1514 /***********************************************************************
1515 * SymGetSymPrev64 (DBGHELP.@)
1517 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1519 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1520 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1521 return FALSE;
1524 /***********************************************************************
1525 * SymGetSymPrev (DBGHELP.@)
1527 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1529 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1530 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1531 return FALSE;
1534 /******************************************************************
1535 * copy_line_64_from_32 (internal)
1538 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1541 l64->Key = l32->Key;
1542 l64->LineNumber = l32->LineNumber;
1543 l64->FileName = l32->FileName;
1544 l64->Address = l32->Address;
1547 /******************************************************************
1548 * copy_line_W64_from_32 (internal)
1551 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1553 unsigned len;
1555 l64w->Key = l64->Key;
1556 l64w->LineNumber = l64->LineNumber;
1557 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1558 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1559 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1560 l64w->Address = l64->Address;
1563 /******************************************************************
1564 * copy_line_32_from_64 (internal)
1567 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1570 l32->Key = l64->Key;
1571 l32->LineNumber = l64->LineNumber;
1572 l32->FileName = l64->FileName;
1573 l32->Address = l64->Address;
1576 /******************************************************************
1577 * SymGetLineFromAddr (DBGHELP.@)
1580 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1581 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1583 IMAGEHLP_LINE64 il64;
1585 il64.SizeOfStruct = sizeof(il64);
1586 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1587 return FALSE;
1588 copy_line_32_from_64(Line, &il64);
1589 return TRUE;
1592 /******************************************************************
1593 * SymGetLineFromAddr64 (DBGHELP.@)
1596 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1597 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1599 struct module_pair pair;
1600 struct symt_ht* symt;
1602 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1604 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1606 pair.pcs = process_find_by_handle(hProcess);
1607 if (!pair.pcs) return FALSE;
1608 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1609 if (!module_get_debug(&pair)) return FALSE;
1610 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1612 if (symt->symt.tag != SymTagFunction) return FALSE;
1613 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1614 dwAddr, Line)) return FALSE;
1615 *pdwDisplacement = dwAddr - Line->Address;
1616 return TRUE;
1619 /******************************************************************
1620 * SymGetLineFromAddrW64 (DBGHELP.@)
1623 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1624 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1626 IMAGEHLP_LINE64 il64;
1628 il64.SizeOfStruct = sizeof(il64);
1629 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1630 return FALSE;
1631 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1632 return TRUE;
1635 /******************************************************************
1636 * SymGetLinePrev64 (DBGHELP.@)
1639 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1641 struct module_pair pair;
1642 struct line_info* li;
1643 BOOL in_search = FALSE;
1645 TRACE("(%p %p)\n", hProcess, Line);
1647 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1649 pair.pcs = process_find_by_handle(hProcess);
1650 if (!pair.pcs) return FALSE;
1651 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1652 if (!module_get_debug(&pair)) return FALSE;
1654 if (Line->Key == 0) return FALSE;
1655 li = Line->Key;
1656 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1657 * element we have to go back until we find the prev one to get the real
1658 * source file name for the DLIT_OFFSET element just before
1659 * the first DLIT_SOURCEFILE
1661 while (!li->is_first)
1663 li--;
1664 if (!li->is_source_file)
1666 Line->LineNumber = li->line_number;
1667 Line->Address = li->u.pc_offset;
1668 Line->Key = li;
1669 if (!in_search) return TRUE;
1671 else
1673 if (in_search)
1675 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1676 return TRUE;
1678 in_search = TRUE;
1681 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1682 return FALSE;
1685 /******************************************************************
1686 * SymGetLinePrev (DBGHELP.@)
1689 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1691 IMAGEHLP_LINE64 line64;
1693 line64.SizeOfStruct = sizeof(line64);
1694 copy_line_64_from_32(&line64, Line);
1695 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1696 copy_line_32_from_64(Line, &line64);
1697 return TRUE;
1700 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1702 struct line_info* li;
1704 if (line->Key == 0) return FALSE;
1705 li = line->Key;
1706 while (!li->is_last)
1708 li++;
1709 if (!li->is_source_file)
1711 line->LineNumber = li->line_number;
1712 line->Address = li->u.pc_offset;
1713 line->Key = li;
1714 return TRUE;
1716 line->FileName = (char*)source_get(module, li->u.source_file);
1718 return FALSE;
1721 /******************************************************************
1722 * SymGetLineNext64 (DBGHELP.@)
1725 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1727 struct module_pair pair;
1729 TRACE("(%p %p)\n", hProcess, Line);
1731 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1732 pair.pcs = process_find_by_handle(hProcess);
1733 if (!pair.pcs) return FALSE;
1734 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1735 if (!module_get_debug(&pair)) return FALSE;
1737 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1738 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1739 return FALSE;
1742 /******************************************************************
1743 * SymGetLineNext (DBGHELP.@)
1746 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1748 IMAGEHLP_LINE64 line64;
1750 line64.SizeOfStruct = sizeof(line64);
1751 copy_line_64_from_32(&line64, Line);
1752 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1753 copy_line_32_from_64(Line, &line64);
1754 return TRUE;
1757 /***********************************************************************
1758 * SymUnDName (DBGHELP.@)
1760 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1762 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1763 UNDNAME_COMPLETE) != 0;
1766 /***********************************************************************
1767 * SymUnDName64 (DBGHELP.@)
1769 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1771 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1772 UNDNAME_COMPLETE) != 0;
1775 static void * CDECL und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1776 static void CDECL und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1778 static char *und_name(char *buffer, const char *mangled, int buflen, unsigned short flags)
1780 /* undocumented from msvcrt */
1781 static HANDLE hMsvcrt;
1782 static char* (CDECL *p_undname)(char*, const char*, int, void* (CDECL*)(size_t), void (CDECL*)(void*), unsigned short);
1783 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1785 if (!p_undname)
1787 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1788 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1789 if (!p_undname) return NULL;
1792 return p_undname(buffer, mangled, buflen, und_alloc, und_free, flags);
1795 /***********************************************************************
1796 * UnDecorateSymbolName (DBGHELP.@)
1798 DWORD WINAPI UnDecorateSymbolName(const char *decorated_name, char *undecorated_name,
1799 DWORD undecorated_length, DWORD flags)
1801 TRACE("(%s, %p, %d, 0x%08x)\n",
1802 debugstr_a(decorated_name), undecorated_name, undecorated_length, flags);
1804 if (!undecorated_name || !undecorated_length)
1805 return 0;
1806 if (!und_name(undecorated_name, decorated_name, undecorated_length, flags))
1807 return 0;
1808 return strlen(undecorated_name);
1811 /***********************************************************************
1812 * UnDecorateSymbolNameW (DBGHELP.@)
1814 DWORD WINAPI UnDecorateSymbolNameW(const WCHAR *decorated_name, WCHAR *undecorated_name,
1815 DWORD undecorated_length, DWORD flags)
1817 char *buf, *ptr;
1818 int len, ret = 0;
1820 TRACE("(%s, %p, %d, 0x%08x)\n",
1821 debugstr_w(decorated_name), undecorated_name, undecorated_length, flags);
1823 if (!undecorated_name || !undecorated_length)
1824 return 0;
1826 len = WideCharToMultiByte(CP_ACP, 0, decorated_name, -1, NULL, 0, NULL, NULL);
1827 if ((buf = HeapAlloc(GetProcessHeap(), 0, len)))
1829 WideCharToMultiByte(CP_ACP, 0, decorated_name, -1, buf, len, NULL, NULL);
1830 if ((ptr = und_name(NULL, buf, 0, flags)))
1832 MultiByteToWideChar(CP_ACP, 0, ptr, -1, undecorated_name, undecorated_length);
1833 undecorated_name[undecorated_length - 1] = 0;
1834 ret = strlenW(undecorated_name);
1835 und_free(ptr);
1837 HeapFree(GetProcessHeap(), 0, buf);
1840 return ret;
1843 #define WILDCHAR(x) (-(x))
1845 static int re_fetch_char(const WCHAR** re)
1847 switch (**re)
1849 case '\\': (*re)++; return *(*re)++;
1850 case '*': case '[': case '?': case '+': case '#': case ']': return WILDCHAR(*(*re)++);
1851 default: return *(*re)++;
1855 static inline int re_match_char(WCHAR ch1, WCHAR ch2, BOOL _case)
1857 return _case ? ch1 - ch2 : toupperW(ch1) - toupperW(ch2);
1860 static const WCHAR* re_match_one(const WCHAR* string, const WCHAR* elt, BOOL _case)
1862 int ch1, prev = 0;
1863 unsigned state = 0;
1865 switch (ch1 = re_fetch_char(&elt))
1867 default:
1868 return (ch1 >= 0 && re_match_char(*string, ch1, _case) == 0) ? ++string : NULL;
1869 case WILDCHAR('?'): return *string ? ++string : NULL;
1870 case WILDCHAR('*'): assert(0);
1871 case WILDCHAR('['): break;
1874 for (;;)
1876 ch1 = re_fetch_char(&elt);
1877 if (ch1 == WILDCHAR(']')) return NULL;
1878 if (state == 1 && ch1 == '-') state = 2;
1879 else
1881 if (re_match_char(*string, ch1, _case) == 0) return ++string;
1882 switch (state)
1884 case 0:
1885 state = 1;
1886 prev = ch1;
1887 break;
1888 case 1:
1889 state = 0;
1890 break;
1891 case 2:
1892 if (prev >= 0 && ch1 >= 0 && re_match_char(prev, *string, _case) <= 0 &&
1893 re_match_char(*string, ch1, _case) <= 0)
1894 return ++string;
1895 state = 0;
1896 break;
1902 /******************************************************************
1903 * re_match_multi
1905 * match a substring of *pstring according to *pre regular expression
1906 * pstring and pre are only updated in case of successful match
1908 static BOOL re_match_multi(const WCHAR** pstring, const WCHAR** pre, BOOL _case)
1910 const WCHAR* re_end = *pre;
1911 const WCHAR* string_end = *pstring;
1912 const WCHAR* re_beg;
1913 const WCHAR* string_beg;
1914 const WCHAR* next;
1915 int ch;
1917 while (*re_end && *string_end)
1919 string_beg = string_end;
1920 re_beg = re_end;
1921 switch (ch = re_fetch_char(&re_end))
1923 case WILDCHAR(']'): case WILDCHAR('+'): case WILDCHAR('#'): return FALSE;
1924 case WILDCHAR('*'):
1925 /* transform '*' into '?#' */
1926 {static const WCHAR qmW[] = {'?',0}; re_beg = qmW;}
1927 goto closure;
1928 case WILDCHAR('['):
1931 if (!(ch = re_fetch_char(&re_end))) return FALSE;
1932 } while (ch != WILDCHAR(']'));
1933 /* fall through */
1934 case WILDCHAR('?'):
1935 default:
1936 break;
1939 switch (*re_end)
1941 case '+':
1942 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1943 string_beg++;
1944 /* fall through */
1945 case '#':
1946 re_end++;
1947 closure:
1948 while ((next = re_match_one(string_end, re_beg, _case))) string_end = next;
1949 for ( ; string_end >= string_beg; string_end--)
1951 if (re_match_multi(&string_end, &re_end, _case)) goto found;
1953 return FALSE;
1954 default:
1955 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1956 string_end = next;
1958 re_beg = re_end;
1961 if (*re_end || *string_end) return FALSE;
1963 found:
1964 *pre = re_end;
1965 *pstring = string_end;
1966 return TRUE;
1969 /******************************************************************
1970 * SymMatchStringA (DBGHELP.@)
1973 BOOL WINAPI SymMatchStringA(PCSTR string, PCSTR re, BOOL _case)
1975 WCHAR* strW;
1976 WCHAR* reW;
1977 BOOL ret = FALSE;
1978 DWORD sz;
1980 if (!string || !re)
1982 SetLastError(ERROR_INVALID_HANDLE);
1983 return FALSE;
1985 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1987 sz = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);
1988 if ((strW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1989 MultiByteToWideChar(CP_ACP, 0, string, -1, strW, sz);
1990 sz = MultiByteToWideChar(CP_ACP, 0, re, -1, NULL, 0);
1991 if ((reW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1992 MultiByteToWideChar(CP_ACP, 0, re, -1, reW, sz);
1994 if (strW && reW)
1995 ret = SymMatchStringW(strW, reW, _case);
1996 HeapFree(GetProcessHeap(), 0, strW);
1997 HeapFree(GetProcessHeap(), 0, reW);
1998 return ret;
2001 /******************************************************************
2002 * SymMatchStringW (DBGHELP.@)
2005 BOOL WINAPI SymMatchStringW(PCWSTR string, PCWSTR re, BOOL _case)
2007 TRACE("%s %s %c\n", debugstr_w(string), debugstr_w(re), _case ? 'Y' : 'N');
2009 if (!string || !re)
2011 SetLastError(ERROR_INVALID_HANDLE);
2012 return FALSE;
2014 return re_match_multi(&string, &re, _case);
2017 static inline BOOL doSymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2018 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
2019 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
2020 PVOID UserContext, DWORD Options)
2022 struct sym_enum se;
2024 if (Options != SYMSEARCH_GLOBALSONLY)
2026 FIXME("Unsupported searching with options (%x)\n", Options);
2027 SetLastError(ERROR_INVALID_PARAMETER);
2028 return FALSE;
2031 se.cb = EnumSymbolsCallback;
2032 se.user = UserContext;
2033 se.index = Index;
2034 se.tag = SymTag;
2035 se.addr = Address;
2036 se.sym_info = (PSYMBOL_INFO)se.buffer;
2038 return sym_enum(hProcess, BaseOfDll, Mask, &se);
2041 /******************************************************************
2042 * SymSearch (DBGHELP.@)
2044 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2045 DWORD SymTag, PCSTR Mask, DWORD64 Address,
2046 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
2047 PVOID UserContext, DWORD Options)
2049 LPWSTR maskW = NULL;
2050 BOOLEAN ret;
2052 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2053 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
2054 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
2055 UserContext, Options);
2057 if (Mask)
2059 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
2061 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2062 return FALSE;
2063 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
2065 ret = doSymSearch(hProcess, BaseOfDll, Index, SymTag, maskW, Address,
2066 EnumSymbolsCallback, UserContext, Options);
2067 HeapFree(GetProcessHeap(), 0, maskW);
2068 return ret;
2071 /******************************************************************
2072 * SymSearchW (DBGHELP.@)
2074 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2075 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
2076 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
2077 PVOID UserContext, DWORD Options)
2079 struct sym_enumW sew;
2081 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2082 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
2083 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
2084 UserContext, Options);
2086 sew.ctx = UserContext;
2087 sew.cb = EnumSymbolsCallback;
2088 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
2090 return doSymSearch(hProcess, BaseOfDll, Index, SymTag, Mask, Address,
2091 sym_enumW, &sew, Options);
2094 /******************************************************************
2095 * SymAddSymbol (DBGHELP.@)
2098 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
2099 DWORD64 addr, DWORD size, DWORD flags)
2101 WCHAR nameW[MAX_SYM_NAME];
2103 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, ARRAY_SIZE(nameW));
2104 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
2107 /******************************************************************
2108 * SymAddSymbolW (DBGHELP.@)
2111 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
2112 DWORD64 addr, DWORD size, DWORD flags)
2114 struct module_pair pair;
2116 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
2118 pair.pcs = process_find_by_handle(hProcess);
2119 if (!pair.pcs) return FALSE;
2120 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
2121 if (!module_get_debug(&pair)) return FALSE;
2123 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2124 return FALSE;
2127 /******************************************************************
2128 * SymSetScopeFromAddr (DBGHELP.@)
2130 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
2132 struct process* pcs;
2134 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2136 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2137 return TRUE;
2140 /******************************************************************
2141 * SymEnumLines (DBGHELP.@)
2144 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2145 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2147 struct module_pair pair;
2148 struct hash_table_iter hti;
2149 struct symt_ht* sym;
2150 WCHAR* srcmask;
2151 struct line_info* dli;
2152 void* ptr;
2153 SRCCODEINFO sci;
2154 const char* file;
2156 if (!cb) return FALSE;
2157 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2159 pair.pcs = process_find_by_handle(hProcess);
2160 if (!pair.pcs) return FALSE;
2161 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2162 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2163 if (!module_get_debug(&pair)) return FALSE;
2164 if (!(srcmask = file_regex(srcfile))) return FALSE;
2166 sci.SizeOfStruct = sizeof(sci);
2167 sci.ModBase = base;
2169 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2170 while ((ptr = hash_table_iter_up(&hti)))
2172 unsigned int i;
2174 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
2175 if (sym->symt.tag != SymTagFunction) continue;
2177 sci.FileName[0] = '\0';
2178 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2180 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2181 if (dli->is_source_file)
2183 file = source_get(pair.effective, dli->u.source_file);
2184 if (!file) sci.FileName[0] = '\0';
2185 else
2187 DWORD sz = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
2188 WCHAR* fileW;
2190 if ((fileW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2191 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, sz);
2192 if (SymMatchStringW(fileW, srcmask, FALSE))
2193 strcpy(sci.FileName, file);
2194 else
2195 sci.FileName[0] = '\0';
2196 HeapFree(GetProcessHeap(), 0, fileW);
2199 else if (sci.FileName[0])
2201 sci.Key = dli;
2202 sci.Obj[0] = '\0'; /* FIXME */
2203 sci.LineNumber = dli->line_number;
2204 sci.Address = dli->u.pc_offset;
2205 if (!cb(&sci, user)) break;
2209 HeapFree(GetProcessHeap(), 0, srcmask);
2210 return TRUE;
2213 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2214 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2216 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2217 dwLineNumber, plDisplacement, Line);
2218 return FALSE;
2221 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2222 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2224 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2225 dwLineNumber, lpDisplacement, Line);
2226 return FALSE;
2229 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2230 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2232 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2233 dwLineNumber, plDisplacement, Line);
2234 return FALSE;
2237 /******************************************************************
2238 * SymFromIndex (DBGHELP.@)
2241 BOOL WINAPI SymFromIndex(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFO symbol)
2243 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2244 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2246 return FALSE;
2249 /******************************************************************
2250 * SymFromIndexW (DBGHELP.@)
2253 BOOL WINAPI SymFromIndexW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFOW symbol)
2255 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2256 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2258 return FALSE;
2261 /******************************************************************
2262 * SymSetHomeDirectory (DBGHELP.@)
2265 PCHAR WINAPI SymSetHomeDirectory(HANDLE hProcess, PCSTR dir)
2267 FIXME("(%p, %s): stub\n", hProcess, dir);
2269 return NULL;
2272 /******************************************************************
2273 * SymSetHomeDirectoryW (DBGHELP.@)
2276 PWSTR WINAPI SymSetHomeDirectoryW(HANDLE hProcess, PCWSTR dir)
2278 FIXME("(%p, %s): stub\n", hProcess, debugstr_w(dir));
2280 return NULL;