kernel32: Fix the size of ThreadAffinityMask to match PSDK.
[wine/multimedia.git] / dlls / dbghelp / symbol.c
bloba5bb6f089798cda8d7c27a6c9d5e68a20fb0dd17
1 /*
2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <assert.h>
33 #ifdef HAVE_REGEX_H
34 # include <regex.h>
35 #endif
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
39 #include "winnls.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
44 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
46 if (a1 > a2) return 1;
47 if (a1 < a2) return -1;
48 return 0;
51 static inline int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
53 ULONG64 ref;
55 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
56 return cmp_addr(ref, addr);
59 int symt_cmp_addr(const void* p1, const void* p2)
61 const struct symt* sym1 = *(const struct symt* const *)p1;
62 const struct symt* sym2 = *(const struct symt* const *)p2;
63 ULONG64 a1, a2;
65 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
66 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
67 return cmp_addr(a1, a2);
70 static inline void re_append(char** mask, unsigned* len, char ch)
72 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
73 (*mask)[*len - 2] = ch;
76 /* transforms a dbghelp's regular expression into a POSIX one
77 * Here are the valid dbghelp reg ex characters:
78 * * 0 or more characters
79 * ? a single character
80 * [] list
81 * # 0 or more of preceding char
82 * + 1 or more of preceding char
83 * escapes \ on #, ?, [, ], *, +. don't work on -
85 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
87 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
88 unsigned len = 1;
89 BOOL in_escape = FALSE;
90 unsigned flags = REG_NOSUB;
92 re_append(&mask, &len, '^');
94 while (*str && numchar--)
96 /* FIXME: this shouldn't be valid on '-' */
97 if (in_escape)
99 re_append(&mask, &len, '\\');
100 re_append(&mask, &len, *str);
101 in_escape = FALSE;
103 else switch (*str)
105 case '\\': in_escape = TRUE; break;
106 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
107 case '?': re_append(&mask, &len, '.'); break;
108 case '#': re_append(&mask, &len, '*'); break;
109 /* escape some valid characters in dbghelp reg exp:s */
110 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
111 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
112 default: re_append(&mask, &len, *str); break;
114 str++;
116 if (in_escape)
118 re_append(&mask, &len, '\\');
119 re_append(&mask, &len, '\\');
121 re_append(&mask, &len, '$');
122 mask[len - 1] = '\0';
123 if (_case) flags |= REG_ICASE;
124 if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
125 HeapFree(GetProcessHeap(), 0, mask);
128 struct symt_compiland* symt_new_compiland(struct module* module,
129 unsigned long address, unsigned src_idx)
131 struct symt_compiland* sym;
133 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
134 debugstr_w(module->module.ModuleName), source_get(module, src_idx));
135 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
137 sym->symt.tag = SymTagCompiland;
138 sym->address = address;
139 sym->source = src_idx;
140 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
142 return sym;
145 struct symt_public* symt_new_public(struct module* module,
146 struct symt_compiland* compiland,
147 const char* name,
148 unsigned long address, unsigned size,
149 BOOL in_code, BOOL is_func)
151 struct symt_public* sym;
152 struct symt** p;
154 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
155 debugstr_w(module->module.ModuleName), name, address);
156 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
157 symt_find_nearest(module, address) != NULL)
158 return NULL;
159 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
161 sym->symt.tag = SymTagPublicSymbol;
162 sym->hash_elt.name = pool_strdup(&module->pool, name);
163 hash_table_add(&module->ht_symbols, &sym->hash_elt);
164 module->sortlist_valid = FALSE;
165 sym->container = compiland ? &compiland->symt : NULL;
166 sym->address = address;
167 sym->size = size;
168 sym->in_code = in_code;
169 sym->is_function = is_func;
170 if (compiland)
172 p = vector_add(&compiland->vchildren, &module->pool);
173 *p = &sym->symt;
176 return sym;
179 struct symt_data* symt_new_global_variable(struct module* module,
180 struct symt_compiland* compiland,
181 const char* name, unsigned is_static,
182 unsigned long addr, unsigned long size,
183 struct symt* type)
185 struct symt_data* sym;
186 struct symt** p;
187 DWORD64 tsz;
189 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
190 debugstr_w(module->module.ModuleName), name, addr, type);
191 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
193 sym->symt.tag = SymTagData;
194 sym->hash_elt.name = pool_strdup(&module->pool, name);
195 hash_table_add(&module->ht_symbols, &sym->hash_elt);
196 module->sortlist_valid = FALSE;
197 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
198 sym->container = compiland ? &compiland->symt : NULL;
199 sym->type = type;
200 sym->u.var.offset = addr;
201 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
203 if (tsz != size)
204 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
205 debugstr_w(module->module.ModuleName), name,
206 wine_dbgstr_longlong(tsz), size);
208 if (compiland)
210 p = vector_add(&compiland->vchildren, &module->pool);
211 *p = &sym->symt;
214 return sym;
217 struct symt_function* symt_new_function(struct module* module,
218 struct symt_compiland* compiland,
219 const char* name,
220 unsigned long addr, unsigned long size,
221 struct symt* sig_type)
223 struct symt_function* sym;
224 struct symt** p;
226 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
227 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
229 assert(!sig_type || sig_type->tag == SymTagFunctionType);
230 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
232 sym->symt.tag = SymTagFunction;
233 sym->hash_elt.name = pool_strdup(&module->pool, name);
234 hash_table_add(&module->ht_symbols, &sym->hash_elt);
235 module->sortlist_valid = FALSE;
236 sym->container = &compiland->symt;
237 sym->address = addr;
238 sym->type = sig_type;
239 sym->size = size;
240 vector_init(&sym->vlines, sizeof(struct line_info), 64);
241 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
242 if (compiland)
244 p = vector_add(&compiland->vchildren, &module->pool);
245 *p = &sym->symt;
248 return sym;
251 void symt_add_func_line(struct module* module, struct symt_function* func,
252 unsigned source_idx, int line_num, unsigned long offset)
254 struct line_info* dli;
255 BOOL last_matches = FALSE;
256 int i;
258 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
260 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
261 func, func->hash_elt.name, offset,
262 source_get(module, source_idx), line_num);
264 assert(func->symt.tag == SymTagFunction);
266 for (i=vector_length(&func->vlines)-1; i>=0; i--)
268 dli = vector_at(&func->vlines, i);
269 if (dli->is_source_file)
271 last_matches = (source_idx == dli->u.source_file);
272 break;
276 if (!last_matches)
278 /* we shouldn't have line changes on first line of function */
279 dli = vector_add(&func->vlines, &module->pool);
280 dli->is_source_file = 1;
281 dli->is_first = dli->is_last = 0;
282 dli->line_number = 0;
283 dli->u.source_file = source_idx;
285 dli = vector_add(&func->vlines, &module->pool);
286 dli->is_source_file = 0;
287 dli->is_first = dli->is_last = 0;
288 dli->line_number = line_num;
289 dli->u.pc_offset = func->address + offset;
292 /******************************************************************
293 * symt_add_func_local
295 * Adds a new local/parameter to a given function:
296 * In any cases, dt tells whether it's a local variable or a parameter
297 * If regno it's not 0:
298 * - then variable is stored in a register
299 * - otherwise, value is referenced by register + offset
300 * Otherwise, the variable is stored on the stack:
301 * - offset is then the offset from the frame register
303 struct symt_data* symt_add_func_local(struct module* module,
304 struct symt_function* func,
305 enum DataKind dt,
306 const struct location* loc,
307 struct symt_block* block,
308 struct symt* type, const char* name)
310 struct symt_data* locsym;
311 struct symt** p;
313 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
314 debugstr_w(module->module.ModuleName), func->hash_elt.name,
315 name, type);
317 assert(func);
318 assert(func->symt.tag == SymTagFunction);
319 assert(dt == DataIsParam || dt == DataIsLocal);
321 locsym = pool_alloc(&module->pool, sizeof(*locsym));
322 locsym->symt.tag = SymTagData;
323 locsym->hash_elt.name = pool_strdup(&module->pool, name);
324 locsym->hash_elt.next = NULL;
325 locsym->kind = dt;
326 locsym->container = &block->symt;
327 locsym->type = type;
328 locsym->u.var = *loc;
329 if (block)
330 p = vector_add(&block->vchildren, &module->pool);
331 else
332 p = vector_add(&func->vchildren, &module->pool);
333 *p = &locsym->symt;
334 return locsym;
338 struct symt_block* symt_open_func_block(struct module* module,
339 struct symt_function* func,
340 struct symt_block* parent_block,
341 unsigned pc, unsigned len)
343 struct symt_block* block;
344 struct symt** p;
346 assert(func);
347 assert(func->symt.tag == SymTagFunction);
349 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
350 block = pool_alloc(&module->pool, sizeof(*block));
351 block->symt.tag = SymTagBlock;
352 block->address = func->address + pc;
353 block->size = len;
354 block->container = parent_block ? &parent_block->symt : &func->symt;
355 vector_init(&block->vchildren, sizeof(struct symt*), 4);
356 if (parent_block)
357 p = vector_add(&parent_block->vchildren, &module->pool);
358 else
359 p = vector_add(&func->vchildren, &module->pool);
360 *p = &block->symt;
362 return block;
365 struct symt_block* symt_close_func_block(struct module* module,
366 struct symt_function* func,
367 struct symt_block* block, unsigned pc)
369 assert(func);
370 assert(func->symt.tag == SymTagFunction);
372 if (pc) block->size = func->address + pc - block->address;
373 return (block->container->tag == SymTagBlock) ?
374 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
377 struct symt_function_point* symt_add_function_point(struct module* module,
378 struct symt_function* func,
379 enum SymTagEnum point,
380 const struct location* loc,
381 const char* name)
383 struct symt_function_point* sym;
384 struct symt** p;
386 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
388 sym->symt.tag = point;
389 sym->parent = func;
390 sym->loc = *loc;
391 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
392 p = vector_add(&func->vchildren, &module->pool);
393 *p = &sym->symt;
395 return sym;
398 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
400 unsigned len;
401 struct line_info* dli;
403 assert(func);
404 /* We aren't adding any more locals or line numbers to this function.
405 * Free any spare memory that we might have allocated.
407 assert(func->symt.tag == SymTagFunction);
409 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
410 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
412 len = vector_length(&func->vlines);
413 if (len--)
415 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
416 dli = vector_at(&func->vlines, len); dli->is_last = 1;
418 return TRUE;
421 struct symt_thunk* symt_new_thunk(struct module* module,
422 struct symt_compiland* compiland,
423 const char* name, THUNK_ORDINAL ord,
424 unsigned long addr, unsigned long size)
426 struct symt_thunk* sym;
428 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
429 debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
431 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
433 sym->symt.tag = SymTagThunk;
434 sym->hash_elt.name = pool_strdup(&module->pool, name);
435 hash_table_add(&module->ht_symbols, &sym->hash_elt);
436 module->sortlist_valid = FALSE;
437 sym->container = &compiland->symt;
438 sym->address = addr;
439 sym->size = size;
440 sym->ordinal = ord;
441 if (compiland)
443 struct symt** p;
444 p = vector_add(&compiland->vchildren, &module->pool);
445 *p = &sym->symt;
448 return sym;
451 struct symt_data* symt_new_constant(struct module* module,
452 struct symt_compiland* compiland,
453 const char* name, struct symt* type,
454 const VARIANT* v)
456 struct symt_data* sym;
458 TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
459 debugstr_w(module->module.ModuleName), name);
461 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
463 sym->symt.tag = SymTagData;
464 sym->hash_elt.name = pool_strdup(&module->pool, name);
465 hash_table_add(&module->ht_symbols, &sym->hash_elt);
466 module->sortlist_valid = FALSE;
467 sym->kind = DataIsConstant;
468 sym->container = compiland ? &compiland->symt : NULL;
469 sym->type = type;
470 sym->u.value = *v;
471 if (compiland)
473 struct symt** p;
474 p = vector_add(&compiland->vchildren, &module->pool);
475 *p = &sym->symt;
478 return sym;
481 /* expect sym_info->MaxNameLen to be set before being called */
482 static void symt_fill_sym_info(const struct module_pair* pair,
483 const struct symt_function* func,
484 const struct symt* sym, SYMBOL_INFO* sym_info)
486 const char* name;
487 DWORD64 size;
489 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
490 sym_info->TypeIndex = 0;
491 sym_info->info = (DWORD)sym;
492 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
493 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
494 (!sym_info->TypeIndex ||
495 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
496 size = 0;
497 sym_info->Size = (DWORD)size;
498 sym_info->ModBase = pair->requested->module.BaseOfImage;
499 sym_info->Flags = 0;
500 sym_info->Value = 0;
502 switch (sym->tag)
504 case SymTagData:
506 const struct symt_data* data = (const struct symt_data*)sym;
507 switch (data->kind)
509 case DataIsParam:
510 sym_info->Flags |= SYMFLAG_PARAMETER;
511 /* fall through */
512 case DataIsLocal:
514 struct location loc = data->u.var;
516 if (loc.kind >= loc_user)
517 pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);
519 switch (loc.kind)
521 case loc_error:
522 /* for now we report error cases as a negative register number */
523 sym_info->Flags |= SYMFLAG_LOCAL;
524 /* fall through */
525 case loc_register:
526 sym_info->Flags |= SYMFLAG_REGISTER;
527 sym_info->Register = loc.reg;
528 sym_info->Address = 0;
529 break;
530 case loc_regrel:
531 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
532 /* FIXME: it's i386 dependent !!! */
533 sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
534 sym_info->Address = loc.offset;
535 break;
536 default:
537 FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
538 assert(0);
541 break;
542 case DataIsGlobal:
543 case DataIsFileStatic:
544 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
545 sym_info->Register = 0;
546 break;
547 case DataIsConstant:
548 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
549 switch (data->u.value.n1.n2.vt)
551 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
552 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
553 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
554 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
555 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
556 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
557 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
558 default:
559 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
560 sym_info->Value = 0;
561 break;
563 break;
564 default:
565 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
568 break;
569 case SymTagPublicSymbol:
570 sym_info->Flags |= SYMFLAG_EXPORT;
571 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
572 break;
573 case SymTagFunction:
574 sym_info->Flags |= SYMFLAG_FUNCTION;
575 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
576 break;
577 case SymTagThunk:
578 sym_info->Flags |= SYMFLAG_THUNK;
579 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
580 break;
581 default:
582 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
583 sym_info->Register = 0;
584 break;
586 sym_info->Scope = 0; /* FIXME */
587 sym_info->Tag = sym->tag;
588 name = symt_get_name(sym);
589 if (sym_info->MaxNameLen)
591 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
592 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
593 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
595 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
596 memcpy(sym_info->Name, name, sym_info->NameLen);
597 sym_info->Name[sym_info->NameLen] = '\0';
600 TRACE_(dbghelp_symt)("%p => %s %u %s\n",
601 sym, sym_info->Name, sym_info->Size,
602 wine_dbgstr_longlong(sym_info->Address));
605 struct sym_enum
607 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
608 PVOID user;
609 SYMBOL_INFO* sym_info;
610 DWORD index;
611 DWORD tag;
612 DWORD64 addr;
613 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
616 static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
617 const struct symt_function* func, const struct symt* sym)
619 symt_fill_sym_info(pair, func, sym, se->sym_info);
620 if (se->index && se->sym_info->info != se->index) return FALSE;
621 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
622 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
623 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
626 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
627 const struct sym_enum* se)
629 void* ptr;
630 struct symt_ht* sym = NULL;
631 struct hash_table_iter hti;
633 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
634 while ((ptr = hash_table_iter_up(&hti)))
636 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
637 if (sym->hash_elt.name &&
638 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
640 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
641 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
642 if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
645 return FALSE;
648 /***********************************************************************
649 * resort_symbols
651 * Rebuild sorted list of symbols for a module.
653 static BOOL resort_symbols(struct module* module)
655 void* ptr;
656 struct symt_ht* sym;
657 struct hash_table_iter hti;
658 ULONG64 addr;
660 if (!(module->module.NumSyms = module->ht_symbols.num_elts))
661 return FALSE;
663 if (module->addr_sorttab)
664 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
665 module->addr_sorttab,
666 module->module.NumSyms * sizeof(struct symt_ht*));
667 else
668 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
669 module->module.NumSyms * sizeof(struct symt_ht*));
670 if (!module->addr_sorttab) return FALSE;
672 module->num_sorttab = 0;
673 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
674 while ((ptr = hash_table_iter_up(&hti)))
676 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
677 assert(sym);
678 /* Don't store in sorttab symbol without address, they are of
679 * no use here (e.g. constant values)
680 * As the number of those symbols is very couple (a couple per module)
681 * we don't bother for the unused spots at the end of addr_sorttab
683 if (symt_get_info(&sym->symt, TI_GET_ADDRESS, &addr))
684 module->addr_sorttab[module->num_sorttab++] = sym;
686 qsort(module->addr_sorttab, module->num_sorttab, sizeof(struct symt_ht*), symt_cmp_addr);
687 return module->sortlist_valid = TRUE;
690 /* assume addr is in module */
691 struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
693 int mid, high, low;
694 ULONG64 ref_addr, ref_size;
696 if (!module->sortlist_valid || !module->addr_sorttab)
698 if (!resort_symbols(module)) return NULL;
702 * Binary search to find closest symbol.
704 low = 0;
705 high = module->num_sorttab;
707 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
708 if (addr < ref_addr) return NULL;
709 if (high)
711 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
712 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
713 ref_size = 0x1000; /* arbitrary value */
714 if (addr >= ref_addr + ref_size) return NULL;
717 while (high > low + 1)
719 mid = (high + low) / 2;
720 if (cmp_sorttab_addr(module, mid, addr) < 0)
721 low = mid;
722 else
723 high = mid;
725 if (low != high && high != module->num_sorttab &&
726 cmp_sorttab_addr(module, high, addr) <= 0)
727 low = high;
729 /* If found symbol is a public symbol, check if there are any other entries that
730 * might also have the same address, but would get better information
732 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
734 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
735 if (low > 0 &&
736 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
737 !cmp_sorttab_addr(module, low - 1, ref_addr))
738 low--;
739 else if (low < module->num_sorttab - 1 &&
740 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
741 !cmp_sorttab_addr(module, low + 1, ref_addr))
742 low++;
744 /* finally check that we fit into the found symbol */
745 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
746 if (addr < ref_addr) return NULL;
747 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
748 ref_size = 0x1000; /* arbitrary value */
749 if (addr >= ref_addr + ref_size) return NULL;
751 return module->addr_sorttab[low];
754 static BOOL symt_enum_locals_helper(struct module_pair* pair,
755 regex_t* preg, const struct sym_enum* se,
756 struct symt_function* func, const struct vector* v)
758 struct symt* lsym = NULL;
759 DWORD pc = pair->pcs->ctx_frame.InstructionOffset;
760 int i;
762 for (i=0; i<vector_length(v); i++)
764 lsym = *(struct symt**)vector_at(v, i);
765 switch (lsym->tag)
767 case SymTagBlock:
769 struct symt_block* block = (struct symt_block*)lsym;
770 if (pc < block->address || block->address + block->size <= pc)
771 continue;
772 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
773 return FALSE;
775 break;
776 case SymTagData:
777 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
779 if (send_symbol(se, pair, func, lsym)) return FALSE;
781 break;
782 case SymTagLabel:
783 case SymTagFuncDebugStart:
784 case SymTagFuncDebugEnd:
785 case SymTagCustom:
786 break;
787 default:
788 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
789 assert(0);
792 return TRUE;
795 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
796 const struct sym_enum* se)
798 struct module_pair pair;
799 struct symt_ht* sym;
800 DWORD pc = pcs->ctx_frame.InstructionOffset;
802 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
803 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
805 pair.pcs = pcs;
806 pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
807 if (!module_get_debug(&pair)) return FALSE;
808 if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
810 if (sym->symt.tag == SymTagFunction)
812 BOOL ret;
813 regex_t preg;
815 compile_regex(mask ? mask : "*", -1, &preg,
816 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
817 ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
818 &((struct symt_function*)sym)->vchildren);
819 regfree(&preg);
820 return ret;
823 return send_symbol(se, &pair, NULL, &sym->symt);
826 /******************************************************************
827 * copy_symbolW
829 * Helper for transforming an ANSI symbol info into an UNICODE one.
830 * Assume that MaxNameLen is the same for both version (A & W).
832 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
834 siw->SizeOfStruct = si->SizeOfStruct;
835 siw->TypeIndex = si->TypeIndex;
836 siw->Reserved[0] = si->Reserved[0];
837 siw->Reserved[1] = si->Reserved[1];
838 siw->Index = si->info; /* FIXME: see dbghelp.h */
839 siw->Size = si->Size;
840 siw->ModBase = si->ModBase;
841 siw->Flags = si->Flags;
842 siw->Value = si->Value;
843 siw->Address = si->Address;
844 siw->Register = si->Register;
845 siw->Scope = si->Scope;
846 siw->Tag = si->Tag;
847 siw->NameLen = si->NameLen;
848 siw->MaxNameLen = si->MaxNameLen;
849 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
852 /******************************************************************
853 * sym_enum
855 * Core routine for most of the enumeration of symbols
857 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
858 const struct sym_enum* se)
860 struct module_pair pair;
861 const char* bang;
862 regex_t mod_regex, sym_regex;
864 pair.pcs = process_find_by_handle(hProcess);
865 if (BaseOfDll == 0)
867 /* do local variables ? */
868 if (!Mask || !(bang = strchr(Mask, '!')))
869 return symt_enum_locals(pair.pcs, Mask, se);
871 if (bang == Mask) return FALSE;
873 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
874 compile_regex(bang + 1, -1, &sym_regex,
875 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
877 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
879 if (pair.requested->type == DMT_PE && module_get_debug(&pair))
881 if (regexec(&mod_regex, pair.requested->module_name, 0, NULL, 0) == 0 &&
882 symt_enum_module(&pair, &sym_regex, se))
883 break;
886 /* not found in PE modules, retry on the ELF ones
888 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
890 for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
892 if (pair.requested->type == DMT_ELF &&
893 !module_get_containee(pair.pcs, pair.requested) &&
894 module_get_debug(&pair))
896 if (regexec(&mod_regex, pair.requested->module_name, 0, NULL, 0) == 0 &&
897 symt_enum_module(&pair, &sym_regex, se))
898 break;
902 regfree(&mod_regex);
903 regfree(&sym_regex);
904 return TRUE;
906 pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
907 if (!module_get_debug(&pair))
908 return FALSE;
910 /* we always ignore module name from Mask when BaseOfDll is defined */
911 if (Mask && (bang = strchr(Mask, '!')))
913 if (bang == Mask) return FALSE;
914 Mask = bang + 1;
917 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
918 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
919 symt_enum_module(&pair, &sym_regex, se);
920 regfree(&sym_regex);
922 return TRUE;
925 /******************************************************************
926 * SymEnumSymbols (DBGHELP.@)
928 * cases BaseOfDll = 0
929 * !foo fails always (despite what MSDN states)
930 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
931 * no ! in Mask, lookup in local Context
932 * cases BaseOfDll != 0
933 * !foo fails always (despite what MSDN states)
934 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
936 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
937 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
938 PVOID UserContext)
940 struct sym_enum se;
942 TRACE("(%p %s %s %p %p)\n",
943 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
944 EnumSymbolsCallback, UserContext);
946 se.cb = EnumSymbolsCallback;
947 se.user = UserContext;
948 se.index = 0;
949 se.tag = 0;
950 se.addr = 0;
951 se.sym_info = (PSYMBOL_INFO)se.buffer;
953 return sym_enum(hProcess, BaseOfDll, Mask, &se);
956 struct sym_enumW
958 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
959 void* ctx;
960 PSYMBOL_INFOW sym_info;
961 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
965 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
967 struct sym_enumW* sew = ctx;
969 copy_symbolW(sew->sym_info, si);
971 return (sew->cb)(sew->sym_info, size, sew->ctx);
974 /******************************************************************
975 * SymEnumSymbolsW (DBGHELP.@)
978 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
979 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
980 PVOID UserContext)
982 struct sym_enumW sew;
983 BOOL ret = FALSE;
984 char* maskA = NULL;
986 sew.ctx = UserContext;
987 sew.cb = EnumSymbolsCallback;
988 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
990 if (Mask)
992 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
993 maskA = HeapAlloc(GetProcessHeap(), 0, len);
994 if (!maskA) return FALSE;
995 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
997 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
998 HeapFree(GetProcessHeap(), 0, maskA);
1000 return ret;
1003 struct sym_enumerate
1005 void* ctx;
1006 PSYM_ENUMSYMBOLS_CALLBACK cb;
1009 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1011 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
1012 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1015 /***********************************************************************
1016 * SymEnumerateSymbols (DBGHELP.@)
1018 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1019 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
1020 PVOID UserContext)
1022 struct sym_enumerate se;
1024 se.ctx = UserContext;
1025 se.cb = EnumSymbolsCallback;
1027 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1030 /******************************************************************
1031 * SymFromAddr (DBGHELP.@)
1034 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
1035 DWORD64* Displacement, PSYMBOL_INFO Symbol)
1037 struct module_pair pair;
1038 struct symt_ht* sym;
1040 pair.pcs = process_find_by_handle(hProcess);
1041 if (!pair.pcs) return FALSE;
1042 pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1043 if (!module_get_debug(&pair)) return FALSE;
1044 if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1046 symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1047 *Displacement = Address - Symbol->Address;
1048 return TRUE;
1051 /******************************************************************
1052 * SymFromAddrW (DBGHELP.@)
1055 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1056 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1058 PSYMBOL_INFO si;
1059 unsigned len;
1060 BOOL ret;
1062 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1063 si = HeapAlloc(GetProcessHeap(), 0, len);
1064 if (!si) return FALSE;
1066 si->SizeOfStruct = sizeof(*si);
1067 si->MaxNameLen = Symbol->MaxNameLen;
1068 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1070 copy_symbolW(Symbol, si);
1072 HeapFree(GetProcessHeap(), 0, si);
1073 return ret;
1076 /******************************************************************
1077 * SymGetSymFromAddr (DBGHELP.@)
1080 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1081 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1083 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1084 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1085 size_t len;
1086 DWORD64 Displacement64;
1088 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1089 si->SizeOfStruct = sizeof(*si);
1090 si->MaxNameLen = MAX_SYM_NAME;
1091 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1092 return FALSE;
1094 if (Displacement)
1095 *Displacement = Displacement64;
1096 Symbol->Address = si->Address;
1097 Symbol->Size = si->Size;
1098 Symbol->Flags = si->Flags;
1099 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1100 lstrcpynA(Symbol->Name, si->Name, len);
1101 return TRUE;
1104 /******************************************************************
1105 * SymGetSymFromAddr64 (DBGHELP.@)
1108 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1109 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1111 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1112 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1113 size_t len;
1114 DWORD64 Displacement64;
1116 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1117 si->SizeOfStruct = sizeof(*si);
1118 si->MaxNameLen = MAX_SYM_NAME;
1119 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1120 return FALSE;
1122 if (Displacement)
1123 *Displacement = Displacement64;
1124 Symbol->Address = si->Address;
1125 Symbol->Size = si->Size;
1126 Symbol->Flags = si->Flags;
1127 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1128 lstrcpynA(Symbol->Name, si->Name, len);
1129 return TRUE;
1132 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1133 SYMBOL_INFO* symbol)
1135 struct hash_table_iter hti;
1136 void* ptr;
1137 struct symt_ht* sym = NULL;
1138 struct module_pair pair;
1140 pair.pcs = pcs;
1141 if (!(pair.requested = module)) return FALSE;
1142 if (!module_get_debug(&pair)) return FALSE;
1144 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1145 while ((ptr = hash_table_iter_up(&hti)))
1147 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1149 if (!strcmp(sym->hash_elt.name, name))
1151 symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1152 return TRUE;
1155 return FALSE;
1158 /******************************************************************
1159 * SymFromName (DBGHELP.@)
1162 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1164 struct process* pcs = process_find_by_handle(hProcess);
1165 struct module* module;
1166 const char* name;
1168 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1169 if (!pcs) return FALSE;
1170 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1171 name = strchr(Name, '!');
1172 if (name)
1174 char tmp[128];
1175 assert(name - Name < sizeof(tmp));
1176 memcpy(tmp, Name, name - Name);
1177 tmp[name - Name] = '\0';
1178 module = module_find_by_nameA(pcs, tmp);
1179 return find_name(pcs, module, name + 1, Symbol);
1181 for (module = pcs->lmodules; module; module = module->next)
1183 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1184 return TRUE;
1186 /* not found in PE modules, retry on the ELF ones
1188 if (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES)
1190 for (module = pcs->lmodules; module; module = module->next)
1192 if (module->type == DMT_ELF && !module_get_containee(pcs, module) &&
1193 find_name(pcs, module, Name, Symbol))
1194 return TRUE;
1197 return FALSE;
1200 /***********************************************************************
1201 * SymGetSymFromName (DBGHELP.@)
1203 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1205 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1206 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1207 size_t len;
1209 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1210 si->SizeOfStruct = sizeof(*si);
1211 si->MaxNameLen = MAX_SYM_NAME;
1212 if (!SymFromName(hProcess, Name, si)) return FALSE;
1214 Symbol->Address = si->Address;
1215 Symbol->Size = si->Size;
1216 Symbol->Flags = si->Flags;
1217 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1218 lstrcpynA(Symbol->Name, si->Name, len);
1219 return TRUE;
1222 /******************************************************************
1223 * sym_fill_func_line_info
1225 * fills information about a file
1227 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1228 DWORD addr, IMAGEHLP_LINE* line)
1230 struct line_info* dli = NULL;
1231 BOOL found = FALSE;
1232 int i;
1234 assert(func->symt.tag == SymTagFunction);
1236 for (i=vector_length(&func->vlines)-1; i>=0; i--)
1238 dli = vector_at(&func->vlines, i);
1239 if (!dli->is_source_file)
1241 if (found || dli->u.pc_offset > addr) continue;
1242 line->LineNumber = dli->line_number;
1243 line->Address = dli->u.pc_offset;
1244 line->Key = dli;
1245 found = TRUE;
1246 continue;
1248 if (found)
1250 line->FileName = (char*)source_get(module, dli->u.source_file);
1251 return TRUE;
1254 return FALSE;
1257 /***********************************************************************
1258 * SymGetSymNext (DBGHELP.@)
1260 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1262 /* algo:
1263 * get module from Symbol.Address
1264 * get index in module.addr_sorttab of Symbol.Address
1265 * increment index
1266 * if out of module bounds, move to next module in process address space
1268 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1269 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1270 return FALSE;
1273 /***********************************************************************
1274 * SymGetSymPrev (DBGHELP.@)
1277 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1279 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1280 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1281 return FALSE;
1284 /******************************************************************
1285 * SymGetLineFromAddr (DBGHELP.@)
1288 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1289 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1291 struct module_pair pair;
1292 struct symt_ht* symt;
1294 TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1296 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1298 pair.pcs = process_find_by_handle(hProcess);
1299 if (!pair.pcs) return FALSE;
1300 pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1301 if (!module_get_debug(&pair)) return FALSE;
1302 if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1304 if (symt->symt.tag != SymTagFunction) return FALSE;
1305 if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1306 dwAddr, Line)) return FALSE;
1307 *pdwDisplacement = dwAddr - Line->Address;
1308 return TRUE;
1311 /******************************************************************
1312 * copy_line_64_from_32 (internal)
1315 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1318 l64->Key = l32->Key;
1319 l64->LineNumber = l32->LineNumber;
1320 l64->FileName = l32->FileName;
1321 l64->Address = l32->Address;
1324 /******************************************************************
1325 * copy_line_W64_from_32 (internal)
1328 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1330 unsigned len;
1332 l64->Key = l32->Key;
1333 l64->LineNumber = l32->LineNumber;
1334 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1335 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1336 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1337 l64->Address = l32->Address;
1340 /******************************************************************
1341 * copy_line_32_from_64 (internal)
1344 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1347 l32->Key = l64->Key;
1348 l32->LineNumber = l64->LineNumber;
1349 l32->FileName = l64->FileName;
1350 l32->Address = l64->Address;
1353 /******************************************************************
1354 * SymGetLineFromAddr64 (DBGHELP.@)
1357 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1358 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1360 IMAGEHLP_LINE line32;
1362 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1363 if (!validate_addr64(dwAddr)) return FALSE;
1364 line32.SizeOfStruct = sizeof(line32);
1365 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1366 return FALSE;
1367 copy_line_64_from_32(Line, &line32);
1368 return TRUE;
1371 /******************************************************************
1372 * SymGetLineFromAddrW64 (DBGHELP.@)
1375 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1376 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1378 struct process* pcs = process_find_by_handle(hProcess);
1379 IMAGEHLP_LINE line32;
1381 if (!pcs) return FALSE;
1382 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1383 if (!validate_addr64(dwAddr)) return FALSE;
1384 line32.SizeOfStruct = sizeof(line32);
1385 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1386 return FALSE;
1387 copy_line_W64_from_32(pcs, Line, &line32);
1388 return TRUE;
1391 /******************************************************************
1392 * SymGetLinePrev (DBGHELP.@)
1395 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1397 struct module_pair pair;
1398 struct line_info* li;
1399 BOOL in_search = FALSE;
1401 TRACE("(%p %p)\n", hProcess, Line);
1403 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1405 pair.pcs = process_find_by_handle(hProcess);
1406 if (!pair.pcs) return FALSE;
1407 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1408 if (!module_get_debug(&pair)) return FALSE;
1410 if (Line->Key == 0) return FALSE;
1411 li = (struct line_info*)Line->Key;
1412 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1413 * element we have to go back until we find the prev one to get the real
1414 * source file name for the DLIT_OFFSET element just before
1415 * the first DLIT_SOURCEFILE
1417 while (!li->is_first)
1419 li--;
1420 if (!li->is_source_file)
1422 Line->LineNumber = li->line_number;
1423 Line->Address = li->u.pc_offset;
1424 Line->Key = li;
1425 if (!in_search) return TRUE;
1427 else
1429 if (in_search)
1431 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1432 return TRUE;
1434 in_search = TRUE;
1437 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1438 return FALSE;
1441 /******************************************************************
1442 * SymGetLinePrev64 (DBGHELP.@)
1445 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1447 IMAGEHLP_LINE line32;
1449 line32.SizeOfStruct = sizeof(line32);
1450 copy_line_32_from_64(&line32, Line);
1451 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1452 copy_line_64_from_32(Line, &line32);
1453 return TRUE;
1456 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1458 struct line_info* li;
1460 if (line->Key == 0) return FALSE;
1461 li = (struct line_info*)line->Key;
1462 while (!li->is_last)
1464 li++;
1465 if (!li->is_source_file)
1467 line->LineNumber = li->line_number;
1468 line->Address = li->u.pc_offset;
1469 line->Key = li;
1470 return TRUE;
1472 line->FileName = (char*)source_get(module, li->u.source_file);
1474 return FALSE;
1477 /******************************************************************
1478 * SymGetLineNext (DBGHELP.@)
1481 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1483 struct module_pair pair;
1485 TRACE("(%p %p)\n", hProcess, Line);
1487 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1488 pair.pcs = process_find_by_handle(hProcess);
1489 if (!pair.pcs) return FALSE;
1490 pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1491 if (!module_get_debug(&pair)) return FALSE;
1493 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1494 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1495 return FALSE;
1498 /******************************************************************
1499 * SymGetLineNext64 (DBGHELP.@)
1502 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1504 IMAGEHLP_LINE line32;
1506 line32.SizeOfStruct = sizeof(line32);
1507 copy_line_32_from_64(&line32, Line);
1508 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1509 copy_line_64_from_32(Line, &line32);
1510 return TRUE;
1513 /***********************************************************************
1514 * SymFunctionTableAccess (DBGHELP.@)
1516 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1518 WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1519 return NULL;
1522 /***********************************************************************
1523 * SymFunctionTableAccess64 (DBGHELP.@)
1525 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1527 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1528 return NULL;
1531 /***********************************************************************
1532 * SymUnDName (DBGHELP.@)
1534 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1536 TRACE("(%p %s %u)\n", sym, UnDecName, UnDecNameLength);
1537 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1538 UNDNAME_COMPLETE) != 0;
1541 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1542 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1544 /***********************************************************************
1545 * UnDecorateSymbolName (DBGHELP.@)
1547 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1548 DWORD UndecoratedLength, DWORD Flags)
1550 /* undocumented from msvcrt */
1551 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1552 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1554 TRACE("(%s, %p, %d, 0x%08x)\n",
1555 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1557 if (!p_undname)
1559 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1560 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1561 if (!p_undname) return 0;
1564 if (!UnDecoratedName) return 0;
1565 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1566 und_alloc, und_free, Flags))
1567 return 0;
1568 return strlen(UnDecoratedName);
1571 /******************************************************************
1572 * SymMatchString (DBGHELP.@)
1575 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1577 regex_t preg;
1578 BOOL ret;
1580 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1582 compile_regex(re, -1, &preg, _case);
1583 ret = regexec(&preg, string, 0, NULL, 0) == 0;
1584 regfree(&preg);
1585 return ret;
1588 /******************************************************************
1589 * SymSearch (DBGHELP.@)
1591 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1592 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1593 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1594 PVOID UserContext, DWORD Options)
1596 struct sym_enum se;
1598 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1599 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1600 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1601 UserContext, Options);
1603 if (Options != SYMSEARCH_GLOBALSONLY)
1605 FIXME("Unsupported searching with options (%x)\n", Options);
1606 SetLastError(ERROR_INVALID_PARAMETER);
1607 return FALSE;
1610 se.cb = EnumSymbolsCallback;
1611 se.user = UserContext;
1612 se.index = Index;
1613 se.tag = SymTag;
1614 se.addr = Address;
1615 se.sym_info = (PSYMBOL_INFO)se.buffer;
1617 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1620 /******************************************************************
1621 * SymSearchW (DBGHELP.@)
1623 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1624 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1625 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1626 PVOID UserContext, DWORD Options)
1628 struct sym_enumW sew;
1629 BOOL ret = FALSE;
1630 char* maskA = NULL;
1632 TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1633 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1634 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1635 UserContext, Options);
1637 sew.ctx = UserContext;
1638 sew.cb = EnumSymbolsCallback;
1639 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1641 if (Mask)
1643 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1644 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1645 if (!maskA) return FALSE;
1646 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1648 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1649 sym_enumW, &sew, Options);
1650 HeapFree(GetProcessHeap(), 0, maskA);
1652 return ret;