oleaut32: Include minimal ungif source to remove dependency on libungif.
[wine/testsucceed.git] / dlls / dbghelp / symbol.c
blob2354c1af9a6d8200a0a1bbeac996391488f26945
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 inline static int cmp_addr(ULONG64 a1, ULONG64 a2)
46 if (a1 > a2) return 1;
47 if (a1 < a2) return -1;
48 return 0;
51 inline static 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, unsigned src_idx)
130 struct symt_compiland* sym;
132 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
133 module->module.ModuleName, source_get(module, src_idx));
134 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
136 sym->symt.tag = SymTagCompiland;
137 sym->source = src_idx;
138 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
140 return sym;
143 struct symt_public* symt_new_public(struct module* module,
144 struct symt_compiland* compiland,
145 const char* name,
146 unsigned long address, unsigned size,
147 BOOL in_code, BOOL is_func)
149 struct symt_public* sym;
150 struct symt** p;
152 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
153 module->module.ModuleName, name, address);
154 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
155 symt_find_nearest(module, address) != -1)
156 return NULL;
157 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
159 sym->symt.tag = SymTagPublicSymbol;
160 sym->hash_elt.name = pool_strdup(&module->pool, name);
161 hash_table_add(&module->ht_symbols, &sym->hash_elt);
162 module->sortlist_valid = FALSE;
163 sym->container = compiland ? &compiland->symt : NULL;
164 sym->address = address;
165 sym->size = size;
166 sym->in_code = in_code;
167 sym->is_function = is_func;
168 if (compiland)
170 p = vector_add(&compiland->vchildren, &module->pool);
171 *p = &sym->symt;
174 return sym;
177 struct symt_data* symt_new_global_variable(struct module* module,
178 struct symt_compiland* compiland,
179 const char* name, unsigned is_static,
180 unsigned long addr, unsigned long size,
181 struct symt* type)
183 struct symt_data* sym;
184 struct symt** p;
185 DWORD64 tsz;
187 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
188 module->module.ModuleName, name, addr, type);
189 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
191 sym->symt.tag = SymTagData;
192 sym->hash_elt.name = pool_strdup(&module->pool, name);
193 hash_table_add(&module->ht_symbols, &sym->hash_elt);
194 module->sortlist_valid = FALSE;
195 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
196 sym->container = compiland ? &compiland->symt : NULL;
197 sym->type = type;
198 sym->u.address = addr;
199 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
201 if (tsz != size)
202 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
203 module->module.ModuleName, name,
204 wine_dbgstr_longlong(tsz), size);
206 if (compiland)
208 p = vector_add(&compiland->vchildren, &module->pool);
209 *p = &sym->symt;
212 return sym;
215 struct symt_function* symt_new_function(struct module* module,
216 struct symt_compiland* compiland,
217 const char* name,
218 unsigned long addr, unsigned long size,
219 struct symt* sig_type)
221 struct symt_function* sym;
222 struct symt** p;
224 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
225 module->module.ModuleName, name, addr, addr + size - 1);
227 assert(!sig_type || sig_type->tag == SymTagFunctionType);
228 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
230 sym->symt.tag = SymTagFunction;
231 sym->hash_elt.name = pool_strdup(&module->pool, name);
232 hash_table_add(&module->ht_symbols, &sym->hash_elt);
233 module->sortlist_valid = FALSE;
234 sym->container = &compiland->symt;
235 sym->address = addr;
236 sym->type = sig_type;
237 sym->size = size;
238 vector_init(&sym->vlines, sizeof(struct line_info), 64);
239 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
240 if (compiland)
242 p = vector_add(&compiland->vchildren, &module->pool);
243 *p = &sym->symt;
246 return sym;
249 void symt_add_func_line(struct module* module, struct symt_function* func,
250 unsigned source_idx, int line_num, unsigned long offset)
252 struct line_info* dli;
253 BOOL last_matches = FALSE;
255 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
257 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
258 func, func->hash_elt.name, offset,
259 source_get(module, source_idx), line_num);
261 assert(func->symt.tag == SymTagFunction);
263 dli = NULL;
264 while ((dli = vector_iter_down(&func->vlines, dli)))
266 if (dli->is_source_file)
268 last_matches = (source_idx == dli->u.source_file);
269 break;
273 if (!last_matches)
275 /* we shouldn't have line changes on first line of function */
276 dli = vector_add(&func->vlines, &module->pool);
277 dli->is_source_file = 1;
278 dli->is_first = dli->is_last = 0;
279 dli->line_number = 0;
280 dli->u.source_file = source_idx;
282 dli = vector_add(&func->vlines, &module->pool);
283 dli->is_source_file = 0;
284 dli->is_first = dli->is_last = 0;
285 dli->line_number = line_num;
286 dli->u.pc_offset = func->address + offset;
289 /******************************************************************
290 * symt_add_func_local
292 * Adds a new local/parameter to a given function:
293 * In any cases, dt tells whether it's a local variable or a parameter
294 * If regno it's not 0:
295 * - then variable is stored in a register
296 * - otherwise, value is referenced by register + offset
297 * Otherwise, the variable is stored on the stack:
298 * - offset is then the offset from the frame register
300 struct symt_data* symt_add_func_local(struct module* module,
301 struct symt_function* func,
302 enum DataKind dt,
303 int regno, long offset,
304 struct symt_block* block,
305 struct symt* type, const char* name)
307 struct symt_data* locsym;
308 struct symt** p;
310 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
311 module->module.ModuleName, func->hash_elt.name,
312 name, type);
314 assert(func);
315 assert(func->symt.tag == SymTagFunction);
316 assert(dt == DataIsParam || dt == DataIsLocal);
318 locsym = pool_alloc(&module->pool, sizeof(*locsym));
319 locsym->symt.tag = SymTagData;
320 locsym->hash_elt.name = pool_strdup(&module->pool, name);
321 locsym->hash_elt.next = NULL;
322 locsym->kind = dt;
323 locsym->container = &block->symt;
324 locsym->type = type;
325 locsym->u.s.reg_id = regno;
326 locsym->u.s.offset = offset * 8;
327 locsym->u.s.length = 0;
328 if (block)
329 p = vector_add(&block->vchildren, &module->pool);
330 else
331 p = vector_add(&func->vchildren, &module->pool);
332 *p = &locsym->symt;
333 return locsym;
337 struct symt_block* symt_open_func_block(struct module* module,
338 struct symt_function* func,
339 struct symt_block* parent_block,
340 unsigned pc, unsigned len)
342 struct symt_block* block;
343 struct symt** p;
345 assert(func);
346 assert(func->symt.tag == SymTagFunction);
348 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
349 block = pool_alloc(&module->pool, sizeof(*block));
350 block->symt.tag = SymTagBlock;
351 block->address = func->address + pc;
352 block->size = len;
353 block->container = parent_block ? &parent_block->symt : &func->symt;
354 vector_init(&block->vchildren, sizeof(struct symt*), 4);
355 if (parent_block)
356 p = vector_add(&parent_block->vchildren, &module->pool);
357 else
358 p = vector_add(&func->vchildren, &module->pool);
359 *p = &block->symt;
361 return block;
364 struct symt_block* symt_close_func_block(struct module* module,
365 struct symt_function* func,
366 struct symt_block* block, unsigned pc)
368 assert(func);
369 assert(func->symt.tag == SymTagFunction);
371 if (pc) block->size = func->address + pc - block->address;
372 return (block->container->tag == SymTagBlock) ?
373 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
376 struct symt_function_point* symt_add_function_point(struct module* module,
377 struct symt_function* func,
378 enum SymTagEnum point,
379 unsigned offset, const char* name)
381 struct symt_function_point* sym;
382 struct symt** p;
384 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
386 sym->symt.tag = point;
387 sym->parent = func;
388 sym->offset = offset;
389 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
390 p = vector_add(&func->vchildren, &module->pool);
391 *p = &sym->symt;
393 return sym;
396 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
398 unsigned len;
399 struct line_info* dli;
401 assert(func);
402 /* We aren't adding any more locals or line numbers to this function.
403 * Free any spare memory that we might have allocated.
405 assert(func->symt.tag == SymTagFunction);
407 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
408 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
410 len = vector_length(&func->vlines);
411 if (len--)
413 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
414 dli = vector_at(&func->vlines, len); dli->is_last = 1;
416 return TRUE;
419 struct symt_thunk* symt_new_thunk(struct module* module,
420 struct symt_compiland* compiland,
421 const char* name, THUNK_ORDINAL ord,
422 unsigned long addr, unsigned long size)
424 struct symt_thunk* sym;
426 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
427 module->module.ModuleName, name, addr, addr + size - 1);
429 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
431 sym->symt.tag = SymTagThunk;
432 sym->hash_elt.name = pool_strdup(&module->pool, name);
433 hash_table_add(&module->ht_symbols, &sym->hash_elt);
434 module->sortlist_valid = FALSE;
435 sym->container = &compiland->symt;
436 sym->address = addr;
437 sym->size = size;
438 sym->ordinal = ord;
439 if (compiland)
441 struct symt** p;
442 p = vector_add(&compiland->vchildren, &module->pool);
443 *p = &sym->symt;
446 return sym;
449 /* expect sym_info->MaxNameLen to be set before being called */
450 static void symt_fill_sym_info(const struct module_pair* pair,
451 const struct symt* sym, SYMBOL_INFO* sym_info)
453 const char* name;
454 DWORD64 size;
456 if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
457 sym_info->TypeIndex = 0;
458 sym_info->info = (DWORD)sym;
459 sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
460 if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
461 (!sym_info->TypeIndex ||
462 !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
463 size = 0;
464 sym_info->Size = (DWORD)size;
465 sym_info->ModBase = pair->requested->module.BaseOfImage;
466 sym_info->Flags = 0;
467 sym_info->Value = 0;
469 switch (sym->tag)
471 case SymTagData:
473 const struct symt_data* data = (const struct symt_data*)sym;
474 switch (data->kind)
476 case DataIsParam:
477 sym_info->Flags |= SYMFLAG_PARAMETER;
478 /* fall through */
479 case DataIsLocal:
480 if (data->u.s.reg_id)
482 sym_info->Flags |= SYMFLAG_REGISTER;
483 sym_info->Register = data->u.s.reg_id;
484 sym_info->Address = 0;
486 else
488 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
489 /* FIXME: needed ? moreover, it's i386 dependent !!! */
490 sym_info->Register = CV_REG_EBP;
491 sym_info->Address = data->u.s.offset / 8;
493 break;
494 case DataIsGlobal:
495 case DataIsFileStatic:
496 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
497 sym_info->Register = 0;
498 break;
499 case DataIsConstant:
500 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
501 switch (data->u.value.n1.n2.vt)
503 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
504 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
505 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
506 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
507 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
508 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
509 default:
510 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
512 break;
513 default:
514 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
517 break;
518 case SymTagPublicSymbol:
519 sym_info->Flags |= SYMFLAG_EXPORT;
520 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
521 break;
522 case SymTagFunction:
523 sym_info->Flags |= SYMFLAG_FUNCTION;
524 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
525 break;
526 case SymTagThunk:
527 sym_info->Flags |= SYMFLAG_THUNK;
528 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
529 break;
530 default:
531 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
532 sym_info->Register = 0;
533 break;
535 sym_info->Scope = 0; /* FIXME */
536 sym_info->Tag = sym->tag;
537 name = symt_get_name(sym);
538 if (sym_info->MaxNameLen)
540 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
541 (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
542 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
544 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
545 memcpy(sym_info->Name, name, sym_info->NameLen);
546 sym_info->Name[sym_info->NameLen] = '\0';
549 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
550 sym, sym_info->Name, sym_info->Size,
551 wine_dbgstr_longlong(sym_info->Address));
554 struct sym_enum
556 PSYM_ENUMERATESYMBOLS_CALLBACK cb;
557 PVOID user;
558 SYMBOL_INFO* sym_info;
559 DWORD index;
560 DWORD tag;
561 DWORD64 addr;
562 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
565 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
566 const struct symt* sym)
568 symt_fill_sym_info(pair, sym, se->sym_info);
569 if (se->index && se->sym_info->info != se->index) return FALSE;
570 if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
571 if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
572 return !se->cb(se->sym_info, se->sym_info->Size, se->user);
575 static BOOL symt_enum_module(struct module_pair* pair, regex_t* regex,
576 const struct sym_enum* se)
578 void* ptr;
579 struct symt_ht* sym = NULL;
580 struct hash_table_iter hti;
582 hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
583 while ((ptr = hash_table_iter_up(&hti)))
585 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
586 if (sym->hash_elt.name &&
587 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
589 se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
590 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
591 if (send_symbol(se, pair, &sym->symt)) return TRUE;
594 return FALSE;
597 /***********************************************************************
598 * resort_symbols
600 * Rebuild sorted list of symbols for a module.
602 static BOOL resort_symbols(struct module* module)
604 int nsym;
605 void* ptr;
606 struct symt_ht* sym;
607 struct hash_table_iter hti;
609 if (!module_compute_num_syms(module)) return FALSE;
611 if (module->addr_sorttab)
612 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
613 module->addr_sorttab,
614 module->module.NumSyms * sizeof(struct symt_ht*));
615 else
616 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
617 module->module.NumSyms * sizeof(struct symt_ht*));
618 if (!module->addr_sorttab) return FALSE;
620 nsym = 0;
621 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
622 while ((ptr = hash_table_iter_up(&hti)))
624 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
625 assert(sym);
626 module->addr_sorttab[nsym++] = sym;
629 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
630 return module->sortlist_valid = TRUE;
633 /* assume addr is in module */
634 int symt_find_nearest(struct module* module, DWORD addr)
636 int mid, high, low;
637 ULONG64 ref_addr, ref_size;
639 if (!module->sortlist_valid || !module->addr_sorttab)
641 if (!resort_symbols(module)) return -1;
645 * Binary search to find closest symbol.
647 low = 0;
648 high = module->module.NumSyms;
650 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
651 if (addr < ref_addr) return -1;
652 if (high)
654 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
655 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
656 ref_size = 0x1000; /* arbitrary value */
657 if (addr >= ref_addr + ref_size) return -1;
660 while (high > low + 1)
662 mid = (high + low) / 2;
663 if (cmp_sorttab_addr(module, mid, addr) < 0)
664 low = mid;
665 else
666 high = mid;
668 if (low != high && high != module->module.NumSyms &&
669 cmp_sorttab_addr(module, high, addr) <= 0)
670 low = high;
672 /* If found symbol is a public symbol, check if there are any other entries that
673 * might also have the same address, but would get better information
675 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
677 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
678 if (low > 0 &&
679 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
680 !cmp_sorttab_addr(module, low - 1, ref_addr))
681 low--;
682 else if (low < module->module.NumSyms - 1 &&
683 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
684 !cmp_sorttab_addr(module, low + 1, ref_addr))
685 low++;
687 /* finally check that we fit into the found symbol */
688 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
689 if (addr < ref_addr) return -1;
690 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
691 ref_size = 0x1000; /* arbitrary value */
692 if (addr >= ref_addr + ref_size) return -1;
694 return low;
697 static BOOL symt_enum_locals_helper(struct process* pcs, struct module_pair* pair,
698 regex_t* preg, const struct sym_enum* se,
699 struct vector* v)
701 struct symt** plsym = NULL;
702 struct symt* lsym = NULL;
703 DWORD pc = pcs->ctx_frame.InstructionOffset;
705 while ((plsym = vector_iter_up(v, plsym)))
707 lsym = *plsym;
708 switch (lsym->tag)
710 case SymTagBlock:
712 struct symt_block* block = (struct symt_block*)lsym;
713 if (pc < block->address || block->address + block->size <= pc)
714 continue;
715 if (!symt_enum_locals_helper(pcs, pair, preg, se, &block->vchildren))
716 return FALSE;
718 break;
719 case SymTagData:
720 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
722 if (send_symbol(se, pair, lsym)) return FALSE;
724 break;
725 case SymTagLabel:
726 case SymTagFuncDebugStart:
727 case SymTagFuncDebugEnd:
728 break;
729 default:
730 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
731 assert(0);
734 return TRUE;
737 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
738 const struct sym_enum* se)
740 struct module_pair pair;
741 struct symt_ht* sym;
742 DWORD pc = pcs->ctx_frame.InstructionOffset;
743 int idx;
745 se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
746 se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
748 pair.requested = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
749 if (!module_get_debug(pcs, &pair)) return FALSE;
750 if ((idx = symt_find_nearest(pair.effective, pc)) == -1) return FALSE;
752 sym = pair.effective->addr_sorttab[idx];
753 if (sym->symt.tag == SymTagFunction)
755 BOOL ret;
756 regex_t preg;
758 compile_regex(mask ? mask : "*", -1, &preg,
759 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
760 ret = symt_enum_locals_helper(pcs, &pair, &preg, se,
761 &((struct symt_function*)sym)->vchildren);
762 regfree(&preg);
763 return ret;
766 return send_symbol(se, &pair, &sym->symt);
769 /******************************************************************
770 * copy_symbolW
772 * Helper for transforming an ANSI symbol info into an UNICODE one.
773 * Assume that MaxNameLen is the same for both version (A & W).
775 static void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
777 siw->SizeOfStruct = si->SizeOfStruct;
778 siw->TypeIndex = si->TypeIndex;
779 siw->Reserved[0] = si->Reserved[0];
780 siw->Reserved[1] = si->Reserved[1];
781 siw->Index = si->info; /* FIXME: see dbghelp.h */
782 siw->Size = si->Size;
783 siw->ModBase = si->ModBase;
784 siw->Flags = si->Flags;
785 siw->Value = si->Value;
786 siw->Address = si->Address;
787 siw->Register = si->Register;
788 siw->Scope = si->Scope;
789 siw->Tag = si->Tag;
790 siw->NameLen = si->NameLen;
791 siw->MaxNameLen = si->MaxNameLen;
792 MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
795 /******************************************************************
796 * sym_enum
798 * Core routine for most of the enumeration of symbols
800 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
801 const struct sym_enum* se)
803 struct process* pcs = process_find_by_handle(hProcess);
804 struct module_pair pair;
805 const char* bang;
806 regex_t mod_regex, sym_regex;
808 if (BaseOfDll == 0)
810 /* do local variables ? */
811 if (!Mask || !(bang = strchr(Mask, '!')))
812 return symt_enum_locals(pcs, Mask, se);
814 if (bang == Mask) return FALSE;
816 compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
817 compile_regex(bang + 1, -1, &sym_regex,
818 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
820 for (pair.requested = pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
822 if (pair.requested->type == DMT_PE && module_get_debug(pcs, &pair))
824 if (regexec(&mod_regex, pair.requested->module.ModuleName, 0, NULL, 0) == 0 &&
825 symt_enum_module(&pair, &sym_regex, se))
826 break;
829 /* not found in PE modules, retry on the ELF ones
831 if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
833 for (pair.requested = pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
835 if (pair.requested->type == DMT_ELF &&
836 !module_get_containee(pcs, pair.requested) &&
837 module_get_debug(pcs, &pair))
839 if (regexec(&mod_regex, pair.requested->module.ModuleName, 0, NULL, 0) == 0 &&
840 symt_enum_module(&pair, &sym_regex, se))
841 break;
845 regfree(&mod_regex);
846 regfree(&sym_regex);
847 return TRUE;
849 pair.requested = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
850 if (!module_get_debug(pcs, &pair))
851 return FALSE;
853 /* we always ignore module name from Mask when BaseOfDll is defined */
854 if (Mask && (bang = strchr(Mask, '!')))
856 if (bang == Mask) return FALSE;
857 Mask = bang + 1;
860 compile_regex(Mask ? Mask : "*", -1, &sym_regex,
861 dbghelp_options & SYMOPT_CASE_INSENSITIVE);
862 symt_enum_module(&pair, &sym_regex, se);
863 regfree(&sym_regex);
865 return TRUE;
868 /******************************************************************
869 * SymEnumSymbols (DBGHELP.@)
871 * cases BaseOfDll = 0
872 * !foo fails always (despite what MSDN states)
873 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
874 * no ! in Mask, lookup in local Context
875 * cases BaseOfDll != 0
876 * !foo fails always (despite what MSDN states)
877 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
879 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
880 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
881 PVOID UserContext)
883 struct sym_enum se;
885 TRACE("(%p %s %s %p %p)\n",
886 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
887 EnumSymbolsCallback, UserContext);
889 se.cb = EnumSymbolsCallback;
890 se.user = UserContext;
891 se.index = 0;
892 se.tag = 0;
893 se.addr = 0;
894 se.sym_info = (PSYMBOL_INFO)se.buffer;
896 return sym_enum(hProcess, BaseOfDll, Mask, &se);
899 struct sym_enumW
901 PSYM_ENUMERATESYMBOLS_CALLBACKW cb;
902 void* ctx;
903 PSYMBOL_INFOW sym_info;
904 char buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
908 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
910 struct sym_enumW* sew = ctx;
912 copy_symbolW(sew->sym_info, si);
914 return (sew->cb)(sew->sym_info, size, sew->ctx);
917 /******************************************************************
918 * SymEnumSymbolsW (DBGHELP.@)
921 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
922 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
923 PVOID UserContext)
925 struct sym_enumW sew;
926 BOOL ret = FALSE;
927 char* maskA = NULL;
929 sew.ctx = UserContext;
930 sew.cb = EnumSymbolsCallback;
931 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
933 if (Mask)
935 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
936 maskA = HeapAlloc(GetProcessHeap(), 0, len);
937 if (!maskA) return FALSE;
938 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
940 ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
941 HeapFree(GetProcessHeap(), 0, maskA);
943 return ret;
946 struct sym_enumerate
948 void* ctx;
949 PSYM_ENUMSYMBOLS_CALLBACK cb;
952 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
954 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
955 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
958 /***********************************************************************
959 * SymEnumerateSymbols (DBGHELP.@)
961 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
962 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
963 PVOID UserContext)
965 struct sym_enumerate se;
967 se.ctx = UserContext;
968 se.cb = EnumSymbolsCallback;
970 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
973 /******************************************************************
974 * SymFromAddr (DBGHELP.@)
977 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
978 DWORD64* Displacement, PSYMBOL_INFO Symbol)
980 struct process* pcs = process_find_by_handle(hProcess);
981 struct module_pair pair;
982 struct symt_ht* sym;
983 int idx;
985 if (!pcs) return FALSE;
986 pair.requested = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
987 if (!module_get_debug(pcs, &pair)) return FALSE;
988 if ((idx = symt_find_nearest(pair.effective, Address)) == -1) return FALSE;
990 sym = pair.effective->addr_sorttab[idx];
992 symt_fill_sym_info(&pair, &sym->symt, Symbol);
993 *Displacement = Address - Symbol->Address;
994 return TRUE;
997 /******************************************************************
998 * SymFromAddrW (DBGHELP.@)
1001 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address,
1002 DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1004 PSYMBOL_INFO si;
1005 unsigned len;
1006 BOOL ret;
1008 len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1009 si = HeapAlloc(GetProcessHeap(), 0, len);
1010 if (!si) return FALSE;
1012 si->SizeOfStruct = sizeof(*si);
1013 si->MaxNameLen = Symbol->MaxNameLen;
1014 if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1016 copy_symbolW(Symbol, si);
1018 HeapFree(GetProcessHeap(), 0, si);
1019 return ret;
1022 /******************************************************************
1023 * SymGetSymFromAddr (DBGHELP.@)
1026 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1027 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1029 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1030 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1031 size_t len;
1032 DWORD64 Displacement64;
1034 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1035 si->SizeOfStruct = sizeof(*si);
1036 si->MaxNameLen = MAX_SYM_NAME;
1037 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1038 return FALSE;
1040 if (Displacement)
1041 *Displacement = Displacement64;
1042 Symbol->Address = si->Address;
1043 Symbol->Size = si->Size;
1044 Symbol->Flags = si->Flags;
1045 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1046 lstrcpynA(Symbol->Name, si->Name, len);
1047 return TRUE;
1050 /******************************************************************
1051 * SymGetSymFromAddr64 (DBGHELP.@)
1054 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1055 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1057 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1058 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1059 size_t len;
1060 DWORD64 Displacement64;
1062 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1063 si->SizeOfStruct = sizeof(*si);
1064 si->MaxNameLen = MAX_SYM_NAME;
1065 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1066 return FALSE;
1068 if (Displacement)
1069 *Displacement = Displacement64;
1070 Symbol->Address = si->Address;
1071 Symbol->Size = si->Size;
1072 Symbol->Flags = si->Flags;
1073 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1074 lstrcpynA(Symbol->Name, si->Name, len);
1075 return TRUE;
1078 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1079 SYMBOL_INFO* symbol)
1081 struct hash_table_iter hti;
1082 void* ptr;
1083 struct symt_ht* sym = NULL;
1084 struct module_pair pair;
1086 if (!(pair.requested = module)) return FALSE;
1087 if (!module_get_debug(pcs, &pair)) return FALSE;
1089 hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1090 while ((ptr = hash_table_iter_up(&hti)))
1092 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1094 if (!strcmp(sym->hash_elt.name, name))
1096 symt_fill_sym_info(&pair, &sym->symt, symbol);
1097 return TRUE;
1100 return FALSE;
1103 /******************************************************************
1104 * SymFromName (DBGHELP.@)
1107 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1109 struct process* pcs = process_find_by_handle(hProcess);
1110 struct module* module;
1111 const char* name;
1113 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1114 if (!pcs) return FALSE;
1115 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1116 name = strchr(Name, '!');
1117 if (name)
1119 char tmp[128];
1120 assert(name - Name < sizeof(tmp));
1121 memcpy(tmp, Name, name - Name);
1122 tmp[name - Name] = '\0';
1123 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
1124 return find_name(pcs, module, (char*)(name + 1), Symbol);
1126 for (module = pcs->lmodules; module; module = module->next)
1128 if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1129 return TRUE;
1131 /* not found in PE modules, retry on the ELF ones
1133 if (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES)
1135 for (module = pcs->lmodules; module; module = module->next)
1137 if (module->type == DMT_ELF && !module_get_containee(pcs, module) &&
1138 find_name(pcs, module, Name, Symbol))
1139 return TRUE;
1142 return FALSE;
1145 /***********************************************************************
1146 * SymGetSymFromName (DBGHELP.@)
1148 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1150 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1151 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1152 size_t len;
1154 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1155 si->SizeOfStruct = sizeof(*si);
1156 si->MaxNameLen = MAX_SYM_NAME;
1157 if (!SymFromName(hProcess, Name, si)) return FALSE;
1159 Symbol->Address = si->Address;
1160 Symbol->Size = si->Size;
1161 Symbol->Flags = si->Flags;
1162 len = min(Symbol->MaxNameLength, si->MaxNameLen);
1163 lstrcpynA(Symbol->Name, si->Name, len);
1164 return TRUE;
1167 /******************************************************************
1168 * sym_fill_func_line_info
1170 * fills information about a file
1172 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
1173 DWORD addr, IMAGEHLP_LINE* line)
1175 struct line_info* dli = NULL;
1176 BOOL found = FALSE;
1178 assert(func->symt.tag == SymTagFunction);
1180 while ((dli = vector_iter_down(&func->vlines, dli)))
1182 if (!dli->is_source_file)
1184 if (found || dli->u.pc_offset > addr) continue;
1185 line->LineNumber = dli->line_number;
1186 line->Address = dli->u.pc_offset;
1187 line->Key = dli;
1188 found = TRUE;
1189 continue;
1191 if (found)
1193 line->FileName = (char*)source_get(module, dli->u.source_file);
1194 return TRUE;
1197 return FALSE;
1200 /***********************************************************************
1201 * SymGetSymNext (DBGHELP.@)
1203 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1205 /* algo:
1206 * get module from Symbol.Address
1207 * get index in module.addr_sorttab of Symbol.Address
1208 * increment index
1209 * if out of module bounds, move to next module in process address space
1211 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1212 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1213 return FALSE;
1216 /***********************************************************************
1217 * SymGetSymPrev (DBGHELP.@)
1220 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1222 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1223 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1224 return FALSE;
1227 /******************************************************************
1228 * SymGetLineFromAddr (DBGHELP.@)
1231 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1232 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1234 struct process* pcs = process_find_by_handle(hProcess);
1235 struct module_pair pair;
1236 int idx;
1238 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1240 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1242 if (!pcs) return FALSE;
1243 pair.requested = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1244 if (!module_get_debug(pcs, &pair)) return FALSE;
1245 if ((idx = symt_find_nearest(pair.effective, dwAddr)) == -1) return FALSE;
1247 if (pair.effective->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1248 if (!symt_fill_func_line_info(pair.effective,
1249 (struct symt_function*)pair.effective->addr_sorttab[idx],
1250 dwAddr, Line)) return FALSE;
1251 *pdwDisplacement = dwAddr - Line->Address;
1252 return TRUE;
1255 /******************************************************************
1256 * copy_line_64_from_32 (internal)
1259 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1262 l64->Key = l32->Key;
1263 l64->LineNumber = l32->LineNumber;
1264 l64->FileName = l32->FileName;
1265 l64->Address = l32->Address;
1268 /******************************************************************
1269 * copy_line_W64_from_32 (internal)
1272 static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
1274 unsigned len;
1276 l64->Key = l32->Key;
1277 l64->LineNumber = l32->LineNumber;
1278 len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
1279 if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1280 MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
1281 l64->Address = l32->Address;
1284 /******************************************************************
1285 * copy_line_32_from_64 (internal)
1288 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1291 l32->Key = l64->Key;
1292 l32->LineNumber = l64->LineNumber;
1293 l32->FileName = l64->FileName;
1294 l32->Address = l64->Address;
1297 /******************************************************************
1298 * SymGetLineFromAddr64 (DBGHELP.@)
1301 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
1302 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1304 IMAGEHLP_LINE line32;
1306 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1307 if (!validate_addr64(dwAddr)) return FALSE;
1308 line32.SizeOfStruct = sizeof(line32);
1309 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1310 return FALSE;
1311 copy_line_64_from_32(Line, &line32);
1312 return TRUE;
1315 /******************************************************************
1316 * SymGetLineFromAddrW64 (DBGHELP.@)
1319 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr,
1320 PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1322 struct process* pcs = process_find_by_handle(hProcess);
1323 IMAGEHLP_LINE line32;
1325 if (!pcs) return FALSE;
1326 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1327 if (!validate_addr64(dwAddr)) return FALSE;
1328 line32.SizeOfStruct = sizeof(line32);
1329 if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1330 return FALSE;
1331 copy_line_W64_from_32(pcs, Line, &line32);
1332 return TRUE;
1335 /******************************************************************
1336 * SymGetLinePrev (DBGHELP.@)
1339 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1341 struct process* pcs = process_find_by_handle(hProcess);
1342 struct module_pair pair;
1343 struct line_info* li;
1344 BOOL in_search = FALSE;
1346 TRACE("(%p %p)\n", hProcess, Line);
1348 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1350 if (!pcs) return FALSE;
1351 pair.requested = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1352 if (!module_get_debug(pcs, &pair)) return FALSE;
1354 if (Line->Key == 0) return FALSE;
1355 li = (struct line_info*)Line->Key;
1356 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1357 * element we have to go back until we find the prev one to get the real
1358 * source file name for the DLIT_OFFSET element just before
1359 * the first DLIT_SOURCEFILE
1361 while (!li->is_first)
1363 li--;
1364 if (!li->is_source_file)
1366 Line->LineNumber = li->line_number;
1367 Line->Address = li->u.pc_offset;
1368 Line->Key = li;
1369 if (!in_search) return TRUE;
1371 else
1373 if (in_search)
1375 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1376 return TRUE;
1378 in_search = TRUE;
1381 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1382 return FALSE;
1385 /******************************************************************
1386 * SymGetLinePrev64 (DBGHELP.@)
1389 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1391 IMAGEHLP_LINE line32;
1393 line32.SizeOfStruct = sizeof(line32);
1394 copy_line_32_from_64(&line32, Line);
1395 if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1396 copy_line_64_from_32(Line, &line32);
1397 return TRUE;
1400 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1402 struct line_info* li;
1404 if (line->Key == 0) return FALSE;
1405 li = (struct line_info*)line->Key;
1406 while (!li->is_last)
1408 li++;
1409 if (!li->is_source_file)
1411 line->LineNumber = li->line_number;
1412 line->Address = li->u.pc_offset;
1413 line->Key = li;
1414 return TRUE;
1416 line->FileName = (char*)source_get(module, li->u.source_file);
1418 return FALSE;
1421 /******************************************************************
1422 * SymGetLineNext (DBGHELP.@)
1425 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1427 struct process* pcs = process_find_by_handle(hProcess);
1428 struct module_pair pair;
1430 TRACE("(%p %p)\n", hProcess, Line);
1432 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1433 if (!pcs) return FALSE;
1434 pair.requested = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1435 if (!module_get_debug(pcs, &pair)) return FALSE;
1437 if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1438 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1439 return FALSE;
1442 /******************************************************************
1443 * SymGetLineNext64 (DBGHELP.@)
1446 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1448 IMAGEHLP_LINE line32;
1450 line32.SizeOfStruct = sizeof(line32);
1451 copy_line_32_from_64(&line32, Line);
1452 if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1453 copy_line_64_from_32(Line, &line32);
1454 return TRUE;
1457 /***********************************************************************
1458 * SymFunctionTableAccess (DBGHELP.@)
1460 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1462 WARN("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1463 return NULL;
1466 /***********************************************************************
1467 * SymFunctionTableAccess64 (DBGHELP.@)
1469 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1471 WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1472 return NULL;
1475 /***********************************************************************
1476 * SymUnDName (DBGHELP.@)
1478 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1480 TRACE("(%p %s %lu)\n", sym, UnDecName, UnDecNameLength);
1481 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1482 UNDNAME_COMPLETE) != 0;
1485 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1486 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1488 /***********************************************************************
1489 * UnDecorateSymbolName (DBGHELP.@)
1491 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1492 DWORD UndecoratedLength, DWORD Flags)
1494 /* undocumented from msvcrt */
1495 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1496 static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1498 TRACE("(%s, %p, %ld, 0x%08lx)\n",
1499 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1501 if (!p_undname)
1503 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1504 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1505 if (!p_undname) return 0;
1508 if (!UnDecoratedName) return 0;
1509 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1510 und_alloc, und_free, Flags))
1511 return 0;
1512 return strlen(UnDecoratedName);
1515 /******************************************************************
1516 * SymMatchString (DBGHELP.@)
1519 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1521 regex_t preg;
1522 BOOL ret;
1524 TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1526 compile_regex(re, -1, &preg, _case);
1527 ret = regexec(&preg, string, 0, NULL, 0) == 0;
1528 regfree(&preg);
1529 return ret;
1532 /******************************************************************
1533 * SymSearch (DBGHELP.@)
1535 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1536 DWORD SymTag, PCSTR Mask, DWORD64 Address,
1537 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1538 PVOID UserContext, DWORD Options)
1540 struct sym_enum se;
1542 TRACE("(%p %s %lu %lu %s %s %p %p %lx)\n",
1543 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1544 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1545 UserContext, Options);
1547 if (Options != SYMSEARCH_GLOBALSONLY)
1549 FIXME("Unsupported searching with options (%lx)\n", Options);
1550 SetLastError(ERROR_INVALID_PARAMETER);
1551 return FALSE;
1554 se.cb = EnumSymbolsCallback;
1555 se.user = UserContext;
1556 se.index = Index;
1557 se.tag = SymTag;
1558 se.addr = Address;
1559 se.sym_info = (PSYMBOL_INFO)se.buffer;
1561 return sym_enum(hProcess, BaseOfDll, Mask, &se);
1564 /******************************************************************
1565 * SymSearchW (DBGHELP.@)
1567 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1568 DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1569 PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1570 PVOID UserContext, DWORD Options)
1572 struct sym_enumW sew;
1573 BOOL ret = FALSE;
1574 char* maskA = NULL;
1576 TRACE("(%p %s %lu %lu %s %s %p %p %lx)\n",
1577 hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1578 wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1579 UserContext, Options);
1581 sew.ctx = UserContext;
1582 sew.cb = EnumSymbolsCallback;
1583 sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1585 if (Mask)
1587 unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1588 maskA = HeapAlloc(GetProcessHeap(), 0, len);
1589 if (!maskA) return FALSE;
1590 WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1592 ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1593 sym_enumW, &sew, Options);
1594 HeapFree(GetProcessHeap(), 0, maskA);
1596 return ret;