30c390c6689dbe8642b906c773a3c2cbd8797339
[reactos.git] / reactos / lib / dbghelp / symbol.c
1 /*
2 * File symbol.c - management of symbols (lexical tree)
3 *
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004, Eric Pouech
6 *
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.
11 *
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.
16 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include "config.h"
26
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 #define HAVE_REGEX_H 1
34 #ifdef HAVE_REGEX_H
35 # include "regex.h"
36 #endif
37
38 #include "wine/debug.h"
39 #include "dbghelp_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
43
44 struct line_info
45 {
46 unsigned long is_first : 1,
47 is_last : 1,
48 is_source_file : 1,
49 line_number;
50 union
51 {
52 unsigned long pc_offset; /* if is_source_file isn't set */
53 unsigned source_file; /* if is_source_file is set */
54 } u;
55 };
56
57 inline static int cmp_addr(ULONG64 a1, ULONG64 a2)
58 {
59 if (a1 > a2) return 1;
60 if (a1 < a2) return -1;
61 return 0;
62 }
63
64 inline static int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
65 {
66 ULONG64 ref;
67
68 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
69 return cmp_addr(ref, addr);
70 }
71
72 int symt_cmp_addr(const void* p1, const void* p2)
73 {
74 const struct symt* sym1 = *(const struct symt* const *)p1;
75 const struct symt* sym2 = *(const struct symt* const *)p2;
76 ULONG64 a1, a2;
77
78 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
79 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
80 return cmp_addr(a1, a2);
81 }
82
83 static inline void re_append(char** mask, unsigned* len, char ch)
84 {
85 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
86 (*mask)[*len - 2] = ch;
87 }
88
89 /* transforms a dbghelp's regular expression into a POSIX one
90 * Here are the valid dbghelp reg ex characters:
91 * * 0 or more characters
92 * ? a single character
93 * [] list
94 * # 0 or more of preceding char
95 * + 1 or more of preceding char
96 * escapes \ on #, ?, [, ], *, +. don't work on -
97 */
98 static void compile_regex(const char* str, int numchar, regex_t* re)
99 {
100 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
101 unsigned len = 1;
102 BOOL in_escape = FALSE;
103
104 re_append(&mask, &len, '^');
105
106 while (*str && numchar--)
107 {
108 /* FIXME: this shouldn't be valid on '-' */
109 if (in_escape)
110 {
111 re_append(&mask, &len, '\\');
112 re_append(&mask, &len, *str);
113 in_escape = FALSE;
114 }
115 else switch (*str)
116 {
117 case '\\': in_escape = TRUE; break;
118 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
119 case '?': re_append(&mask, &len, '.'); break;
120 case '#': re_append(&mask, &len, '*'); break;
121 /* escape some valid characters in dbghelp reg exp:s */
122 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
123 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
124 default: re_append(&mask, &len, *str); break;
125 }
126 str++;
127 }
128 if (in_escape)
129 {
130 re_append(&mask, &len, '\\');
131 re_append(&mask, &len, '\\');
132 }
133 re_append(&mask, &len, '$');
134 mask[len - 1] = '\0';
135 if (regcomp(re, mask, REG_NOSUB)) FIXME("Couldn't compile %s\n", mask);
136 HeapFree(GetProcessHeap(), 0, mask);
137 }
138
139 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
140 {
141 struct symt_compiland* sym;
142
143 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
144 module->module.ModuleName, name);
145 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
146 {
147 sym->symt.tag = SymTagCompiland;
148 sym->source = source_new(module, name);
149 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
150 }
151 return sym;
152 }
153
154 struct symt_public* symt_new_public(struct module* module,
155 struct symt_compiland* compiland,
156 const char* name,
157 unsigned long address, unsigned size,
158 BOOL in_code, BOOL is_func)
159 {
160 struct symt_public* sym;
161 struct symt** p;
162
163 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
164 module->module.ModuleName, name, address);
165 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
166 symt_find_nearest(module, address) != -1)
167 return NULL;
168 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
169 {
170 sym->symt.tag = SymTagPublicSymbol;
171 sym->hash_elt.name = pool_strdup(&module->pool, name);
172 hash_table_add(&module->ht_symbols, &sym->hash_elt);
173 module->sortlist_valid = FALSE;
174 sym->container = compiland ? &compiland->symt : NULL;
175 sym->address = address;
176 sym->size = size;
177 sym->in_code = in_code;
178 sym->is_function = is_func;
179 if (compiland)
180 {
181 p = vector_add(&compiland->vchildren, &module->pool);
182 *p = &sym->symt;
183 }
184 }
185 return sym;
186 }
187
188 struct symt_data* symt_new_global_variable(struct module* module,
189 struct symt_compiland* compiland,
190 const char* name, unsigned is_static,
191 unsigned long addr, unsigned long size,
192 struct symt* type)
193 {
194 struct symt_data* sym;
195 struct symt** p;
196 DWORD tsz;
197
198 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
199 module->module.ModuleName, name, addr, type);
200 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
201 {
202 sym->symt.tag = SymTagData;
203 sym->hash_elt.name = pool_strdup(&module->pool, name);
204 hash_table_add(&module->ht_symbols, &sym->hash_elt);
205 module->sortlist_valid = FALSE;
206 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
207 sym->container = compiland ? &compiland->symt : NULL;
208 sym->type = type;
209 sym->u.address = addr;
210 if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
211 {
212 if (tsz != size)
213 FIXME("Size mismatch for %s.%s between type (%lu) and src (%lu)\n",
214 module->module.ModuleName, name, tsz, size);
215 }
216 if (compiland)
217 {
218 p = vector_add(&compiland->vchildren, &module->pool);
219 *p = &sym->symt;
220 }
221 }
222 return sym;
223 }
224
225 struct symt_function* symt_new_function(struct module* module,
226 struct symt_compiland* compiland,
227 const char* name,
228 unsigned long addr, unsigned long size,
229 struct symt* sig_type)
230 {
231 struct symt_function* sym;
232 struct symt** p;
233
234 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
235 module->module.ModuleName, name, addr, addr + size - 1);
236
237 assert(!sig_type || sig_type->tag == SymTagFunctionType);
238 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
239 {
240 sym->symt.tag = SymTagFunction;
241 sym->hash_elt.name = pool_strdup(&module->pool, name);
242 hash_table_add(&module->ht_symbols, &sym->hash_elt);
243 module->sortlist_valid = FALSE;
244 sym->container = &compiland->symt;
245 sym->address = addr;
246 sym->type = sig_type;
247 sym->size = size;
248 vector_init(&sym->vlines, sizeof(struct line_info), 64);
249 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
250 if (compiland)
251 {
252 p = vector_add(&compiland->vchildren, &module->pool);
253 *p = &sym->symt;
254 }
255 }
256 return sym;
257 }
258
259 void symt_add_func_line(struct module* module, struct symt_function* func,
260 unsigned source_idx, int line_num, unsigned long offset)
261 {
262 struct line_info* dli;
263 BOOL last_matches = FALSE;
264
265 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
266
267 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
268 func, func->hash_elt.name, offset,
269 source_get(module, source_idx), line_num);
270
271 assert(func->symt.tag == SymTagFunction);
272
273 dli = NULL;
274 while ((dli = vector_iter_down(&func->vlines, dli)))
275 {
276 if (dli->is_source_file)
277 {
278 last_matches = (source_idx == dli->u.source_file);
279 break;
280 }
281 }
282
283 if (!last_matches)
284 {
285 /* we shouldn't have line changes on first line of function */
286 dli = vector_add(&func->vlines, &module->pool);
287 dli->is_source_file = 1;
288 dli->is_first = dli->is_last = 0;
289 dli->line_number = 0;
290 dli->u.source_file = source_idx;
291 }
292 dli = vector_add(&func->vlines, &module->pool);
293 dli->is_source_file = 0;
294 dli->is_first = dli->is_last = 0;
295 dli->line_number = line_num;
296 dli->u.pc_offset = func->address + offset;
297 }
298
299 struct symt_data* symt_add_func_local(struct module* module,
300 struct symt_function* func,
301 int regno, int offset,
302 struct symt_block* block,
303 struct symt* type, const char* name)
304 {
305 struct symt_data* locsym;
306 struct symt** p;
307
308 assert(func);
309 assert(func->symt.tag == SymTagFunction);
310
311 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
312 module->module.ModuleName, func->hash_elt.name,
313 name, type);
314 locsym = pool_alloc(&module->pool, sizeof(*locsym));
315 locsym->symt.tag = SymTagData;
316 locsym->hash_elt.name = pool_strdup(&module->pool, name);
317 locsym->hash_elt.next = NULL;
318 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
319 locsym->container = &block->symt;
320 locsym->type = type;
321 if (regno)
322 {
323 locsym->u.s.reg_id = regno;
324 locsym->u.s.offset = 0;
325 locsym->u.s.length = 0;
326 }
327 else
328 {
329 locsym->u.s.reg_id = 0;
330 locsym->u.s.offset = offset * 8;
331 locsym->u.s.length = 0;
332 }
333 if (block)
334 p = vector_add(&block->vchildren, &module->pool);
335 else
336 p = vector_add(&func->vchildren, &module->pool);
337 *p = &locsym->symt;
338 return locsym;
339 }
340
341 struct symt_block* symt_open_func_block(struct module* module,
342 struct symt_function* func,
343 struct symt_block* parent_block,
344 unsigned pc, unsigned len)
345 {
346 struct symt_block* block;
347 struct symt** p;
348
349 assert(func);
350 assert(func->symt.tag == SymTagFunction);
351
352 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
353 block = pool_alloc(&module->pool, sizeof(*block));
354 block->symt.tag = SymTagBlock;
355 block->address = func->address + pc;
356 block->size = len;
357 block->container = parent_block ? &parent_block->symt : &func->symt;
358 vector_init(&block->vchildren, sizeof(struct symt*), 4);
359 if (parent_block)
360 p = vector_add(&parent_block->vchildren, &module->pool);
361 else
362 p = vector_add(&func->vchildren, &module->pool);
363 *p = &block->symt;
364
365 return block;
366 }
367
368 struct symt_block* symt_close_func_block(struct module* module,
369 struct symt_function* func,
370 struct symt_block* block, unsigned pc)
371 {
372 assert(func->symt.tag == SymTagFunction);
373
374 if (pc) block->size = func->address + pc - block->address;
375 return (block->container->tag == SymTagBlock) ?
376 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
377 }
378
379 struct symt_function_point* symt_add_function_point(struct module* module,
380 struct symt_function* func,
381 enum SymTagEnum point,
382 unsigned offset, const char* name)
383 {
384 struct symt_function_point* sym;
385 struct symt** p;
386
387 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
388 {
389 sym->symt.tag = point;
390 sym->parent = func;
391 sym->offset = offset;
392 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
393 p = vector_add(&func->vchildren, &module->pool);
394 *p = &sym->symt;
395 }
396 return sym;
397 }
398
399 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
400 {
401 unsigned len;
402 struct line_info* dli;
403
404 assert(func);
405 /* We aren't adding any more locals or line numbers to this function.
406 * Free any spare memory that we might have allocated.
407 */
408 assert(func->symt.tag == SymTagFunction);
409
410 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
411 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
412
413 len = vector_length(&func->vlines);
414 if (len--)
415 {
416 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
417 dli = vector_at(&func->vlines, len); dli->is_last = 1;
418 }
419 return TRUE;
420 }
421
422 struct symt_thunk* symt_new_thunk(struct module* module,
423 struct symt_compiland* compiland,
424 const char* name, THUNK_ORDINAL ord,
425 unsigned long addr, unsigned long size)
426 {
427 struct symt_thunk* sym;
428
429 TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
430 module->module.ModuleName, name, addr, addr + size - 1);
431
432 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
433 {
434 sym->symt.tag = SymTagThunk;
435 sym->hash_elt.name = pool_strdup(&module->pool, name);
436 hash_table_add(&module->ht_symbols, &sym->hash_elt);
437 module->sortlist_valid = FALSE;
438 sym->container = &compiland->symt;
439 sym->address = addr;
440 sym->size = size;
441 sym->ordinal = ord;
442 if (compiland)
443 {
444 struct symt** p;
445 p = vector_add(&compiland->vchildren, &module->pool);
446 *p = &sym->symt;
447 }
448 }
449 return sym;
450 }
451
452 /* expect sym_info->MaxNameLen to be set before being called */
453 static void symt_fill_sym_info(const struct module* module,
454 const struct symt* sym, SYMBOL_INFO* sym_info)
455 {
456 const char* name;
457
458 sym_info->TypeIndex = (DWORD)sym;
459 sym_info->info = 0; /* TBD */
460 symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
461 sym_info->ModBase = module->module.BaseOfImage;
462 sym_info->Flags = 0;
463 switch (sym->tag)
464 {
465 case SymTagData:
466 {
467 const struct symt_data* data = (const struct symt_data*)sym;
468 switch (data->kind)
469 {
470 case DataIsLocal:
471 case DataIsParam:
472 if (data->u.s.reg_id)
473 {
474 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGISTER;
475 sym_info->Register = data->u.s.reg_id;
476 sym_info->Address = 0;
477 }
478 else
479 {
480 if (data->u.s.offset < 0)
481 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
482 else
483 sym_info->Flags |= SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
484 /* FIXME: needed ? moreover, it's i386 dependent !!! */
485 sym_info->Register = CV_REG_EBP;
486 sym_info->Address = data->u.s.offset;
487 }
488 break;
489 case DataIsGlobal:
490 case DataIsFileStatic:
491 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
492 sym_info->Register = 0;
493 break;
494 case DataIsConstant:
495 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
496 switch (data->u.value.n1.n2.vt)
497 {
498 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
499 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
500 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
501 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
502 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
503 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
504 default:
505 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
506 }
507 break;
508 default:
509 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
510 }
511 }
512 break;
513 case SymTagPublicSymbol:
514 sym_info->Flags |= SYMFLAG_EXPORT;
515 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
516 break;
517 case SymTagFunction:
518 sym_info->Flags |= SYMFLAG_FUNCTION;
519 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
520 break;
521 case SymTagThunk:
522 sym_info->Flags |= SYMFLAG_THUNK;
523 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
524 break;
525 default:
526 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
527 sym_info->Register = 0;
528 break;
529 }
530 sym_info->Scope = 0; /* FIXME */
531 sym_info->Tag = sym->tag;
532 name = symt_get_name(sym);
533 if (sym_info->MaxNameLen)
534 {
535 if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
536 (sym_info->NameLen = UnDecorateSymbolName(sym_info->Name, sym_info->Name,
537 sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
538 {
539 sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
540 strncpy(sym_info->Name, name, sym_info->NameLen);
541 sym_info->Name[sym_info->NameLen] = '\0';
542 }
543 }
544 TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
545 sym, sym_info->Name, sym_info->Size,
546 wine_dbgstr_longlong(sym_info->Address));
547 }
548
549 static BOOL symt_enum_module(struct module* module, regex_t* regex,
550 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
551 {
552 char buffer[sizeof(SYMBOL_INFO) + 256];
553 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
554 void* ptr;
555 struct symt_ht* sym = NULL;
556 struct hash_table_iter hti;
557
558 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
559 while ((ptr = hash_table_iter_up(&hti)))
560 {
561 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
562 if (sym->hash_elt.name &&
563 regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
564 {
565 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
566 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
567 symt_fill_sym_info(module, &sym->symt, sym_info);
568 if (!cb(sym_info, sym_info->Size, user)) return TRUE;
569 }
570 }
571 return FALSE;
572 }
573
574 /***********************************************************************
575 * resort_symbols
576 *
577 * Rebuild sorted list of symbols for a module.
578 */
579 static BOOL resort_symbols(struct module* module)
580 {
581 int nsym = 0;
582 void* ptr;
583 struct symt_ht* sym;
584 struct hash_table_iter hti;
585
586 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
587 while ((ptr = hash_table_iter_up(&hti)))
588 nsym++;
589
590 if (!(module->module.NumSyms = nsym)) return FALSE;
591
592 if (module->addr_sorttab)
593 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
594 module->addr_sorttab,
595 nsym * sizeof(struct symt_ht*));
596 else
597 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
598 nsym * sizeof(struct symt_ht*));
599 if (!module->addr_sorttab) return FALSE;
600
601 nsym = 0;
602 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
603 while ((ptr = hash_table_iter_up(&hti)))
604 {
605 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
606 assert(sym);
607 module->addr_sorttab[nsym++] = sym;
608 }
609
610 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
611 return module->sortlist_valid = TRUE;
612 }
613
614 /* assume addr is in module */
615 int symt_find_nearest(struct module* module, DWORD addr)
616 {
617 int mid, high, low;
618 ULONG64 ref_addr;
619 DWORD ref_size;
620
621 if (!module->sortlist_valid || !module->addr_sorttab)
622 {
623 if (!resort_symbols(module)) return -1;
624 }
625
626 /*
627 * Binary search to find closest symbol.
628 */
629 low = 0;
630 high = module->module.NumSyms;
631
632 symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
633 if (addr < ref_addr) return -1;
634 if (high)
635 {
636 symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
637 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
638 ref_size = 0x1000; /* arbitrary value */
639 if (addr >= ref_addr + ref_size) return -1;
640 }
641
642 while (high > low + 1)
643 {
644 mid = (high + low) / 2;
645 if (cmp_sorttab_addr(module, mid, addr) < 0)
646 low = mid;
647 else
648 high = mid;
649 }
650 if (low != high && high != module->module.NumSyms &&
651 cmp_sorttab_addr(module, high, addr) <= 0)
652 low = high;
653
654 /* If found symbol is a public symbol, check if there are any other entries that
655 * might also have the same address, but would get better information
656 */
657 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
658 {
659 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
660 if (low > 0 &&
661 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
662 !cmp_sorttab_addr(module, low - 1, ref_addr))
663 low--;
664 else if (low < module->module.NumSyms - 1 &&
665 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
666 !cmp_sorttab_addr(module, low + 1, ref_addr))
667 low++;
668 }
669 /* finally check that we fit into the found symbol */
670 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
671 if (addr < ref_addr) return -1;
672 if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
673 ref_size = 0x1000; /* arbitrary value */
674 if (addr >= ref_addr + ref_size) return -1;
675
676 return low;
677 }
678
679 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
680 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
681 PVOID user, SYMBOL_INFO* sym_info,
682 struct vector* v)
683 {
684 struct symt** plsym = NULL;
685 struct symt* lsym = NULL;
686 DWORD pc = pcs->ctx_frame.InstructionOffset;
687
688 while ((plsym = vector_iter_up(v, plsym)))
689 {
690 lsym = *plsym;
691 switch (lsym->tag)
692 {
693 case SymTagBlock:
694 {
695 struct symt_block* block = (struct symt_block*)lsym;
696 if (pc < block->address || block->address + block->size <= pc)
697 continue;
698 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
699 sym_info, &block->vchildren))
700 return FALSE;
701 }
702 break;
703 case SymTagData:
704 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
705 {
706 symt_fill_sym_info(module, lsym, sym_info);
707 if (!cb(sym_info, sym_info->Size, user))
708 return FALSE;
709 }
710 break;
711 case SymTagLabel:
712 case SymTagFuncDebugStart:
713 case SymTagFuncDebugEnd:
714 break;
715 default:
716 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
717 assert(0);
718 }
719 }
720 return TRUE;
721 }
722
723 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
724 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
725 PVOID UserContext)
726 {
727 struct module* module;
728 struct symt_ht* sym;
729 char buffer[sizeof(SYMBOL_INFO) + 256];
730 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
731 DWORD pc = pcs->ctx_frame.InstructionOffset;
732 int idx;
733
734 sym_info->SizeOfStruct = sizeof(*sym_info);
735 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
736
737 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
738 if (!(module = module_get_debug(pcs, module))) return FALSE;
739 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
740
741 sym = module->addr_sorttab[idx];
742 if (sym->symt.tag == SymTagFunction)
743 {
744 BOOL ret;
745 regex_t preg;
746
747 compile_regex(mask ? mask : "*", -1, &preg);
748 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
749 UserContext, sym_info,
750 &((struct symt_function*)sym)->vchildren);
751 regfree(&preg);
752 return ret;
753
754 }
755 symt_fill_sym_info(module, &sym->symt, sym_info);
756 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
757 }
758
759 /******************************************************************
760 * SymEnumSymbols (DBGHELP.@)
761 *
762 * cases BaseOfDll = 0
763 * !foo fails always (despite what MSDN states)
764 * RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
765 * no ! in Mask, lookup in local Context
766 * cases BaseOfDll != 0
767 * !foo fails always (despite what MSDN states)
768 * RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
769 */
770 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
771 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
772 PVOID UserContext)
773 {
774 struct process* pcs = process_find_by_handle(hProcess);
775 struct module* module;
776 struct module* dbg_module;
777 const char* bang;
778 regex_t mod_regex, sym_regex;
779
780 TRACE("(%p %s %s %p %p)\n",
781 hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
782 EnumSymbolsCallback, UserContext);
783
784 if (!pcs) return FALSE;
785
786 if (BaseOfDll == 0)
787 {
788 /* do local variables ? */
789 if (!Mask || !(bang = strchr(Mask, '!')))
790 return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
791
792 if (bang == Mask) return FALSE;
793
794 compile_regex(Mask, bang - Mask, &mod_regex);
795 compile_regex(bang + 1, -1, &sym_regex);
796
797 for (module = pcs->lmodules; module; module = module->next)
798 {
799 if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
800 {
801 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
802 symt_enum_module(dbg_module, &sym_regex,
803 EnumSymbolsCallback, UserContext))
804 break;
805 }
806 }
807 /* not found in PE modules, retry on the ELF ones
808 */
809 if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
810 {
811 for (module = pcs->lmodules; module; module = module->next)
812 {
813 if (module->type == DMT_ELF &&
814 !module_get_containee(pcs, module) &&
815 (dbg_module = module_get_debug(pcs, module)))
816 {
817 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
818 symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
819 break;
820 }
821 }
822 }
823 regfree(&mod_regex);
824 regfree(&sym_regex);
825 return TRUE;
826 }
827 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
828 if (!(module = module_get_debug(pcs, module)))
829 return FALSE;
830
831 /* we always ignore module name from Mask when BaseOfDll is defined */
832 if (Mask && (bang = strchr(Mask, '!')))
833 {
834 if (bang == Mask) return FALSE;
835 Mask = bang + 1;
836 }
837
838 compile_regex(Mask ? Mask : "*", -1, &sym_regex);
839 symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
840 regfree(&sym_regex);
841
842 return TRUE;
843 }
844
845 struct sym_enumerate
846 {
847 void* ctx;
848 PSYM_ENUMSYMBOLS_CALLBACK cb;
849 };
850
851 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
852 {
853 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
854 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
855 }
856
857 /***********************************************************************
858 * SymEnumerateSymbols (DBGHELP.@)
859 */
860 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
861 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
862 PVOID UserContext)
863 {
864 struct sym_enumerate se;
865
866 se.ctx = UserContext;
867 se.cb = EnumSymbolsCallback;
868
869 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
870 }
871
872 /******************************************************************
873 * SymFromAddr (DBGHELP.@)
874 *
875 */
876 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address,
877 DWORD64* Displacement, PSYMBOL_INFO Symbol)
878 {
879 struct process* pcs = process_find_by_handle(hProcess);
880 struct module* module;
881 struct symt_ht* sym;
882 int idx;
883
884 if (!pcs) return FALSE;
885 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
886 if (!(module = module_get_debug(pcs, module))) return FALSE;
887 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
888
889 sym = module->addr_sorttab[idx];
890
891 symt_fill_sym_info(module, &sym->symt, Symbol);
892 if (Displacement) *Displacement = Address - Symbol->Address;
893 return TRUE;
894 }
895
896 /******************************************************************
897 * SymGetSymFromAddr (DBGHELP.@)
898 *
899 */
900 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
901 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
902 {
903 char buffer[sizeof(SYMBOL_INFO) + 256];
904 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
905 size_t len;
906 DWORD64 Displacement64;
907
908 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
909 si->SizeOfStruct = sizeof(*si);
910 si->MaxNameLen = 256;
911 if (!SymFromAddr(hProcess, Address, &Displacement64, si))
912 return FALSE;
913
914 if (Displacement)
915 *Displacement = Displacement64;
916 Symbol->Address = si->Address;
917 Symbol->Size = si->Size;
918 Symbol->Flags = si->Flags;
919 len = min(Symbol->MaxNameLength, si->MaxNameLen);
920 strncpy(Symbol->Name, si->Name, len);
921 Symbol->Name[len - 1] = '\0';
922 return TRUE;
923 }
924
925 /******************************************************************
926 * SymFromName (DBGHELP.@)
927 *
928 */
929 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
930 {
931 struct process* pcs = process_find_by_handle(hProcess);
932 struct module* module;
933 struct hash_table_iter hti;
934 void* ptr;
935 struct symt_ht* sym = NULL;
936 const char* name;
937
938 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
939 if (!pcs) return FALSE;
940 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
941 name = strchr(Name, '!');
942 if (name)
943 {
944 char tmp[128];
945 assert(name - Name < sizeof(tmp));
946 memcpy(tmp, Name, name - Name);
947 tmp[name - Name] = '\0';
948 module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
949 if (!module) return FALSE;
950 Name = (char*)(name + 1);
951 }
952 else module = pcs->lmodules;
953
954 /* FIXME: Name could be made out of a regular expression */
955 for (; module; module = (name) ? NULL : module->next)
956 {
957 if (module->module.SymType == SymNone) continue;
958 if (module->module.SymType == SymDeferred)
959 {
960 struct module* xmodule = module_get_debug(pcs, module);
961 if (!xmodule || xmodule != module) continue;
962 }
963 hash_table_iter_init(&module->ht_symbols, &hti, Name);
964 while ((ptr = hash_table_iter_up(&hti)))
965 {
966 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
967
968 if (!strcmp(sym->hash_elt.name, Name))
969 {
970 symt_fill_sym_info(module, &sym->symt, Symbol);
971 return TRUE;
972 }
973 }
974 }
975 return FALSE;
976 }
977
978 /***********************************************************************
979 * SymGetSymFromName (DBGHELP.@)
980 */
981 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
982 {
983 char buffer[sizeof(SYMBOL_INFO) + 256];
984 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
985 size_t len;
986
987 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
988 si->SizeOfStruct = sizeof(*si);
989 si->MaxNameLen = 256;
990 if (!SymFromName(hProcess, Name, si)) return FALSE;
991
992 Symbol->Address = si->Address;
993 Symbol->Size = si->Size;
994 Symbol->Flags = si->Flags;
995 len = min(Symbol->MaxNameLength, si->MaxNameLen);
996 strncpy(Symbol->Name, si->Name, len);
997 Symbol->Name[len - 1] = '\0';
998 return TRUE;
999 }
1000
1001 /******************************************************************
1002 * sym_fill_func_line_info
1003 *
1004 * fills information about a file
1005 */
1006 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
1007 DWORD addr, IMAGEHLP_LINE* line)
1008 {
1009 struct line_info* dli = NULL;
1010 BOOL found = FALSE;
1011
1012 assert(func->symt.tag == SymTagFunction);
1013
1014 while ((dli = vector_iter_down(&func->vlines, dli)))
1015 {
1016 if (!dli->is_source_file)
1017 {
1018 if (found || dli->u.pc_offset > addr) continue;
1019 line->LineNumber = dli->line_number;
1020 line->Address = dli->u.pc_offset;
1021 line->Key = dli;
1022 found = TRUE;
1023 continue;
1024 }
1025 if (found)
1026 {
1027 line->FileName = (char*)source_get(module, dli->u.source_file);
1028 return TRUE;
1029 }
1030 }
1031 return FALSE;
1032 }
1033
1034 /***********************************************************************
1035 * SymGetSymNext (DBGHELP.@)
1036 */
1037 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1038 {
1039 /* algo:
1040 * get module from Symbol.Address
1041 * get index in module.addr_sorttab of Symbol.Address
1042 * increment index
1043 * if out of module bounds, move to next module in process address space
1044 */
1045 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1046 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1047 return FALSE;
1048 }
1049
1050 /***********************************************************************
1051 * SymGetSymPrev (DBGHELP.@)
1052 */
1053
1054 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1055 {
1056 FIXME("(%p, %p): stub\n", hProcess, Symbol);
1057 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1058 return FALSE;
1059 }
1060
1061 /******************************************************************
1062 * SymGetLineFromAddr (DBGHELP.@)
1063 *
1064 */
1065 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1066 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1067 {
1068 struct process* pcs = process_find_by_handle(hProcess);
1069 struct module* module;
1070 int idx;
1071
1072 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1073
1074 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1075
1076 if (!pcs) return FALSE;
1077 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1078 if (!(module = module_get_debug(pcs, module))) return FALSE;
1079 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1080
1081 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1082 if (!symt_fill_func_line_info(module,
1083 (struct symt_function*)module->addr_sorttab[idx],
1084 dwAddr, Line)) return FALSE;
1085 if (pdwDisplacement) *pdwDisplacement = dwAddr - Line->Address;
1086 return TRUE;
1087 }
1088
1089 /******************************************************************
1090 * SymGetLinePrev (DBGHELP.@)
1091 *
1092 */
1093 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1094 {
1095 struct process* pcs = process_find_by_handle(hProcess);
1096 struct module* module;
1097 struct line_info* li;
1098 BOOL in_search = FALSE;
1099
1100 TRACE("(%p %p)\n", hProcess, Line);
1101
1102 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1103
1104 if (!pcs) return FALSE;
1105 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1106 if (!(module = module_get_debug(pcs, module))) return FALSE;
1107
1108 if (Line->Key == 0) return FALSE;
1109 li = (struct line_info*)Line->Key;
1110 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1111 * element we have to go back until we find the prev one to get the real
1112 * source file name for the DLIT_OFFSET element just before
1113 * the first DLIT_SOURCEFILE
1114 */
1115 while (!li->is_first)
1116 {
1117 li--;
1118 if (!li->is_source_file)
1119 {
1120 Line->LineNumber = li->line_number;
1121 Line->Address = li->u.pc_offset;
1122 Line->Key = li;
1123 if (!in_search) return TRUE;
1124 }
1125 else
1126 {
1127 if (in_search)
1128 {
1129 Line->FileName = (char*)source_get(module, li->u.source_file);
1130 return TRUE;
1131 }
1132 in_search = TRUE;
1133 }
1134 }
1135 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1136 return FALSE;
1137 }
1138
1139 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1140 {
1141 struct line_info* li;
1142
1143 if (line->Key == 0) return FALSE;
1144 li = (struct line_info*)line->Key;
1145 while (!li->is_last)
1146 {
1147 li++;
1148 if (!li->is_source_file)
1149 {
1150 line->LineNumber = li->line_number;
1151 line->Address = li->u.pc_offset;
1152 line->Key = li;
1153 return TRUE;
1154 }
1155 line->FileName = (char*)source_get(module, li->u.source_file);
1156 }
1157 return FALSE;
1158 }
1159
1160 /******************************************************************
1161 * SymGetLineNext (DBGHELP.@)
1162 *
1163 */
1164 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1165 {
1166 struct process* pcs = process_find_by_handle(hProcess);
1167 struct module* module;
1168
1169 TRACE("(%p %p)\n", hProcess, Line);
1170
1171 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1172 if (!pcs) return FALSE;
1173 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1174 if (!(module = module_get_debug(pcs, module))) return FALSE;
1175
1176 if (symt_get_func_line_next(module, Line)) return TRUE;
1177 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1178 return FALSE;
1179 }
1180
1181 /***********************************************************************
1182 * SymFunctionTableAccess (DBGHELP.@)
1183 */
1184 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1185 {
1186 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1187 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1188 return FALSE;
1189 }
1190
1191 /***********************************************************************
1192 * SymUnDName (DBGHELP.@)
1193 */
1194 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1195 {
1196 TRACE("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1197 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1198 UNDNAME_COMPLETE) != 0;
1199 }
1200
1201 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1202 static void und_free (void* ptr) { HeapFree(GetProcessHeap(), 0, ptr); }
1203
1204 /***********************************************************************
1205 * UnDecorateSymbolName (DBGHELP.@)
1206 */
1207 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1208 DWORD UndecoratedLength, DWORD Flags)
1209 {
1210 /* undocumented from msvcrt */
1211 static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1212 static WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1213
1214 TRACE("(%s, %p, %ld, 0x%08lx): stub\n",
1215 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1216
1217 if (!p_undname)
1218 {
1219 if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1220 if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1221 if (!p_undname) return 0;
1222 }
1223
1224 if (!UnDecoratedName) return 0;
1225 if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength,
1226 und_alloc, und_free, Flags))
1227 return 0;
1228 return strlen(UnDecoratedName);
1229 }