2 * File symbol.c - management of symbols (lexical tree)
4 * Copyright (C) 1993, Eric Youngdale.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
30 #include <sys/types.h>
33 #include "wine/debug.h"
35 #include "dbghelp_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
38 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symtype);
40 #define DLIT_OFFSET 0x00
41 #define DLIT_FIRST 0x01
42 #define DLIT_LAST 0x02
43 #define DLIT_SOURCEFILE 0x04
47 unsigned long cookie : 3,
51 unsigned long pc_offset;
56 inline static int cmp_addr(DWORD a1, DWORD a2)
58 if (a1 > a2) return 1;
59 if (a1 < a2) return -1;
63 inline static int cmp_sorttab_addr(const struct module* module, int idx, DWORD addr)
67 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
68 return cmp_addr(ref, addr);
71 int symt_cmp_addr(const void* p1, const void* p2)
73 struct symt* sym1 = *(struct symt**)p1;
74 struct symt* sym2 = *(struct symt**)p2;
77 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
78 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
79 return cmp_addr(a1, a2);
82 static inline void re_append(char** mask, unsigned* len, char ch)
84 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
85 (*mask)[*len - 2] = ch;
88 /* transforms a dbghelp's regular expression into a POSIX one
89 * Here are the valid dbghelp reg ex characters:
90 * * 0 or more characters
91 * ? a single character
93 * # 0 or more of preceding char
94 * + 1 or more of preceding char
95 * escapes \ on #, ?, [, ], *, +. don't work on -
97 static void compile_regex(const char* str, regex_t* re)
99 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
101 BOOL in_escape = FALSE;
103 re_append(&mask, &len, '^');
106 /* FIXME: this shouldn't be valid on '-' */
109 re_append(&mask, &len, '\\');
110 re_append(&mask, &len, *str);
115 case '\\': in_escape = TRUE; break;
116 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
117 case '?': re_append(&mask, &len, '.'); break;
118 case '#': re_append(&mask, &len, '*'); break;
119 /* escape some valid characters in dbghelp reg exp:s */
120 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
121 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
122 default: re_append(&mask, &len, *str); break;
128 re_append(&mask, &len, '\\');
129 re_append(&mask, &len, '\\');
131 re_append(&mask, &len, '$');
132 mask[len - 1] = '\0';
133 regcomp(re, mask, REG_NOSUB);
134 HeapFree(GetProcessHeap(), 0, mask);
137 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
139 struct symt_compiland* sym;
141 TRACE_(dbghelp_symtype)("Adding compiland symbol %s:%s\n",
142 module->module.ModuleName, name);
143 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
145 sym->symt.tag = SymTagCompiland;
146 sym->source = source_new(module, name);
147 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
152 struct symt_public* symt_new_public(struct module* module,
153 struct symt_compiland* compiland,
155 unsigned long address, unsigned size,
156 BOOL in_code, BOOL is_func)
158 struct symt_public* sym;
161 TRACE_(dbghelp_symtype)("Adding public symbol %s:%s @%lx\n",
162 module->module.ModuleName, name, address);
163 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
165 sym->symt.tag = SymTagPublicSymbol;
166 sym->hash_elt.name = pool_strdup(&module->pool, name);
167 hash_table_add(&module->ht_symbols, &sym->hash_elt);
168 module->sortlist_valid = FALSE;
169 sym->container = compiland ? &compiland->symt : NULL;
170 sym->address = address;
172 sym->in_code = in_code;
173 sym->is_function = is_func;
176 p = vector_add(&compiland->vchildren, &module->pool);
183 struct symt_data* symt_new_global_variable(struct module* module,
184 struct symt_compiland* compiland,
185 const char* name, unsigned is_static,
186 unsigned long addr, unsigned long size,
189 struct symt_data* sym;
192 TRACE_(dbghelp_symtype)("Adding global symbol %s:%s @%lx %p\n",
193 module->module.ModuleName, name, addr, type);
194 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
196 sym->symt.tag = SymTagData;
197 sym->hash_elt.name = pool_strdup(&module->pool, name);
198 hash_table_add(&module->ht_symbols, &sym->hash_elt);
199 module->sortlist_valid = FALSE;
200 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
201 sym->container = compiland ? &compiland->symt : NULL;
203 sym->location = LocIsStatic; /* FIXME */
204 sym->u.address = addr;
207 p = vector_add(&compiland->vchildren, &module->pool);
214 struct symt_function* symt_new_function(struct module* module,
215 struct symt_compiland* compiland,
217 unsigned long addr, unsigned long size,
218 struct symt* sig_type)
220 struct symt_function* sym;
223 TRACE_(dbghelp_symtype)("Adding global function %s:%s @%lx-%lx\n",
224 module->module.ModuleName, name, addr, addr + size - 1);
226 assert(!sig_type || sig_type->tag == SymTagFunctionType);
227 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
229 sym->symt.tag = SymTagFunction;
230 sym->hash_elt.name = pool_strdup(&module->pool, name);
231 hash_table_add(&module->ht_symbols, &sym->hash_elt);
232 module->sortlist_valid = FALSE;
233 sym->container = &compiland->symt;
235 sym->type = sig_type;
238 vector_init(&sym->vlines, sizeof(struct line_info), 64);
239 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
242 p = vector_add(&compiland->vchildren, &module->pool);
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_symtype)("(%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);
264 while ((dli = vector_iter_down(&func->vlines, dli)))
266 if (dli->cookie & DLIT_SOURCEFILE)
268 last_matches = (source_idx == dli->u.source_file);
275 /* we shouldn't have line changes on first line of function */
276 dli = vector_add(&func->vlines, &module->pool);
277 dli->cookie = DLIT_SOURCEFILE;
278 dli->line_number = 0;
279 dli->u.source_file = source_idx;
281 dli = vector_add(&func->vlines, &module->pool);
282 dli->cookie = DLIT_OFFSET;
283 dli->line_number = line_num;
284 dli->u.pc_offset = func->addr + offset;
287 struct symt_data* symt_add_func_local(struct module* module,
288 struct symt_function* func,
289 int regno, int offset,
290 struct symt_block* block,
291 struct symt* type, const char* name)
293 struct symt_data* locsym;
297 assert(func->symt.tag == SymTagFunction);
299 TRACE_(dbghelp_symtype)("Adding local symbol (%s:%s): %s %p\n",
300 module->module.ModuleName, func->hash_elt.name,
302 locsym = pool_alloc(&module->pool, sizeof(*locsym));
303 locsym->symt.tag = SymTagData;
304 locsym->hash_elt.name = pool_strdup(&module->pool, name);
305 locsym->hash_elt.next = NULL;
306 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
307 locsym->container = &block->symt;
311 locsym->location = LocIsEnregistered;
312 locsym->u.reg_id = regno;
316 locsym->location = LocIsRegRel;
317 locsym->u.reg_id = CV_REG_EBP;
318 locsym->u.offset = offset;
321 p = vector_add(&block->vchildren, &module->pool);
323 p = vector_add(&func->vchildren, &module->pool);
328 struct symt_block* symt_open_func_block(struct module* module,
329 struct symt_function* func,
330 struct symt_block* parent_block,
331 unsigned pc, unsigned len)
333 struct symt_block* block;
337 assert(func->symt.tag == SymTagFunction);
339 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
340 block = pool_alloc(&module->pool, sizeof(*block));
341 block->symt.tag = SymTagBlock;
342 block->address = func->addr + pc;
344 block->container = parent_block ? &parent_block->symt : &func->symt;
345 vector_init(&block->vchildren, sizeof(struct symt*), 4);
347 p = vector_add(&parent_block->vchildren, &module->pool);
349 p = vector_add(&func->vchildren, &module->pool);
355 struct symt_block* symt_close_func_block(struct module* module,
356 struct symt_function* func,
357 struct symt_block* block, unsigned pc)
359 assert(func->symt.tag == SymTagFunction);
361 if (pc) block->size = func->addr + pc - block->address;
362 return (block->container->tag == SymTagBlock) ?
363 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
366 struct symt_function_point* symt_add_function_point(struct module* module,
367 struct symt_function* func,
368 enum SymTagEnum point,
369 unsigned offset, const char* name)
371 struct symt_function_point* sym;
374 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
376 sym->symt.tag = point;
378 sym->offset = offset;
379 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
380 p = vector_add(&func->vchildren, &module->pool);
386 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
389 struct line_info* dli;
392 /* We aren't adding any more locals or line numbers to this function.
393 * Free any spare memory that we might have allocated.
395 assert(func->symt.tag == SymTagFunction);
397 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
398 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
400 len = vector_length(&func->vlines);
403 dli = vector_at(&func->vlines, 0); dli->cookie |= DLIT_FIRST;
404 dli = vector_at(&func->vlines, len); dli->cookie |= DLIT_LAST;
409 /* expect sym_info->MaxNameLen to be set before being called */
410 static void symt_fill_sym_info(const struct module* module,
411 const struct symt* sym, SYMBOL_INFO* sym_info)
415 sym_info->TypeIndex = (DWORD)sym;
416 sym_info->info = 0; /* TBD */
417 symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
418 sym_info->ModBase = module->module.BaseOfImage;
424 struct symt_data* data = (struct symt_data*)sym;
425 switch (data->location)
427 case LocIsEnregistered:
428 sym_info->Flags |= SYMFLAG_REGISTER;
429 sym_info->Register = data->u.reg_id;
430 sym_info->Address = 0;
434 ((data->u.offset < 0) ? SYMFLAG_LOCAL : SYMFLAG_PARAMETER) |
436 sym_info->Register = data->u.reg_id;
437 sym_info->Address = data->u.offset;
440 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
441 sym_info->Register = 0;
444 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
445 switch (data->u.value.n1.n2.vt)
447 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
448 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
449 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
450 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
451 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
452 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
454 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
458 FIXME("Unhandled loc (%u) in sym data\n", data->location);
462 case SymTagPublicSymbol:
463 sym_info->Flags |= SYMFLAG_EXPORT;
464 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
467 sym_info->Flags |= SYMFLAG_FUNCTION;
468 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
471 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
472 sym_info->Register = 0;
475 sym_info->Scope = 0; /* FIXME */
476 sym_info->Tag = sym->tag;
477 name = symt_get_name(sym);
478 sym_info->NameLen = strlen(name) + 1;
479 if (sym_info->MaxNameLen)
481 strncpy(sym_info->Name, name, min(sym_info->NameLen, sym_info->MaxNameLen));
482 sym_info->Name[sym_info->MaxNameLen - 1] = '\0';
484 TRACE_(dbghelp_symtype)("%p => %s %lu %lx\n",
485 sym, sym_info->Name, sym_info->Size, sym_info->Address);
488 static BOOL symt_enum_module(struct module* module, const char* mask,
489 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
491 char buffer[sizeof(SYMBOL_INFO) + 256];
492 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
494 struct symt_ht* sym = NULL;
495 struct hash_table_iter hti;
499 assert(mask[0] != '!');
500 compile_regex(mask, &preg);
501 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
502 while ((ptr = hash_table_iter_up(&hti)))
504 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
505 /* FIXME: this is not true, we should only drop the public
506 * symbol iff no other one is found
508 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
509 sym->symt.tag == SymTagPublicSymbol) continue;
511 if (sym->hash_elt.name &&
512 regexec(&preg, sym->hash_elt.name, 0, NULL, 0) == 0)
514 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
515 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
516 symt_fill_sym_info(module, &sym->symt, sym_info);
517 if (!cb(sym_info, sym_info->Size, user)) break;
521 return sym ? FALSE : TRUE;
524 /***********************************************************************
527 * Rebuild sorted list of symbols for a module.
529 static BOOL resort_symbols(struct module* module)
534 struct hash_table_iter hti;
536 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
537 while ((ptr = hash_table_iter_up(&hti)))
540 if (!(module->module.NumSyms = nsym)) return FALSE;
542 if (module->addr_sorttab)
543 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
544 module->addr_sorttab,
545 nsym * sizeof(struct symt_ht*));
547 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
548 nsym * sizeof(struct symt_ht*));
549 if (!module->addr_sorttab) return FALSE;
552 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
553 while ((ptr = hash_table_iter_up(&hti)))
555 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
557 module->addr_sorttab[nsym++] = sym;
560 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
561 return module->sortlist_valid = TRUE;
564 /* assume addr is in module */
565 static int symt_find_nearest(struct module* module, DWORD addr)
569 if (!module->sortlist_valid && !resort_symbols(module)) return -1;
572 * Binary search to find closest symbol.
575 high = module->module.NumSyms;
577 while (high > low + 1)
579 mid = (high + low) / 2;
580 if (cmp_sorttab_addr(module, mid, addr) < 0)
585 if (low != high && high != module->module.NumSyms &&
586 cmp_sorttab_addr(module, high, addr) <= 0)
589 /* If found symbol is a public symbol, check if there are any other entries that
590 * might also have the same address, but would get better information
592 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
596 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref);
598 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
599 !cmp_sorttab_addr(module, low - 1, ref))
601 else if (low < module->module.NumSyms - 1 &&
602 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
603 !cmp_sorttab_addr(module, low + 1, ref))
610 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
611 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
612 PVOID user, SYMBOL_INFO* sym_info,
615 struct symt** plsym = NULL;
616 struct symt* lsym = NULL;
617 DWORD pc = pcs->ctx_frame.InstructionOffset;
619 while ((plsym = vector_iter_up(v, plsym)))
626 struct symt_block* block = (struct symt_block*)lsym;
627 if (pc < block->address || block->address + block->size <= pc)
629 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
630 sym_info, &block->vchildren))
635 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
637 symt_fill_sym_info(module, lsym, sym_info);
638 if (!cb(sym_info, sym_info->Size, user))
643 case SymTagFuncDebugStart:
644 case SymTagFuncDebugEnd:
647 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
654 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
655 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
658 struct module* module;
660 char buffer[sizeof(SYMBOL_INFO) + 256];
661 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
662 DWORD pc = pcs->ctx_frame.InstructionOffset;
665 sym_info->SizeOfStruct = sizeof(*sym_info);
666 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
668 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
669 if (!(module = module_get_debug(pcs, module))) return FALSE;
670 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
672 sym = module->addr_sorttab[idx];
673 if (sym->symt.tag == SymTagFunction)
678 compile_regex(mask ? mask : "*", &preg);
679 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
680 UserContext, sym_info,
681 &((struct symt_function*)sym)->vchildren);
686 symt_fill_sym_info(module, &sym->symt, sym_info);
687 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
690 /******************************************************************
691 * SymEnumSymbols (DBGHELP.@)
694 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG BaseOfDll, PCSTR Mask,
695 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
698 struct process* pcs = process_find_by_handle(hProcess);
699 struct module* module;
701 TRACE("(%p %08lx %s %p %p)\n",
702 hProcess, BaseOfDll, debugstr_a(Mask), EnumSymbolsCallback, UserContext);
704 if (!pcs) return FALSE;
708 if (Mask && Mask[0] == '!')
712 /* FIXME: is this really what's intended ??? */
713 for (module = pcs->lmodules; module; module = module->next)
715 if (module->module.SymType != SymNone &&
716 !symt_enum_module(module, "*", EnumSymbolsCallback, UserContext))
721 module = module_find_by_name(pcs, &Mask[1], DMT_UNKNOWN);
724 else return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
728 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
729 if (Mask && Mask[0] == '!')
732 strcmp(&Mask[1], module->module.ModuleName))
734 FIXME("Strange call mode\n");
739 else if (!Mask) Mask = "*";
741 if ((module = module_get_debug(pcs, module)))
742 symt_enum_module(module, Mask, EnumSymbolsCallback, UserContext);
749 PSYM_ENUMSYMBOLS_CALLBACK cb;
752 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
754 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
755 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
758 /***********************************************************************
759 * SymEnumerateSymbols (DBGHELP.@)
761 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
762 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
765 struct sym_enumerate se;
767 se.ctx = UserContext;
768 se.cb = EnumSymbolsCallback;
770 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
773 /******************************************************************
774 * SymFromAddr (DBGHELP.@)
777 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD Address,
778 DWORD* Displacement, PSYMBOL_INFO Symbol)
780 struct process* pcs = process_find_by_handle(hProcess);
781 struct module* module;
785 if (!pcs) return FALSE;
786 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
787 if (!(module = module_get_debug(pcs, module))) return FALSE;
788 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
790 sym = module->addr_sorttab[idx];
792 symt_fill_sym_info(module, &sym->symt, Symbol);
793 if (Displacement) *Displacement = Address - Symbol->Address;
797 /******************************************************************
798 * SymGetSymFromAddr (DBGHELP.@)
801 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
802 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
804 char buffer[sizeof(SYMBOL_INFO) + 256];
805 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
808 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
809 si->SizeOfStruct = sizeof(*si);
810 si->MaxNameLen = 256;
811 if (!SymFromAddr(hProcess, Address, Displacement, si))
814 Symbol->Address = si->Address;
815 Symbol->Size = si->Size;
816 Symbol->Flags = si->Flags;
817 len = min(Symbol->MaxNameLength, si->MaxNameLen);
818 strncpy(Symbol->Name, si->Name, len);
819 Symbol->Name[len - 1] = '\0';
823 /******************************************************************
824 * SymFromName (DBGHELP.@)
827 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
829 struct process* pcs = process_find_by_handle(hProcess);
830 struct module* module;
831 struct hash_table_iter hti;
833 struct symt_ht* sym = NULL;
835 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
836 if (!pcs) return FALSE;
837 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
838 for (module = pcs->lmodules; module; module = module->next)
840 if (module->module.SymType != SymNone)
842 if (module->module.SymType == SymDeferred)
844 struct module* xmodule = module_get_debug(pcs, module);
845 if (!xmodule) continue;
848 hash_table_iter_init(&module->ht_symbols, &hti, Name);
849 while ((ptr = hash_table_iter_up(&hti)))
851 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
853 if (!strcmp(sym->hash_elt.name, Name))
855 symt_fill_sym_info(module, &sym->symt, Symbol);
864 /***********************************************************************
865 * SymGetSymFromName (DBGHELP.@)
867 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
869 char buffer[sizeof(SYMBOL_INFO) + 256];
870 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
873 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
874 si->SizeOfStruct = sizeof(*si);
875 si->MaxNameLen = 256;
876 if (!SymFromName(hProcess, Name, si)) return FALSE;
878 Symbol->Address = si->Address;
879 Symbol->Size = si->Size;
880 Symbol->Flags = si->Flags;
881 len = min(Symbol->MaxNameLength, si->MaxNameLen);
882 strncpy(Symbol->Name, si->Name, len);
883 Symbol->Name[len - 1] = '\0';
887 /******************************************************************
888 * sym_fill_func_line_info
890 * fills information about a file
892 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
893 DWORD addr, IMAGEHLP_LINE* line)
895 struct line_info* dli = NULL;
898 assert(func->symt.tag == SymTagFunction);
900 while ((dli = vector_iter_down(&func->vlines, dli)))
902 if (!(dli->cookie & DLIT_SOURCEFILE))
904 if (found || dli->u.pc_offset > addr) continue;
905 line->LineNumber = dli->line_number;
906 line->Address = dli->u.pc_offset;
913 line->FileName = (char*)source_get(module, dli->u.source_file);
920 /***********************************************************************
921 * SymGetSymNext (DBGHELP.@)
923 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
926 * get module from Symbol.Address
927 * get index in module.addr_sorttab of Symbol.Address
929 * if out of module bounds, move to next module in process address space
931 FIXME("(%p, %p): stub\n", hProcess, Symbol);
932 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
936 /***********************************************************************
937 * SymGetSymPrev (DBGHELP.@)
940 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
942 FIXME("(%p, %p): stub\n", hProcess, Symbol);
943 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
947 /******************************************************************
948 * SymGetLineFromAddr (DBGHELP.@)
951 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
952 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
954 struct process* pcs = process_find_by_handle(hProcess);
955 struct module* module;
958 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
960 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
962 if (!pcs) return FALSE;
963 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
964 if (!(module = module_get_debug(pcs, module))) return FALSE;
965 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
967 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
968 if (!symt_fill_func_line_info(module,
969 (struct symt_function*)module->addr_sorttab[idx],
970 dwAddr, Line)) return FALSE;
971 if (pdwDisplacement) *pdwDisplacement = dwAddr - Line->Address;
975 /******************************************************************
976 * SymGetLinePrev (DBGHELP.@)
979 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
981 struct process* pcs = process_find_by_handle(hProcess);
982 struct module* module;
983 struct line_info* li;
984 BOOL in_search = FALSE;
986 TRACE("(%p %p)\n", hProcess, Line);
988 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
990 if (!pcs) return FALSE;
991 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
992 if (!(module = module_get_debug(pcs, module))) return FALSE;
994 if (Line->Key == 0) return FALSE;
995 li = (struct line_info*)Line->Key;
996 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
997 * element we have to go back until we find the prev one to get the real
998 * source file name for the DLIT_OFFSET element just before
999 * the first DLIT_SOURCEFILE
1001 while (!(li->cookie & DLIT_FIRST))
1004 if (!(li->cookie & DLIT_SOURCEFILE))
1006 Line->LineNumber = li->line_number;
1007 Line->Address = li->u.pc_offset;
1009 if (!in_search) return TRUE;
1015 Line->FileName = (char*)source_get(module, li->u.source_file);
1021 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1025 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1027 struct line_info* li;
1029 if (line->Key == 0) return FALSE;
1030 li = (struct line_info*)line->Key;
1031 while (!(li->cookie & DLIT_LAST))
1034 if (!(li->cookie & DLIT_SOURCEFILE))
1036 line->LineNumber = li->line_number;
1037 line->Address = li->u.pc_offset;
1041 line->FileName = (char*)source_get(module, li->u.source_file);
1046 /******************************************************************
1047 * SymGetLineNext (DBGHELP.@)
1050 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1052 struct process* pcs = process_find_by_handle(hProcess);
1053 struct module* module;
1055 TRACE("(%p %p)\n", hProcess, Line);
1057 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1058 if (!pcs) return FALSE;
1059 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1060 if (!(module = module_get_debug(pcs, module))) return FALSE;
1062 if (symt_get_func_line_next(module, Line)) return TRUE;
1063 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1067 /***********************************************************************
1068 * SymFunctionTableAccess (DBGHELP.@)
1070 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1072 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1073 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1077 /***********************************************************************
1078 * SymUnDName (DBGHELP.@)
1080 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1082 FIXME("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1083 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1087 /***********************************************************************
1088 * UnDecorateSymbolName (DBGHELP.@)
1090 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1091 DWORD UndecoratedLength, DWORD Flags)
1093 FIXME("(%s, %p, %ld, 0x%08lx): stub\n",
1094 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1096 strncpy(UnDecoratedName, DecoratedName, UndecoratedLength);
1097 UnDecoratedName[UndecoratedLength - 1] = '\0';