winemac: Fix the logic for checking if a view is already in the intended z-order.
[wine.git] / dlls / dbghelp / symbol.c
blob08ea834b57501eaedbc7ed4cdd34c2e04dac8ec9
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 unsigned long address, unsigned size)
207 struct symt_public* sym;
208 struct symt** p;
210 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
211 debugstr_w(module->module.ModuleName), name, address);
212 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
213 symt_find_nearest(module, address) != NULL)
214 return NULL;
215 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
217 sym->symt.tag = SymTagPublicSymbol;
218 sym->hash_elt.name = pool_strdup(&module->pool, name);
219 sym->container = compiland ? &compiland->symt : NULL;
220 sym->address = address;
221 sym->size = size;
222 symt_add_module_ht(module, (struct symt_ht*)sym);
223 if (compiland)
225 p = vector_add(&compiland->vchildren, &module->pool);
226 *p = &sym->symt;
229 return sym;
232 struct symt_data* symt_new_global_variable(struct module* module,
233 struct symt_compiland* compiland,
234 const char* name, unsigned is_static,
235 struct location loc, unsigned long size,
236 struct symt* type)
238 struct symt_data* sym;
239 struct symt** p;
240 DWORD64 tsz;
242 TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
243 debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
244 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
246 sym->symt.tag = SymTagData;
247 sym->hash_elt.name = pool_strdup(&module->pool, name);
248 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
249 sym->container = compiland ? &compiland->symt : NULL;
250 sym->type = type;
251 sym->u.var = loc;
252 if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
254 if (tsz != size)
255 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
256 debugstr_w(module->module.ModuleName), name,
257 wine_dbgstr_longlong(tsz), size);
259 symt_add_module_ht(module, (struct symt_ht*)sym);
260 if (compiland)
262 p = vector_add(&compiland->vchildren, &module->pool);
263 *p = &sym->symt;
266 return sym;
269 struct symt_function* symt_new_function(struct module* module,
270 struct symt_compiland* compiland,
271 const char* name,
272 unsigned long addr, unsigned long size,
273 struct symt* sig_type)
275 struct symt_function* sym;
276 struct symt** p;
278 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
279 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
281 assert(!sig_type || sig_type->tag == SymTagFunctionType);
282 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
284 sym->symt.tag = SymTagFunction;
285 sym->hash_elt.name = pool_strdup(&module->pool, name);
286 sym->container = &compiland->symt;
287 sym->address = addr;
288 sym->type = sig_type;
289 sym->size = size;
290 vector_init(&sym->vlines, sizeof(struct line_info), 64);
291 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
292 symt_add_module_ht(module, (struct symt_ht*)sym);
293 if (compiland)
295 p = vector_add(&compiland->vchildren, &module->pool);
296 *p = &sym->symt;
299 return sym;
302 void symt_add_func_line(struct module* module, struct symt_function* func,
303 unsigned source_idx, int line_num, unsigned long offset)
305 struct line_info* dli;
306 BOOL last_matches = FALSE;
307 int i;
309 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
311 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
312 func, func->hash_elt.name, offset,
313 source_get(module, source_idx), line_num);
315 assert(func->symt.tag == SymTagFunction);
317 for (i=vector_length(&func->vlines)-1; i>=0; i--)
319 dli = vector_at(&func->vlines, i);
320 if (dli->is_source_file)
322 last_matches = (source_idx == dli->u.source_file);
323 break;
327 if (!last_matches)
329 /* we shouldn't have line changes on first line of function */
330 dli = vector_add(&func->vlines, &module->pool);
331 dli->is_source_file = 1;
332 dli->is_first = dli->is_last = 0;
333 dli->line_number = 0;
334 dli->u.source_file = source_idx;
336 dli = vector_add(&func->vlines, &module->pool);
337 dli->is_source_file = 0;
338 dli->is_first = dli->is_last = 0;
339 dli->line_number = line_num;
340 dli->u.pc_offset = func->address + offset;
343 /******************************************************************
344 * symt_add_func_local
346 * Adds a new local/parameter to a given function:
347 * In any cases, dt tells whether it's a local variable or a parameter
348 * If regno it's not 0:
349 * - then variable is stored in a register
350 * - otherwise, value is referenced by register + offset
351 * Otherwise, the variable is stored on the stack:
352 * - offset is then the offset from the frame register
354 struct symt_data* symt_add_func_local(struct module* module,
355 struct symt_function* func,
356 enum DataKind dt,
357 const struct location* loc,
358 struct symt_block* block,
359 struct symt* type, const char* name)
361 struct symt_data* locsym;
362 struct symt** p;
364 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
365 debugstr_w(module->module.ModuleName), func->hash_elt.name,
366 name, type);
368 assert(func);
369 assert(func->symt.tag == SymTagFunction);
370 assert(dt == DataIsParam || dt == DataIsLocal);
372 locsym = pool_alloc(&module->pool, sizeof(*locsym));
373 locsym->symt.tag = SymTagData;
374 locsym->hash_elt.name = pool_strdup(&module->pool, name);
375 locsym->hash_elt.next = NULL;
376 locsym->kind = dt;
377 locsym->container = block ? &block->symt : &func->symt;
378 locsym->type = type;
379 locsym->u.var = *loc;
380 if (block)
381 p = vector_add(&block->vchildren, &module->pool);
382 else
383 p = vector_add(&func->vchildren, &module->pool);
384 *p = &locsym->symt;
385 return locsym;
389 struct symt_block* symt_open_func_block(struct module* module,
390 struct symt_function* func,
391 struct symt_block* parent_block,
392 unsigned pc, unsigned len)
394 struct symt_block* block;
395 struct symt** p;
397 assert(func);
398 assert(func->symt.tag == SymTagFunction);
400 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
401 block = pool_alloc(&module->pool, sizeof(*block));
402 block->symt.tag = SymTagBlock;
403 block->address = func->address + pc;
404 block->size = len;
405 block->container = parent_block ? &parent_block->symt : &func->symt;
406 vector_init(&block->vchildren, sizeof(struct symt*), 4);
407 if (parent_block)
408 p = vector_add(&parent_block->vchildren, &module->pool);
409 else
410 p = vector_add(&func->vchildren, &module->pool);
411 *p = &block->symt;
413 return block;
416 struct symt_block* symt_close_func_block(struct module* module,
417 const struct symt_function* func,
418 struct symt_block* block, unsigned pc)
420 assert(func);
421 assert(func->symt.tag == SymTagFunction);
423 if (pc) block->size = func->address + pc - block->address;
424 return (block->container->tag == SymTagBlock) ?
425 CONTAINING_RECORD(block->container, struct symt_block, symt) : NULL;
428 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
429 struct symt_function* func,
430 enum SymTagEnum point,
431 const struct location* loc,
432 const char* name)
434 struct symt_hierarchy_point*sym;
435 struct symt** p;
437 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
439 sym->symt.tag = point;
440 sym->parent = &func->symt;
441 sym->loc = *loc;
442 sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
443 p = vector_add(&func->vchildren, &module->pool);
444 *p = &sym->symt;
446 return sym;
449 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
451 unsigned len;
452 struct line_info* dli;
454 assert(func);
455 /* We aren't adding any more locals or line numbers to this function.
456 * Free any spare memory that we might have allocated.
458 assert(func->symt.tag == SymTagFunction);
460 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
461 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
463 len = vector_length(&func->vlines);
464 if (len--)
466 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
467 dli = vector_at(&func->vlines, len); dli->is_last = 1;
469 return TRUE;
472 struct symt_thunk* symt_new_thunk(struct module* module,
473 struct symt_compiland* compiland,
474 const char* name, THUNK_ORDINAL ord,
475 unsigned long addr, unsigned long size)
477 struct symt_thunk* sym;
479 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
480 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
482 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
484 sym->symt.tag = SymTagThunk;
485 sym->hash_elt.name = pool_strdup(&module->pool, name);
486 sym->container = &compiland->symt;
487 sym->address = addr;
488 sym->size = size;
489 sym->ordinal = ord;
490 symt_add_module_ht(module, (struct symt_ht*)sym);
491 if (compiland)
493 struct symt** p;
494 p = vector_add(&compiland->vchildren, &module->pool);
495 *p = &sym->symt;
498 return sym;
501 struct symt_data* symt_new_constant(struct module* module,
502 struct symt_compiland* compiland,
503 const char* name, struct symt* type,
504 const VARIANT* v)
506 struct symt_data* sym;
508 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
509 debugstr_w(module->module.ModuleName), name);
511 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
513 sym->symt.tag = SymTagData;
514 sym->hash_elt.name = pool_strdup(&module->pool, name);
515 sym->kind = DataIsConstant;
516 sym->container = compiland ? &compiland->symt : NULL;
517 sym->type = type;
518 sym->u.value = *v;
519 symt_add_module_ht(module, (struct symt_ht*)sym);
520 if (compiland)
522 struct symt** p;
523 p = vector_add(&compiland->vchildren, &module->pool);
524 *p = &sym->symt;
527 return sym;
530 struct symt_hierarchy_point* symt_new_label(struct module* module,
531 struct symt_compiland* compiland,
532 const char* name, unsigned long address)
534 struct symt_hierarchy_point* sym;
536 TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
537 debugstr_w(module->module.ModuleName), name);
539 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
541 sym->symt.tag = SymTagLabel;
542 sym->hash_elt.name = pool_strdup(&module->pool, name);
543 sym->loc.kind = loc_absolute;
544 sym->loc.offset = address;
545 sym->parent = compiland ? &compiland->symt : NULL;
546 symt_add_module_ht(module, (struct symt_ht*)sym);
547 if (compiland)
549 struct symt** p;
550 p = vector_add(&compiland->vchildren, &module->pool);
551 *p = &sym->symt;
554 return sym;
557 /* expect sym_info->MaxNameLen to be set before being called */
558 static void symt_fill_sym_info(struct module_pair* pair,
559 const struct symt_function* func,
560 const struct symt* sym, SYMBOL_INFO* sym_info)
562 const char* name;
563 DWORD64 size;
565 if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
566 sym_info->TypeIndex = 0;
567 sym_info->Index = symt_ptr2index(pair->effective, sym);
568 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
569 if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
570 (!sym_info->TypeIndex ||
571 !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
572 TI_GET_LENGTH, &size)))
573 size = 0;
574 sym_info->Size = (DWORD)size;
575 sym_info->ModBase = pair->requested->module.BaseOfImage;
576 sym_info->Flags = 0;
577 sym_info->Value = 0;
579 switch (sym->tag)
581 case SymTagData:
583 const struct symt_data* data = (const struct symt_data*)sym;
584 switch (data->kind)
586 case DataIsParam:
587 sym_info->Flags |= SYMFLAG_PARAMETER;
588 /* fall through */
589 case DataIsLocal:
590 sym_info->Flags |= SYMFLAG_LOCAL;
592 struct location loc = data->u.var;
594 if (loc.kind >= loc_user)
596 unsigned i;
597 struct module_format* modfmt;
599 for (i = 0; i < DFI_LAST; i++)
601 modfmt = pair->effective->format_info[i];
602 if (modfmt && modfmt->loc_compute)
604 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
605 break;
609 switch (loc.kind)
611 case loc_error:
612 /* for now we report error cases as a negative register number */
613 /* fall through */
614 case loc_register:
615 sym_info->Flags |= SYMFLAG_REGISTER;
616 sym_info->Register = loc.reg;
617 sym_info->Address = 0;
618 break;
619 case loc_regrel:
620 sym_info->Flags |= SYMFLAG_REGREL;
621 sym_info->Register = loc.reg;
622 if (loc.reg == CV_REG_NONE || (int)loc.reg < 0 /* error */)
623 FIXME("suspicious register value %x\n", loc.reg);
624 sym_info->Address = loc.offset;
625 break;
626 case loc_absolute:
627 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
628 sym_info->Value = loc.offset;
629 break;
630 default:
631 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
632 assert(0);
635 break;
636 case DataIsGlobal:
637 case DataIsFileStatic:
638 switch (data->u.var.kind)
640 case loc_tlsrel:
641 sym_info->Flags |= SYMFLAG_TLSREL;
642 /* fall through */
643 case loc_absolute:
644 symt_get_address(sym, &sym_info->Address);
645 sym_info->Register = 0;
646 break;
647 default:
648 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
649 assert(0);
651 break;
652 case DataIsConstant:
653 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
654 switch (data->u.value.n1.n2.vt)
656 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
657 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
658 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
659 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
660 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
661 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
662 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
663 case VT_EMPTY: sym_info->Value = 0; break;
664 default:
665 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
666 sym_info->Value = 0;
667 break;
669 break;
670 default:
671 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
674 break;
675 case SymTagPublicSymbol:
676 sym_info->Flags |= SYMFLAG_EXPORT;
677 symt_get_address(sym, &sym_info->Address);
678 break;
679 case SymTagFunction:
680 sym_info->Flags |= SYMFLAG_FUNCTION;
681 symt_get_address(sym, &sym_info->Address);
682 break;
683 case SymTagThunk:
684 sym_info->Flags |= SYMFLAG_THUNK;
685 symt_get_address(sym, &sym_info->Address);
686 break;
687 default:
688 symt_get_address(sym, &sym_info->Address);
689 sym_info->Register = 0;
690 break;
692 sym_info->Scope = 0; /* FIXME */
693 sym_info->Tag = sym->tag;
694 name = symt_get_name(sym);
695 if (sym_info->MaxNameLen)
697 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
698 ((sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
699 sym_info->MaxNameLen, UNDNAME_NAME_ONLY)) == 0))
701 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
702 memcpy(sym_info->Name, name, sym_info->NameLen);
703 sym_info->Name[sym_info->NameLen] = '\0';
706 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
707 sym, sym_info->Name, sym_info->Size,
708 wine_dbgstr_longlong(sym_info->Address));
711 struct sym_enum
713 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
714 PVOID user;
715 SYMBOL_INFO* sym_info;
716 DWORD index;
717 DWORD tag;
718 DWORD64 addr;
719 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
722 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
723 const struct symt_function* func, const struct symt* sym)
725 symt_fill_sym_info(pair, func, sym, se->sym_info);
726 if (se->index && se->sym_info->Index != se->index) return FALSE;
727 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
728 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
729 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
732 static BOOL symt_enum_module(struct module_pair* pair, const WCHAR* match,
733 const struct sym_enum* se)
735 void* ptr;
736 struct symt_ht* sym = NULL;
737 struct hash_table_iter hti;
738 WCHAR* nameW;
739 BOOL ret;
741 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
742 while ((ptr = hash_table_iter_up(&hti)))
744 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
745 nameW = symt_get_nameW(&sym->symt);
746 ret = SymMatchStringW(nameW, match, FALSE);
747 HeapFree(GetProcessHeap(), 0, nameW);
748 if (ret)
750 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
751 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
752 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
755 return FALSE;
758 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
760 unsigned low = 0, mid = high / 2;
761 ULONG64 addr;
763 if (!high) return 0;
764 symt_get_address(&elt->symt, &addr);
767 switch (cmp_sorttab_addr(module, mid, addr))
769 case 0: return mid;
770 case -1: low = mid + 1; break;
771 case 1: high = mid; break;
773 mid = low + (high - low) / 2;
774 } while (low < high);
775 return mid;
778 /***********************************************************************
779 * resort_symbols
781 * Rebuild sorted list of symbols for a module.
783 static BOOL resort_symbols(struct module* module)
785 int delta;
787 if (!(module->module.NumSyms = module->num_symbols))
788 return FALSE;
790 /* we know that set from 0 up to num_sorttab is already sorted
791 * so sort the remaining (new) symbols, and merge the two sets
792 * (unless the first set is empty)
794 delta = module->num_symbols - module->num_sorttab;
795 qsort(&module->addr_sorttab[module->num_sorttab], delta, sizeof(struct symt_ht*), symt_cmp_addr);
796 if (module->num_sorttab)
798 int i, ins_idx = module->num_sorttab, prev_ins_idx;
799 static struct symt_ht** tmp;
800 static unsigned num_tmp;
802 if (num_tmp < delta)
804 static struct symt_ht** new;
805 if (tmp)
806 new = HeapReAlloc(GetProcessHeap(), 0, tmp, delta * sizeof(struct symt_ht*));
807 else
808 new = HeapAlloc(GetProcessHeap(), 0, delta * sizeof(struct symt_ht*));
809 if (!new)
811 module->num_sorttab = 0;
812 return resort_symbols(module);
814 tmp = new;
815 num_tmp = delta;
817 memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
818 qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
820 for (i = delta - 1; i >= 0; i--)
822 prev_ins_idx = ins_idx;
823 ins_idx = where_to_insert(module, ins_idx, tmp[i]);
824 memmove(&module->addr_sorttab[ins_idx + i + 1],
825 &module->addr_sorttab[ins_idx],
826 (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
827 module->addr_sorttab[ins_idx + i] = tmp[i];
830 module->num_sorttab = module->num_symbols;
831 return module->sortlist_valid = TRUE;
834 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
836 DWORD type_index;
838 if (symt_get_info(module, symt, TI_GET_LENGTH, size) && *size)
839 return;
841 if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
842 symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
843 *size = 0x1000; /* arbitrary value */
846 /* assume addr is in module */
847 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
849 int mid, high, low;
850 ULONG64 ref_addr, ref_size;
852 if (!module->sortlist_valid || !module->addr_sorttab)
854 if (!resort_symbols(module)) return NULL;
858 * Binary search to find closest symbol.
860 low = 0;
861 high = module->num_sorttab;
863 symt_get_address(&module->addr_sorttab[0]->symt, &ref_addr);
864 if (addr < ref_addr) return NULL;
865 if (high)
867 symt_get_address(&module->addr_sorttab[high - 1]->symt, &ref_addr);
868 symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
869 if (addr >= ref_addr + ref_size) return NULL;
872 while (high > low + 1)
874 mid = (high + low) / 2;
875 if (cmp_sorttab_addr(module, mid, addr) < 0)
876 low = mid;
877 else
878 high = mid;
880 if (low != high && high != module->num_sorttab &&
881 cmp_sorttab_addr(module, high, addr) <= 0)
882 low = high;
884 /* If found symbol is a public symbol, check if there are any other entries that
885 * might also have the same address, but would get better information
887 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
889 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
890 if (low > 0 &&
891 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
892 !cmp_sorttab_addr(module, low - 1, ref_addr))
893 low--;
894 else if (low < module->num_sorttab - 1 &&
895 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
896 !cmp_sorttab_addr(module, low + 1, ref_addr))
897 low++;
899 /* finally check that we fit into the found symbol */
900 symt_get_address(&module->addr_sorttab[low]->symt, &ref_addr);
901 if (addr < ref_addr) return NULL;
902 symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
903 if (addr >= ref_addr + ref_size) return NULL;
905 return module->addr_sorttab[low];
908 static BOOL symt_enum_locals_helper(struct module_pair* pair,
909 const WCHAR* match, const struct sym_enum* se,
910 struct symt_function* func, const struct vector* v)
912 struct symt* lsym = NULL;
913 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
914 unsigned int i;
915 WCHAR* nameW;
916 BOOL ret;
918 for (i=0; i<vector_length(v); i++)
920 lsym = *(struct symt**)vector_at(v, i);
921 switch (lsym->tag)
923 case SymTagBlock:
925 struct symt_block* block = (struct symt_block*)lsym;
926 if (pc < block->address || block->address + block->size <= pc)
927 continue;
928 if (!symt_enum_locals_helper(pair, match, se, func, &block->vchildren))
929 return FALSE;
931 break;
932 case SymTagData:
933 nameW = symt_get_nameW(lsym);
934 ret = SymMatchStringW(nameW, match,
935 !(dbghelp_options & SYMOPT_CASE_INSENSITIVE));
936 HeapFree(GetProcessHeap(), 0, nameW);
937 if (ret)
939 if (send_symbol(se, pair, func, lsym)) return FALSE;
941 break;
942 case SymTagLabel:
943 case SymTagFuncDebugStart:
944 case SymTagFuncDebugEnd:
945 case SymTagCustom:
946 break;
947 default:
948 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
949 assert(0);
952 return TRUE;
955 static BOOL symt_enum_locals(struct process* pcs, const WCHAR* mask,
956 const struct sym_enum* se)
958 struct module_pair pair;
959 struct symt_ht* sym;
960 DWORD_PTR pc = pcs->ctx_frame.InstructionOffset;
962 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
963 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
965 pair.pcs = pcs;
966 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
967 if (!module_get_debug(&pair)) return FALSE;
968 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
970 if (sym->symt.tag == SymTagFunction)
972 return symt_enum_locals_helper(&pair, mask ? mask : starW, se, (struct symt_function*)sym,
973 &((struct symt_function*)sym)->vchildren);
975 return FALSE;
978 /******************************************************************
979 * copy_symbolW
981 * Helper for transforming an ANSI symbol info into a UNICODE one.
982 * Assume that MaxNameLen is the same for both version (A & W).
984 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
986 siw->SizeOfStruct = si->SizeOfStruct;
987 siw->TypeIndex = si->TypeIndex;
988 siw->Reserved[0] = si->Reserved[0];
989 siw->Reserved[1] = si->Reserved[1];
990 siw->Index = si->Index;
991 siw->Size = si->Size;
992 siw->ModBase = si->ModBase;
993 siw->Flags = si->Flags;
994 siw->Value = si->Value;
995 siw->Address = si->Address;
996 siw->Register = si->Register;
997 siw->Scope = si->Scope;
998 siw->Tag = si->Tag;
999 siw->NameLen = si->NameLen;
1000 siw->MaxNameLen = si->MaxNameLen;
1001 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1004 /******************************************************************
1005 * sym_enum
1007 * Core routine for most of the enumeration of symbols
1009 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1010 const struct sym_enum* se)
1012 struct module_pair pair;
1013 const WCHAR* bang;
1014 WCHAR* mod;
1016 pair.pcs = process_find_by_handle(hProcess);
1017 if (!pair.pcs) return FALSE;
1018 if (BaseOfDll == 0)
1020 /* do local variables ? */
1021 if (!Mask || !(bang = strchrW(Mask, '!')))
1022 return symt_enum_locals(pair.pcs, Mask, se);
1024 if (bang == Mask) return FALSE;
1026 mod = HeapAlloc(GetProcessHeap(), 0, (bang - Mask + 1) * sizeof(WCHAR));
1027 if (!mod) return FALSE;
1028 memcpy(mod, Mask, (bang - Mask) * sizeof(WCHAR));
1029 mod[bang - Mask] = 0;
1031 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1033 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1035 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1036 symt_enum_module(&pair, bang + 1, se))
1037 break;
1040 /* not found in PE modules, retry on the ELF ones
1042 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1044 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1046 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1047 !module_get_containee(pair.pcs, pair.requested) &&
1048 module_get_debug(&pair))
1050 if (SymMatchStringW(pair.requested->module.ModuleName, mod, FALSE) &&
1051 symt_enum_module(&pair, bang + 1, se))
1052 break;
1056 HeapFree(GetProcessHeap(), 0, mod);
1057 return TRUE;
1059 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1060 if (!module_get_debug(&pair))
1061 return FALSE;
1063 /* we always ignore module name from Mask when BaseOfDll is defined */
1064 if (Mask && (bang = strchrW(Mask, '!')))
1066 if (bang == Mask) return FALSE;
1067 Mask = bang + 1;
1070 symt_enum_module(&pair, Mask ? Mask : starW, se);
1072 return TRUE;
1075 static inline BOOL doSymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1076 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1077 PVOID UserContext)
1079 struct sym_enum se;
1081 se.cb = EnumSymbolsCallback;
1082 se.user = UserContext;
1083 se.index = 0;
1084 se.tag = 0;
1085 se.addr = 0;
1086 se.sym_info = (PSYMBOL_INFO)se.buffer;
1088 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1091 /******************************************************************
1092 * SymEnumSymbols (DBGHELP.@)
1094 * cases BaseOfDll = 0
1095 * !foo fails always (despite what MSDN states)
1096 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1097 * no ! in Mask, lookup in local Context
1098 * cases BaseOfDll != 0
1099 * !foo fails always (despite what MSDN states)
1100 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1102 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1103 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1104 PVOID UserContext)
1106 BOOL ret;
1107 PWSTR maskW = NULL;
1109 TRACE("(%p %s %s %p %p)\n",
1110 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1111 EnumSymbolsCallback, UserContext);
1113 if (Mask)
1115 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
1116 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1117 return FALSE;
1118 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
1120 ret = doSymEnumSymbols(hProcess, BaseOfDll, maskW, EnumSymbolsCallback, UserContext);
1121 HeapFree(GetProcessHeap(), 0, maskW);
1122 return ret;
1125 struct sym_enumW
1127 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
1128 void* ctx;
1129 PSYMBOL_INFOW sym_info;
1130 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1134 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1136 struct sym_enumW* sew = ctx;
1138 copy_symbolW(sew->sym_info, si);
1140 return (sew->cb)(sew->sym_info, size, sew->ctx);
1143 /******************************************************************
1144 * SymEnumSymbolsW (DBGHELP.@)
1147 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1148 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1149 PVOID UserContext)
1151 struct sym_enumW sew;
1153 sew.ctx = UserContext;
1154 sew.cb = EnumSymbolsCallback;
1155 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1157 return doSymEnumSymbols(hProcess, BaseOfDll, Mask, sym_enumW, &sew);
1160 struct sym_enumerate
1162 void* ctx;
1163 PSYM_ENUMSYMBOLS_CALLBACK cb;
1166 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1168 struct sym_enumerate* se = ctx;
1169 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1172 /***********************************************************************
1173 * SymEnumerateSymbols (DBGHELP.@)
1175 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1176 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1177 PVOID UserContext)
1179 struct sym_enumerate se;
1181 se.ctx = UserContext;
1182 se.cb = EnumSymbolsCallback;
1184 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1187 struct sym_enumerate64
1189 void* ctx;
1190 PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1193 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1195 struct sym_enumerate64* se = ctx;
1196 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1199 /***********************************************************************
1200 * SymEnumerateSymbols64 (DBGHELP.@)
1202 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1203 PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1204 PVOID UserContext)
1206 struct sym_enumerate64 se;
1208 se.ctx = UserContext;
1209 se.cb = EnumSymbolsCallback;
1211 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1214 /******************************************************************
1215 * SymFromAddr (DBGHELP.@)
1218 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1219 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1221 struct module_pair pair;
1222 struct symt_ht* sym;
1224 pair.pcs = process_find_by_handle(hProcess);
1225 if (!pair.pcs) return FALSE;
1226 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1227 if (!module_get_debug(&pair)) return FALSE;
1228 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1230 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1231 if (Displacement)
1232 *Displacement = Address - Symbol->Address;
1233 return TRUE;
1236 /******************************************************************
1237 * SymFromAddrW (DBGHELP.@)
1240 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1241 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1243 PSYMBOL_INFO si;
1244 unsigned len;
1245 BOOL ret;
1247 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1248 si = HeapAlloc(GetProcessHeap(), 0, len);
1249 if (!si) return FALSE;
1251 si->SizeOfStruct = sizeof(*si);
1252 si->MaxNameLen = Symbol->MaxNameLen;
1253 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1255 copy_symbolW(Symbol, si);
1257 HeapFree(GetProcessHeap(), 0, si);
1258 return ret;
1261 /******************************************************************
1262 * SymGetSymFromAddr (DBGHELP.@)
1265 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1266 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1268 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1269 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1270 size_t len;
1271 DWORD64 Displacement64;
1273 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1274 si->SizeOfStruct = sizeof(*si);
1275 si->MaxNameLen = MAX_SYM_NAME;
1276 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1277 return FALSE;
1279 if (Displacement)
1280 *Displacement = Displacement64;
1281 Symbol->Address = si->Address;
1282 Symbol->Size = si->Size;
1283 Symbol->Flags = si->Flags;
1284 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1285 lstrcpynA(Symbol->Name, si->Name, len);
1286 return TRUE;
1289 /******************************************************************
1290 * SymGetSymFromAddr64 (DBGHELP.@)
1293 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1294 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1296 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1297 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1298 size_t len;
1299 DWORD64 Displacement64;
1301 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1302 si->SizeOfStruct = sizeof(*si);
1303 si->MaxNameLen = MAX_SYM_NAME;
1304 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1305 return FALSE;
1307 if (Displacement)
1308 *Displacement = Displacement64;
1309 Symbol->Address = si->Address;
1310 Symbol->Size = si->Size;
1311 Symbol->Flags = si->Flags;
1312 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1313 lstrcpynA(Symbol->Name, si->Name, len);
1314 return TRUE;
1317 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1318 SYMBOL_INFO* symbol)
1320 struct hash_table_iter hti;
1321 void* ptr;
1322 struct symt_ht* sym = NULL;
1323 struct module_pair pair;
1325 pair.pcs = pcs;
1326 if (!(pair.requested = module)) return FALSE;
1327 if (!module_get_debug(&pair)) return FALSE;
1329 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1330 while ((ptr = hash_table_iter_up(&hti)))
1332 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
1334 if (!strcmp(sym->hash_elt.name, name))
1336 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1337 return TRUE;
1340 return FALSE;
1343 /******************************************************************
1344 * SymFromName (DBGHELP.@)
1347 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1349 struct process* pcs = process_find_by_handle(hProcess);
1350 struct module* module;
1351 const char* name;
1353 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1354 if (!pcs) return FALSE;
1355 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1356 name = strchr(Name, '!');
1357 if (name)
1359 char tmp[128];
1360 assert(name - Name < sizeof(tmp));
1361 memcpy(tmp, Name, name - Name);
1362 tmp[name - Name] = '\0';
1363 module = module_find_by_nameA(pcs, tmp);
1364 return find_name(pcs, module, name + 1, Symbol);
1366 for (module = pcs->lmodules; module; module = module->next)
1368 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1369 return TRUE;
1371 /* not found in PE modules, retry on the ELF ones
1373 if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1375 for (module = pcs->lmodules; module; module = module->next)
1377 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1378 !module_get_containee(pcs, module) &&
1379 find_name(pcs, module, Name, Symbol))
1380 return TRUE;
1383 return FALSE;
1386 /***********************************************************************
1387 * SymGetSymFromName64 (DBGHELP.@)
1389 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1391 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1392 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1393 size_t len;
1395 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1396 si->SizeOfStruct = sizeof(*si);
1397 si->MaxNameLen = MAX_SYM_NAME;
1398 if (!SymFromName(hProcess, Name, si)) return FALSE;
1400 Symbol->Address = si->Address;
1401 Symbol->Size = si->Size;
1402 Symbol->Flags = si->Flags;
1403 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1404 lstrcpynA(Symbol->Name, si->Name, len);
1405 return TRUE;
1408 /***********************************************************************
1409 * SymGetSymFromName (DBGHELP.@)
1411 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1413 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1414 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1415 size_t len;
1417 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1418 si->SizeOfStruct = sizeof(*si);
1419 si->MaxNameLen = MAX_SYM_NAME;
1420 if (!SymFromName(hProcess, Name, si)) return FALSE;
1422 Symbol->Address = si->Address;
1423 Symbol->Size = si->Size;
1424 Symbol->Flags = si->Flags;
1425 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1426 lstrcpynA(Symbol->Name, si->Name, len);
1427 return TRUE;
1430 /******************************************************************
1431 * sym_fill_func_line_info
1433 * fills information about a file
1435 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1436 DWORD64 addr, IMAGEHLP_LINE64* line)
1438 struct line_info* dli = NULL;
1439 BOOL found = FALSE;
1440 int i;
1442 assert(func->symt.tag == SymTagFunction);
1444 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1446 dli = vector_at(&func->vlines, i);
1447 if (!dli->is_source_file)
1449 if (found || dli->u.pc_offset > addr) continue;
1450 line->LineNumber = dli->line_number;
1451 line->Address = dli->u.pc_offset;
1452 line->Key = dli;
1453 found = TRUE;
1454 continue;
1456 if (found)
1458 line->FileName = (char*)source_get(module, dli->u.source_file);
1459 return TRUE;
1462 return FALSE;
1465 /***********************************************************************
1466 * SymGetSymNext64 (DBGHELP.@)
1468 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1470 /* algo:
1471 * get module from Symbol.Address
1472 * get index in module.addr_sorttab of Symbol.Address
1473 * increment index
1474 * if out of module bounds, move to next module in process address space
1476 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1477 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1478 return FALSE;
1481 /***********************************************************************
1482 * SymGetSymNext (DBGHELP.@)
1484 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1486 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1487 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1488 return FALSE;
1491 /***********************************************************************
1492 * SymGetSymPrev64 (DBGHELP.@)
1494 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1496 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1497 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1498 return FALSE;
1501 /***********************************************************************
1502 * SymGetSymPrev (DBGHELP.@)
1504 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1506 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1507 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1508 return FALSE;
1511 /******************************************************************
1512 * copy_line_64_from_32 (internal)
1515 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1518 l64->Key = l32->Key;
1519 l64->LineNumber = l32->LineNumber;
1520 l64->FileName = l32->FileName;
1521 l64->Address = l32->Address;
1524 /******************************************************************
1525 * copy_line_W64_from_32 (internal)
1528 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1530 unsigned len;
1532 l64w->Key = l64->Key;
1533 l64w->LineNumber = l64->LineNumber;
1534 len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1535 if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1536 MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1537 l64w->Address = l64->Address;
1540 /******************************************************************
1541 * copy_line_32_from_64 (internal)
1544 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1547 l32->Key = l64->Key;
1548 l32->LineNumber = l64->LineNumber;
1549 l32->FileName = l64->FileName;
1550 l32->Address = l64->Address;
1553 /******************************************************************
1554 * SymGetLineFromAddr (DBGHELP.@)
1557 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1558 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1560 IMAGEHLP_LINE64 il64;
1562 il64.SizeOfStruct = sizeof(il64);
1563 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1564 return FALSE;
1565 copy_line_32_from_64(Line, &il64);
1566 return TRUE;
1569 /******************************************************************
1570 * SymGetLineFromAddr64 (DBGHELP.@)
1573 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1574 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1576 struct module_pair pair;
1577 struct symt_ht* symt;
1579 TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1581 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1583 pair.pcs = process_find_by_handle(hProcess);
1584 if (!pair.pcs) return FALSE;
1585 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1586 if (!module_get_debug(&pair)) return FALSE;
1587 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1589 if (symt->symt.tag != SymTagFunction) return FALSE;
1590 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1591 dwAddr, Line)) return FALSE;
1592 *pdwDisplacement = dwAddr - Line->Address;
1593 return TRUE;
1596 /******************************************************************
1597 * SymGetLineFromAddrW64 (DBGHELP.@)
1600 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1601 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1603 IMAGEHLP_LINE64 il64;
1605 il64.SizeOfStruct = sizeof(il64);
1606 if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1607 return FALSE;
1608 copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1609 return TRUE;
1612 /******************************************************************
1613 * SymGetLinePrev64 (DBGHELP.@)
1616 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1618 struct module_pair pair;
1619 struct line_info* li;
1620 BOOL in_search = FALSE;
1622 TRACE("(%p %p)\n", hProcess, Line);
1624 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1626 pair.pcs = process_find_by_handle(hProcess);
1627 if (!pair.pcs) return FALSE;
1628 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1629 if (!module_get_debug(&pair)) return FALSE;
1631 if (Line->Key == 0) return FALSE;
1632 li = Line->Key;
1633 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1634 * element we have to go back until we find the prev one to get the real
1635 * source file name for the DLIT_OFFSET element just before
1636 * the first DLIT_SOURCEFILE
1638 while (!li->is_first)
1640 li--;
1641 if (!li->is_source_file)
1643 Line->LineNumber = li->line_number;
1644 Line->Address = li->u.pc_offset;
1645 Line->Key = li;
1646 if (!in_search) return TRUE;
1648 else
1650 if (in_search)
1652 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1653 return TRUE;
1655 in_search = TRUE;
1658 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1659 return FALSE;
1662 /******************************************************************
1663 * SymGetLinePrev (DBGHELP.@)
1666 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1668 IMAGEHLP_LINE64 line64;
1670 line64.SizeOfStruct = sizeof(line64);
1671 copy_line_64_from_32(&line64, Line);
1672 if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1673 copy_line_32_from_64(Line, &line64);
1674 return TRUE;
1677 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1679 struct line_info* li;
1681 if (line->Key == 0) return FALSE;
1682 li = line->Key;
1683 while (!li->is_last)
1685 li++;
1686 if (!li->is_source_file)
1688 line->LineNumber = li->line_number;
1689 line->Address = li->u.pc_offset;
1690 line->Key = li;
1691 return TRUE;
1693 line->FileName = (char*)source_get(module, li->u.source_file);
1695 return FALSE;
1698 /******************************************************************
1699 * SymGetLineNext64 (DBGHELP.@)
1702 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1704 struct module_pair pair;
1706 TRACE("(%p %p)\n", hProcess, Line);
1708 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1709 pair.pcs = process_find_by_handle(hProcess);
1710 if (!pair.pcs) return FALSE;
1711 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1712 if (!module_get_debug(&pair)) return FALSE;
1714 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1715 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1716 return FALSE;
1719 /******************************************************************
1720 * SymGetLineNext (DBGHELP.@)
1723 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1725 IMAGEHLP_LINE64 line64;
1727 line64.SizeOfStruct = sizeof(line64);
1728 copy_line_64_from_32(&line64, Line);
1729 if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1730 copy_line_32_from_64(Line, &line64);
1731 return TRUE;
1734 /***********************************************************************
1735 * SymUnDName (DBGHELP.@)
1737 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1739 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1740 UNDNAME_COMPLETE) != 0;
1743 /***********************************************************************
1744 * SymUnDName64 (DBGHELP.@)
1746 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1748 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1749 UNDNAME_COMPLETE) != 0;
1752 static void * CDECL und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1753 static void CDECL und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1755 static char *und_name(char *buffer, const char *mangled, int buflen, unsigned short flags)
1757 /* undocumented from msvcrt */
1758 static HANDLE hMsvcrt;
1759 static char* (CDECL *p_undname)(char*, const char*, int, void* (CDECL*)(size_t), void (CDECL*)(void*), unsigned short);
1760 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1762 if (!p_undname)
1764 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1765 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1766 if (!p_undname) return NULL;
1769 return p_undname(buffer, mangled, buflen, und_alloc, und_free, flags);
1772 /***********************************************************************
1773 * UnDecorateSymbolName (DBGHELP.@)
1775 DWORD WINAPI UnDecorateSymbolName(const char *decorated_name, char *undecorated_name,
1776 DWORD undecorated_length, DWORD flags)
1778 TRACE("(%s, %p, %d, 0x%08x)\n",
1779 debugstr_a(decorated_name), undecorated_name, undecorated_length, flags);
1781 if (!undecorated_name || !undecorated_length)
1782 return 0;
1783 if (!und_name(undecorated_name, decorated_name, undecorated_length, flags))
1784 return 0;
1785 return strlen(undecorated_name);
1788 /***********************************************************************
1789 * UnDecorateSymbolNameW (DBGHELP.@)
1791 DWORD WINAPI UnDecorateSymbolNameW(const WCHAR *decorated_name, WCHAR *undecorated_name,
1792 DWORD undecorated_length, DWORD flags)
1794 char *buf, *ptr;
1795 int len, ret = 0;
1797 TRACE("(%s, %p, %d, 0x%08x)\n",
1798 debugstr_w(decorated_name), undecorated_name, undecorated_length, flags);
1800 if (!undecorated_name || !undecorated_length)
1801 return 0;
1803 len = WideCharToMultiByte(CP_ACP, 0, decorated_name, -1, NULL, 0, NULL, NULL);
1804 if ((buf = HeapAlloc(GetProcessHeap(), 0, len)))
1806 WideCharToMultiByte(CP_ACP, 0, decorated_name, -1, buf, len, NULL, NULL);
1807 if ((ptr = und_name(NULL, buf, 0, flags)))
1809 MultiByteToWideChar(CP_ACP, 0, ptr, -1, undecorated_name, undecorated_length);
1810 undecorated_name[undecorated_length - 1] = 0;
1811 ret = strlenW(undecorated_name);
1812 und_free(ptr);
1814 HeapFree(GetProcessHeap(), 0, buf);
1817 return ret;
1820 #define WILDCHAR(x) (-(x))
1822 static int re_fetch_char(const WCHAR** re)
1824 switch (**re)
1826 case '\\': (*re)++; return *(*re)++;
1827 case '*': case '[': case '?': case '+': case '#': case ']': return WILDCHAR(*(*re)++);
1828 default: return *(*re)++;
1832 static inline int re_match_char(WCHAR ch1, WCHAR ch2, BOOL _case)
1834 return _case ? ch1 - ch2 : toupperW(ch1) - toupperW(ch2);
1837 static const WCHAR* re_match_one(const WCHAR* string, const WCHAR* elt, BOOL _case)
1839 int ch1, prev = 0;
1840 unsigned state = 0;
1842 switch (ch1 = re_fetch_char(&elt))
1844 default:
1845 return (ch1 >= 0 && re_match_char(*string, ch1, _case) == 0) ? ++string : NULL;
1846 case WILDCHAR('?'): return *string ? ++string : NULL;
1847 case WILDCHAR('*'): assert(0);
1848 case WILDCHAR('['): break;
1851 for (;;)
1853 ch1 = re_fetch_char(&elt);
1854 if (ch1 == WILDCHAR(']')) return NULL;
1855 if (state == 1 && ch1 == '-') state = 2;
1856 else
1858 if (re_match_char(*string, ch1, _case) == 0) return ++string;
1859 switch (state)
1861 case 0:
1862 state = 1;
1863 prev = ch1;
1864 break;
1865 case 1:
1866 state = 0;
1867 break;
1868 case 2:
1869 if (prev >= 0 && ch1 >= 0 && re_match_char(prev, *string, _case) <= 0 &&
1870 re_match_char(*string, ch1, _case) <= 0)
1871 return ++string;
1872 state = 0;
1873 break;
1879 /******************************************************************
1880 * re_match_multi
1882 * match a substring of *pstring according to *pre regular expression
1883 * pstring and pre are only updated in case of successful match
1885 static BOOL re_match_multi(const WCHAR** pstring, const WCHAR** pre, BOOL _case)
1887 const WCHAR* re_end = *pre;
1888 const WCHAR* string_end = *pstring;
1889 const WCHAR* re_beg;
1890 const WCHAR* string_beg;
1891 const WCHAR* next;
1892 int ch;
1894 while (*re_end && *string_end)
1896 string_beg = string_end;
1897 re_beg = re_end;
1898 switch (ch = re_fetch_char(&re_end))
1900 case WILDCHAR(']'): case WILDCHAR('+'): case WILDCHAR('#'): return FALSE;
1901 case WILDCHAR('*'):
1902 /* transform '*' into '?#' */
1903 {static const WCHAR qmW[] = {'?',0}; re_beg = qmW;}
1904 goto closure;
1905 case WILDCHAR('['):
1908 if (!(ch = re_fetch_char(&re_end))) return FALSE;
1909 } while (ch != WILDCHAR(']'));
1910 /* fall through */
1911 case WILDCHAR('?'):
1912 default:
1913 break;
1916 switch (*re_end)
1918 case '+':
1919 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1920 string_beg++;
1921 /* fall through */
1922 case '#':
1923 re_end++;
1924 closure:
1925 while ((next = re_match_one(string_end, re_beg, _case))) string_end = next;
1926 for ( ; string_end >= string_beg; string_end--)
1928 if (re_match_multi(&string_end, &re_end, _case)) goto found;
1930 return FALSE;
1931 default:
1932 if (!(next = re_match_one(string_end, re_beg, _case))) return FALSE;
1933 string_end = next;
1935 re_beg = re_end;
1938 if (*re_end || *string_end) return FALSE;
1940 found:
1941 *pre = re_end;
1942 *pstring = string_end;
1943 return TRUE;
1946 /******************************************************************
1947 * SymMatchStringA (DBGHELP.@)
1950 BOOL WINAPI SymMatchStringA(PCSTR string, PCSTR re, BOOL _case)
1952 WCHAR* strW;
1953 WCHAR* reW;
1954 BOOL ret = FALSE;
1955 DWORD sz;
1957 if (!string || !re)
1959 SetLastError(ERROR_INVALID_HANDLE);
1960 return FALSE;
1962 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1964 sz = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);
1965 if ((strW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1966 MultiByteToWideChar(CP_ACP, 0, string, -1, strW, sz);
1967 sz = MultiByteToWideChar(CP_ACP, 0, re, -1, NULL, 0);
1968 if ((reW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
1969 MultiByteToWideChar(CP_ACP, 0, re, -1, reW, sz);
1971 if (strW && reW)
1972 ret = SymMatchStringW(strW, reW, _case);
1973 HeapFree(GetProcessHeap(), 0, strW);
1974 HeapFree(GetProcessHeap(), 0, reW);
1975 return ret;
1978 /******************************************************************
1979 * SymMatchStringW (DBGHELP.@)
1982 BOOL WINAPI SymMatchStringW(PCWSTR string, PCWSTR re, BOOL _case)
1984 TRACE("%s %s %c\n", debugstr_w(string), debugstr_w(re), _case ? 'Y' : 'N');
1986 if (!string || !re)
1988 SetLastError(ERROR_INVALID_HANDLE);
1989 return FALSE;
1991 return re_match_multi(&string, &re, _case);
1994 static inline BOOL doSymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1995 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1996 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1997 PVOID UserContext, DWORD Options)
1999 struct sym_enum se;
2001 if (Options != SYMSEARCH_GLOBALSONLY)
2003 FIXME("Unsupported searching with options (%x)\n", Options);
2004 SetLastError(ERROR_INVALID_PARAMETER);
2005 return FALSE;
2008 se.cb = EnumSymbolsCallback;
2009 se.user = UserContext;
2010 se.index = Index;
2011 se.tag = SymTag;
2012 se.addr = Address;
2013 se.sym_info = (PSYMBOL_INFO)se.buffer;
2015 return sym_enum(hProcess, BaseOfDll, Mask, &se);
2018 /******************************************************************
2019 * SymSearch (DBGHELP.@)
2021 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2022 DWORD SymTag, PCSTR Mask, DWORD64 Address,
2023 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
2024 PVOID UserContext, DWORD Options)
2026 LPWSTR maskW = NULL;
2027 BOOLEAN ret;
2029 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2030 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
2031 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
2032 UserContext, Options);
2034 if (Mask)
2036 DWORD sz = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
2038 if (!(maskW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2039 return FALSE;
2040 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, sz);
2042 ret = doSymSearch(hProcess, BaseOfDll, Index, SymTag, maskW, Address,
2043 EnumSymbolsCallback, UserContext, Options);
2044 HeapFree(GetProcessHeap(), 0, maskW);
2045 return ret;
2048 /******************************************************************
2049 * SymSearchW (DBGHELP.@)
2051 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
2052 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
2053 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
2054 PVOID UserContext, DWORD Options)
2056 struct sym_enumW sew;
2058 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
2059 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
2060 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
2061 UserContext, Options);
2063 sew.ctx = UserContext;
2064 sew.cb = EnumSymbolsCallback;
2065 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
2067 return doSymSearch(hProcess, BaseOfDll, Index, SymTag, Mask, Address,
2068 sym_enumW, &sew, Options);
2071 /******************************************************************
2072 * SymAddSymbol (DBGHELP.@)
2075 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
2076 DWORD64 addr, DWORD size, DWORD flags)
2078 WCHAR nameW[MAX_SYM_NAME];
2080 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
2081 return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
2084 /******************************************************************
2085 * SymAddSymbolW (DBGHELP.@)
2088 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
2089 DWORD64 addr, DWORD size, DWORD flags)
2091 struct module_pair pair;
2093 TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
2095 pair.pcs = process_find_by_handle(hProcess);
2096 if (!pair.pcs) return FALSE;
2097 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
2098 if (!module_get_debug(&pair)) return FALSE;
2100 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2101 return FALSE;
2104 /******************************************************************
2105 * SymSetScopeFromAddr (DBGHELP.@)
2107 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
2109 struct process* pcs;
2111 FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
2113 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
2114 return TRUE;
2117 /******************************************************************
2118 * SymEnumLines (DBGHELP.@)
2121 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
2122 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
2124 struct module_pair pair;
2125 struct hash_table_iter hti;
2126 struct symt_ht* sym;
2127 WCHAR* srcmask;
2128 struct line_info* dli;
2129 void* ptr;
2130 SRCCODEINFO sci;
2131 const char* file;
2133 if (!cb) return FALSE;
2134 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2136 pair.pcs = process_find_by_handle(hProcess);
2137 if (!pair.pcs) return FALSE;
2138 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2139 pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2140 if (!module_get_debug(&pair)) return FALSE;
2141 if (!(srcmask = file_regex(srcfile))) return FALSE;
2143 sci.SizeOfStruct = sizeof(sci);
2144 sci.ModBase = base;
2146 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2147 while ((ptr = hash_table_iter_up(&hti)))
2149 unsigned int i;
2151 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
2152 if (sym->symt.tag != SymTagFunction) continue;
2154 sci.FileName[0] = '\0';
2155 for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2157 dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2158 if (dli->is_source_file)
2160 file = source_get(pair.effective, dli->u.source_file);
2161 if (!file) sci.FileName[0] = '\0';
2162 else
2164 DWORD sz = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
2165 WCHAR* fileW;
2167 if ((fileW = HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR))))
2168 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, sz);
2169 if (SymMatchStringW(fileW, srcmask, FALSE))
2170 strcpy(sci.FileName, file);
2171 else
2172 sci.FileName[0] = '\0';
2173 HeapFree(GetProcessHeap(), 0, fileW);
2176 else if (sci.FileName[0])
2178 sci.Key = dli;
2179 sci.Obj[0] = '\0'; /* FIXME */
2180 sci.LineNumber = dli->line_number;
2181 sci.Address = dli->u.pc_offset;
2182 if (!cb(&sci, user)) break;
2186 HeapFree(GetProcessHeap(), 0, srcmask);
2187 return TRUE;
2190 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2191 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2193 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2194 dwLineNumber, plDisplacement, Line);
2195 return FALSE;
2198 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2199 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2201 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2202 dwLineNumber, lpDisplacement, Line);
2203 return FALSE;
2206 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2207 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2209 FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2210 dwLineNumber, plDisplacement, Line);
2211 return FALSE;
2214 /******************************************************************
2215 * SymFromIndex (DBGHELP.@)
2218 BOOL WINAPI SymFromIndex(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFO symbol)
2220 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2221 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2223 return FALSE;
2226 /******************************************************************
2227 * SymFromIndexW (DBGHELP.@)
2230 BOOL WINAPI SymFromIndexW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD index, PSYMBOL_INFOW symbol)
2232 FIXME("hProcess = %p, BaseOfDll = %s, index = %d, symbol = %p\n",
2233 hProcess, wine_dbgstr_longlong(BaseOfDll), index, symbol);
2235 return FALSE;
2238 /******************************************************************
2239 * SymSetHomeDirectory (DBGHELP.@)
2242 PCHAR WINAPI SymSetHomeDirectory(HANDLE hProcess, PCSTR dir)
2244 FIXME("(%p, %s): stub\n", hProcess, dir);
2246 return NULL;
2249 /******************************************************************
2250 * SymSetHomeDirectoryW (DBGHELP.@)
2253 PWSTR WINAPI SymSetHomeDirectoryW(HANDLE hProcess, PCWSTR dir)
2255 FIXME("(%p, %s): stub\n", hProcess, debugstr_w(dir));
2257 return NULL;