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_symt);
42 unsigned long is_first : 1,
48 unsigned long pc_offset; /* if is_source_file isn't set */
49 unsigned source_file; /* if is_source_file is set */
53 inline static int cmp_addr(DWORD a1, DWORD a2)
55 if (a1 > a2) return 1;
56 if (a1 < a2) return -1;
60 inline static int cmp_sorttab_addr(const struct module* module, int idx, DWORD addr)
64 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
65 return cmp_addr(ref, addr);
68 int symt_cmp_addr(const void* p1, const void* p2)
70 struct symt* sym1 = *(struct symt**)p1;
71 struct symt* sym2 = *(struct symt**)p2;
74 symt_get_info(sym1, TI_GET_ADDRESS, &a1);
75 symt_get_info(sym2, TI_GET_ADDRESS, &a2);
76 return cmp_addr(a1, a2);
79 static inline void re_append(char** mask, unsigned* len, char ch)
81 *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
82 (*mask)[*len - 2] = ch;
85 /* transforms a dbghelp's regular expression into a POSIX one
86 * Here are the valid dbghelp reg ex characters:
87 * * 0 or more characters
88 * ? a single character
90 * # 0 or more of preceding char
91 * + 1 or more of preceding char
92 * escapes \ on #, ?, [, ], *, +. don't work on -
94 static void compile_regex(const char* str, regex_t* re)
96 char* mask = HeapAlloc(GetProcessHeap(), 0, 1);
98 BOOL in_escape = FALSE;
100 re_append(&mask, &len, '^');
103 /* FIXME: this shouldn't be valid on '-' */
106 re_append(&mask, &len, '\\');
107 re_append(&mask, &len, *str);
112 case '\\': in_escape = TRUE; break;
113 case '*': re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
114 case '?': re_append(&mask, &len, '.'); break;
115 case '#': re_append(&mask, &len, '*'); break;
116 /* escape some valid characters in dbghelp reg exp:s */
117 case '$': re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
118 /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
119 default: re_append(&mask, &len, *str); break;
125 re_append(&mask, &len, '\\');
126 re_append(&mask, &len, '\\');
128 re_append(&mask, &len, '$');
129 mask[len - 1] = '\0';
130 regcomp(re, mask, REG_NOSUB);
131 HeapFree(GetProcessHeap(), 0, mask);
134 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
136 struct symt_compiland* sym;
138 TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
139 module->module.ModuleName, name);
140 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
142 sym->symt.tag = SymTagCompiland;
143 sym->source = source_new(module, name);
144 vector_init(&sym->vchildren, sizeof(struct symt*), 32);
149 struct symt_public* symt_new_public(struct module* module,
150 struct symt_compiland* compiland,
152 unsigned long address, unsigned size,
153 BOOL in_code, BOOL is_func)
155 struct symt_public* sym;
158 TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
159 module->module.ModuleName, name, address);
160 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
162 sym->symt.tag = SymTagPublicSymbol;
163 sym->hash_elt.name = pool_strdup(&module->pool, name);
164 hash_table_add(&module->ht_symbols, &sym->hash_elt);
165 module->sortlist_valid = FALSE;
166 sym->container = compiland ? &compiland->symt : NULL;
167 sym->address = address;
169 sym->in_code = in_code;
170 sym->is_function = is_func;
173 p = vector_add(&compiland->vchildren, &module->pool);
180 struct symt_data* symt_new_global_variable(struct module* module,
181 struct symt_compiland* compiland,
182 const char* name, unsigned is_static,
183 unsigned long addr, unsigned long size,
186 struct symt_data* sym;
189 TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
190 module->module.ModuleName, name, addr, type);
191 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
193 sym->symt.tag = SymTagData;
194 sym->hash_elt.name = pool_strdup(&module->pool, name);
195 hash_table_add(&module->ht_symbols, &sym->hash_elt);
196 module->sortlist_valid = FALSE;
197 sym->kind = is_static ? DataIsFileStatic : DataIsGlobal;
198 sym->container = compiland ? &compiland->symt : NULL;
200 sym->u.address = addr;
203 p = vector_add(&compiland->vchildren, &module->pool);
210 struct symt_function* symt_new_function(struct module* module,
211 struct symt_compiland* compiland,
213 unsigned long addr, unsigned long size,
214 struct symt* sig_type)
216 struct symt_function* sym;
219 TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
220 module->module.ModuleName, name, addr, addr + size - 1);
222 assert(!sig_type || sig_type->tag == SymTagFunctionType);
223 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
225 sym->symt.tag = SymTagFunction;
226 sym->hash_elt.name = pool_strdup(&module->pool, name);
227 hash_table_add(&module->ht_symbols, &sym->hash_elt);
228 module->sortlist_valid = FALSE;
229 sym->container = &compiland->symt;
231 sym->type = sig_type;
234 vector_init(&sym->vlines, sizeof(struct line_info), 64);
235 vector_init(&sym->vchildren, sizeof(struct symt*), 8);
238 p = vector_add(&compiland->vchildren, &module->pool);
245 void symt_add_func_line(struct module* module, struct symt_function* func,
246 unsigned source_idx, int line_num, unsigned long offset)
248 struct line_info* dli;
249 BOOL last_matches = FALSE;
251 if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
253 TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n",
254 func, func->hash_elt.name, offset,
255 source_get(module, source_idx), line_num);
257 assert(func->symt.tag == SymTagFunction);
260 while ((dli = vector_iter_down(&func->vlines, dli)))
262 if (dli->is_source_file)
264 last_matches = (source_idx == dli->u.source_file);
271 /* we shouldn't have line changes on first line of function */
272 dli = vector_add(&func->vlines, &module->pool);
273 dli->is_source_file = 1;
274 dli->is_first = dli->is_last = 0;
275 dli->line_number = 0;
276 dli->u.source_file = source_idx;
278 dli = vector_add(&func->vlines, &module->pool);
279 dli->is_source_file = 0;
280 dli->is_first = dli->is_last = 0;
281 dli->line_number = line_num;
282 dli->u.pc_offset = func->addr + offset;
285 struct symt_data* symt_add_func_local(struct module* module,
286 struct symt_function* func,
287 int regno, int offset,
288 struct symt_block* block,
289 struct symt* type, const char* name)
291 struct symt_data* locsym;
295 assert(func->symt.tag == SymTagFunction);
297 TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
298 module->module.ModuleName, func->hash_elt.name,
300 locsym = pool_alloc(&module->pool, sizeof(*locsym));
301 locsym->symt.tag = SymTagData;
302 locsym->hash_elt.name = pool_strdup(&module->pool, name);
303 locsym->hash_elt.next = NULL;
304 locsym->kind = (offset < 0) ? DataIsParam : DataIsLocal;
305 locsym->container = &block->symt;
309 locsym->u.s.reg_id = regno;
310 locsym->u.s.offset = 0;
311 locsym->u.s.length = 0;
315 locsym->u.s.reg_id = 0;
316 locsym->u.s.offset = offset * 8;
317 locsym->u.s.length = 0;
320 p = vector_add(&block->vchildren, &module->pool);
322 p = vector_add(&func->vchildren, &module->pool);
327 struct symt_block* symt_open_func_block(struct module* module,
328 struct symt_function* func,
329 struct symt_block* parent_block,
330 unsigned pc, unsigned len)
332 struct symt_block* block;
336 assert(func->symt.tag == SymTagFunction);
338 assert(!parent_block || parent_block->symt.tag == SymTagBlock);
339 block = pool_alloc(&module->pool, sizeof(*block));
340 block->symt.tag = SymTagBlock;
341 block->address = func->addr + pc;
343 block->container = parent_block ? &parent_block->symt : &func->symt;
344 vector_init(&block->vchildren, sizeof(struct symt*), 4);
346 p = vector_add(&parent_block->vchildren, &module->pool);
348 p = vector_add(&func->vchildren, &module->pool);
354 struct symt_block* symt_close_func_block(struct module* module,
355 struct symt_function* func,
356 struct symt_block* block, unsigned pc)
358 assert(func->symt.tag == SymTagFunction);
360 if (pc) block->size = func->addr + pc - block->address;
361 return (block->container->tag == SymTagBlock) ?
362 GET_ENTRY(block->container, struct symt_block, symt) : NULL;
365 struct symt_function_point* symt_add_function_point(struct module* module,
366 struct symt_function* func,
367 enum SymTagEnum point,
368 unsigned offset, const char* name)
370 struct symt_function_point* sym;
373 if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
375 sym->symt.tag = point;
377 sym->offset = offset;
378 sym->name = name ? pool_strdup(&module->pool, name) : NULL;
379 p = vector_add(&func->vchildren, &module->pool);
385 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
388 struct line_info* dli;
391 /* We aren't adding any more locals or line numbers to this function.
392 * Free any spare memory that we might have allocated.
394 assert(func->symt.tag == SymTagFunction);
396 /* EPP vector_pool_normalize(&func->vlines, &module->pool); */
397 /* EPP vector_pool_normalize(&func->vchildren, &module->pool); */
399 len = vector_length(&func->vlines);
402 dli = vector_at(&func->vlines, 0); dli->is_first = 1;
403 dli = vector_at(&func->vlines, len); dli->is_last = 1;
408 /* expect sym_info->MaxNameLen to be set before being called */
409 static void symt_fill_sym_info(const struct module* module,
410 const struct symt* sym, SYMBOL_INFO* sym_info)
414 sym_info->TypeIndex = (DWORD)sym;
415 sym_info->info = 0; /* TBD */
416 symt_get_info(sym, TI_GET_LENGTH, &sym_info->Size);
417 sym_info->ModBase = module->module.BaseOfImage;
423 struct symt_data* data = (struct symt_data*)sym;
428 if (data->u.s.reg_id)
430 sym_info->Flags |= SYMFLAG_REGISTER;
431 sym_info->Register = data->u.s.reg_id;
432 sym_info->Address = 0;
436 if (data->u.s.offset < 0)
437 sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_FRAMEREL;
439 sym_info->Flags |= SYMFLAG_PARAMETER | SYMFLAG_FRAMEREL;
440 sym_info->Register = CV_REG_EBP; /* FIXME: needed ? */
441 sym_info->Address = data->u.s.offset;
445 case DataIsFileStatic:
446 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
447 sym_info->Register = 0;
450 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
451 switch (data->u.value.n1.n2.vt)
453 case VT_I4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
454 case VT_I2: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
455 case VT_I1: sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
456 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
457 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
458 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
460 FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
464 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
468 case SymTagPublicSymbol:
469 sym_info->Flags |= SYMFLAG_EXPORT;
470 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
473 sym_info->Flags |= SYMFLAG_FUNCTION;
474 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
477 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
478 sym_info->Register = 0;
481 sym_info->Scope = 0; /* FIXME */
482 sym_info->Tag = sym->tag;
483 name = symt_get_name(sym);
484 sym_info->NameLen = strlen(name) + 1;
485 if (sym_info->MaxNameLen)
487 strncpy(sym_info->Name, name, min(sym_info->NameLen, sym_info->MaxNameLen));
488 sym_info->Name[sym_info->MaxNameLen - 1] = '\0';
490 TRACE_(dbghelp_symt)("%p => %s %lu %lx\n",
491 sym, sym_info->Name, sym_info->Size, sym_info->Address);
494 static BOOL symt_enum_module(struct module* module, const char* mask,
495 PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
497 char buffer[sizeof(SYMBOL_INFO) + 256];
498 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
500 struct symt_ht* sym = NULL;
501 struct hash_table_iter hti;
505 assert(mask[0] != '!');
506 compile_regex(mask, &preg);
507 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
508 while ((ptr = hash_table_iter_up(&hti)))
510 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
511 /* FIXME: this is not true, we should only drop the public
512 * symbol iff no other one is found
514 if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
515 sym->symt.tag == SymTagPublicSymbol) continue;
517 if (sym->hash_elt.name &&
518 regexec(&preg, sym->hash_elt.name, 0, NULL, 0) == 0)
520 sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
521 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
522 symt_fill_sym_info(module, &sym->symt, sym_info);
523 if (!cb(sym_info, sym_info->Size, user)) break;
527 return sym ? FALSE : TRUE;
530 /***********************************************************************
533 * Rebuild sorted list of symbols for a module.
535 static BOOL resort_symbols(struct module* module)
540 struct hash_table_iter hti;
542 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
543 while ((ptr = hash_table_iter_up(&hti)))
546 if (!(module->module.NumSyms = nsym)) return FALSE;
548 if (module->addr_sorttab)
549 module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
550 module->addr_sorttab,
551 nsym * sizeof(struct symt_ht*));
553 module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
554 nsym * sizeof(struct symt_ht*));
555 if (!module->addr_sorttab) return FALSE;
558 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
559 while ((ptr = hash_table_iter_up(&hti)))
561 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
563 module->addr_sorttab[nsym++] = sym;
566 qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
567 return module->sortlist_valid = TRUE;
570 /* assume addr is in module */
571 static int symt_find_nearest(struct module* module, DWORD addr)
575 if (!module->sortlist_valid && !resort_symbols(module)) return -1;
578 * Binary search to find closest symbol.
581 high = module->module.NumSyms;
583 while (high > low + 1)
585 mid = (high + low) / 2;
586 if (cmp_sorttab_addr(module, mid, addr) < 0)
591 if (low != high && high != module->module.NumSyms &&
592 cmp_sorttab_addr(module, high, addr) <= 0)
595 /* If found symbol is a public symbol, check if there are any other entries that
596 * might also have the same address, but would get better information
598 if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
602 symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref);
604 module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
605 !cmp_sorttab_addr(module, low - 1, ref))
607 else if (low < module->module.NumSyms - 1 &&
608 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
609 !cmp_sorttab_addr(module, low + 1, ref))
616 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
617 regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
618 PVOID user, SYMBOL_INFO* sym_info,
621 struct symt** plsym = NULL;
622 struct symt* lsym = NULL;
623 DWORD pc = pcs->ctx_frame.InstructionOffset;
625 while ((plsym = vector_iter_up(v, plsym)))
632 struct symt_block* block = (struct symt_block*)lsym;
633 if (pc < block->address || block->address + block->size <= pc)
635 if (!symt_enum_locals_helper(pcs, module, preg, cb, user,
636 sym_info, &block->vchildren))
641 if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
643 symt_fill_sym_info(module, lsym, sym_info);
644 if (!cb(sym_info, sym_info->Size, user))
649 case SymTagFuncDebugStart:
650 case SymTagFuncDebugEnd:
653 FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
660 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
661 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
664 struct module* module;
666 char buffer[sizeof(SYMBOL_INFO) + 256];
667 SYMBOL_INFO* sym_info = (SYMBOL_INFO*)buffer;
668 DWORD pc = pcs->ctx_frame.InstructionOffset;
671 sym_info->SizeOfStruct = sizeof(*sym_info);
672 sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
674 module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
675 if (!(module = module_get_debug(pcs, module))) return FALSE;
676 if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
678 sym = module->addr_sorttab[idx];
679 if (sym->symt.tag == SymTagFunction)
684 compile_regex(mask ? mask : "*", &preg);
685 ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback,
686 UserContext, sym_info,
687 &((struct symt_function*)sym)->vchildren);
692 symt_fill_sym_info(module, &sym->symt, sym_info);
693 return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
696 /******************************************************************
697 * SymEnumSymbols (DBGHELP.@)
700 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG BaseOfDll, PCSTR Mask,
701 PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
704 struct process* pcs = process_find_by_handle(hProcess);
705 struct module* module;
707 TRACE("(%p %08lx %s %p %p)\n",
708 hProcess, BaseOfDll, debugstr_a(Mask), EnumSymbolsCallback, UserContext);
710 if (!pcs) return FALSE;
714 if (Mask && Mask[0] == '!')
718 /* FIXME: is this really what's intended ??? */
719 for (module = pcs->lmodules; module; module = module->next)
721 if (module->module.SymType != SymNone &&
722 !symt_enum_module(module, "*", EnumSymbolsCallback, UserContext))
727 module = module_find_by_name(pcs, &Mask[1], DMT_UNKNOWN);
730 else return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
734 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
735 if (Mask && Mask[0] == '!')
738 strcmp(&Mask[1], module->module.ModuleName))
740 FIXME("Strange call mode\n");
745 else if (!Mask) Mask = "*";
747 if ((module = module_get_debug(pcs, module)))
748 symt_enum_module(module, Mask, EnumSymbolsCallback, UserContext);
755 PSYM_ENUMSYMBOLS_CALLBACK cb;
758 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
760 struct sym_enumerate* se = (struct sym_enumerate*)ctx;
761 return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
764 /***********************************************************************
765 * SymEnumerateSymbols (DBGHELP.@)
767 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
768 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,
771 struct sym_enumerate se;
773 se.ctx = UserContext;
774 se.cb = EnumSymbolsCallback;
776 return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
779 /******************************************************************
780 * SymFromAddr (DBGHELP.@)
783 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD Address,
784 DWORD* Displacement, PSYMBOL_INFO Symbol)
786 struct process* pcs = process_find_by_handle(hProcess);
787 struct module* module;
791 if (!pcs) return FALSE;
792 module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
793 if (!(module = module_get_debug(pcs, module))) return FALSE;
794 if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
796 sym = module->addr_sorttab[idx];
798 symt_fill_sym_info(module, &sym->symt, Symbol);
799 if (Displacement) *Displacement = Address - Symbol->Address;
803 /******************************************************************
804 * SymGetSymFromAddr (DBGHELP.@)
807 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
808 PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
810 char buffer[sizeof(SYMBOL_INFO) + 256];
811 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
814 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
815 si->SizeOfStruct = sizeof(*si);
816 si->MaxNameLen = 256;
817 if (!SymFromAddr(hProcess, Address, Displacement, si))
820 Symbol->Address = si->Address;
821 Symbol->Size = si->Size;
822 Symbol->Flags = si->Flags;
823 len = min(Symbol->MaxNameLength, si->MaxNameLen);
824 strncpy(Symbol->Name, si->Name, len);
825 Symbol->Name[len - 1] = '\0';
829 /******************************************************************
830 * SymFromName (DBGHELP.@)
833 BOOL WINAPI SymFromName(HANDLE hProcess, LPSTR Name, PSYMBOL_INFO Symbol)
835 struct process* pcs = process_find_by_handle(hProcess);
836 struct module* module;
837 struct hash_table_iter hti;
839 struct symt_ht* sym = NULL;
841 TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
842 if (!pcs) return FALSE;
843 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
844 for (module = pcs->lmodules; module; module = module->next)
846 if (module->module.SymType != SymNone)
848 if (module->module.SymType == SymDeferred)
850 struct module* xmodule = module_get_debug(pcs, module);
851 if (!xmodule) continue;
854 hash_table_iter_init(&module->ht_symbols, &hti, Name);
855 while ((ptr = hash_table_iter_up(&hti)))
857 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
859 if (!strcmp(sym->hash_elt.name, Name))
861 symt_fill_sym_info(module, &sym->symt, Symbol);
870 /***********************************************************************
871 * SymGetSymFromName (DBGHELP.@)
873 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, LPSTR Name, PIMAGEHLP_SYMBOL Symbol)
875 char buffer[sizeof(SYMBOL_INFO) + 256];
876 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
879 if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
880 si->SizeOfStruct = sizeof(*si);
881 si->MaxNameLen = 256;
882 if (!SymFromName(hProcess, Name, si)) return FALSE;
884 Symbol->Address = si->Address;
885 Symbol->Size = si->Size;
886 Symbol->Flags = si->Flags;
887 len = min(Symbol->MaxNameLength, si->MaxNameLen);
888 strncpy(Symbol->Name, si->Name, len);
889 Symbol->Name[len - 1] = '\0';
893 /******************************************************************
894 * sym_fill_func_line_info
896 * fills information about a file
898 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func,
899 DWORD addr, IMAGEHLP_LINE* line)
901 struct line_info* dli = NULL;
904 assert(func->symt.tag == SymTagFunction);
906 while ((dli = vector_iter_down(&func->vlines, dli)))
908 if (!dli->is_source_file)
910 if (found || dli->u.pc_offset > addr) continue;
911 line->LineNumber = dli->line_number;
912 line->Address = dli->u.pc_offset;
919 line->FileName = (char*)source_get(module, dli->u.source_file);
926 /***********************************************************************
927 * SymGetSymNext (DBGHELP.@)
929 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
932 * get module from Symbol.Address
933 * get index in module.addr_sorttab of Symbol.Address
935 * if out of module bounds, move to next module in process address space
937 FIXME("(%p, %p): stub\n", hProcess, Symbol);
938 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
942 /***********************************************************************
943 * SymGetSymPrev (DBGHELP.@)
946 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
948 FIXME("(%p, %p): stub\n", hProcess, Symbol);
949 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
953 /******************************************************************
954 * SymGetLineFromAddr (DBGHELP.@)
957 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
958 PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
960 struct process* pcs = process_find_by_handle(hProcess);
961 struct module* module;
964 TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
966 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
968 if (!pcs) return FALSE;
969 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
970 if (!(module = module_get_debug(pcs, module))) return FALSE;
971 if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
973 if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
974 if (!symt_fill_func_line_info(module,
975 (struct symt_function*)module->addr_sorttab[idx],
976 dwAddr, Line)) return FALSE;
977 if (pdwDisplacement) *pdwDisplacement = dwAddr - Line->Address;
981 /******************************************************************
982 * SymGetLinePrev (DBGHELP.@)
985 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
987 struct process* pcs = process_find_by_handle(hProcess);
988 struct module* module;
989 struct line_info* li;
990 BOOL in_search = FALSE;
992 TRACE("(%p %p)\n", hProcess, Line);
994 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
996 if (!pcs) return FALSE;
997 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
998 if (!(module = module_get_debug(pcs, module))) return FALSE;
1000 if (Line->Key == 0) return FALSE;
1001 li = (struct line_info*)Line->Key;
1002 /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1003 * element we have to go back until we find the prev one to get the real
1004 * source file name for the DLIT_OFFSET element just before
1005 * the first DLIT_SOURCEFILE
1007 while (!li->is_first)
1010 if (!li->is_source_file)
1012 Line->LineNumber = li->line_number;
1013 Line->Address = li->u.pc_offset;
1015 if (!in_search) return TRUE;
1021 Line->FileName = (char*)source_get(module, li->u.source_file);
1027 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1031 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1033 struct line_info* li;
1035 if (line->Key == 0) return FALSE;
1036 li = (struct line_info*)line->Key;
1037 while (!li->is_last)
1040 if (!li->is_source_file)
1042 line->LineNumber = li->line_number;
1043 line->Address = li->u.pc_offset;
1047 line->FileName = (char*)source_get(module, li->u.source_file);
1052 /******************************************************************
1053 * SymGetLineNext (DBGHELP.@)
1056 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1058 struct process* pcs = process_find_by_handle(hProcess);
1059 struct module* module;
1061 TRACE("(%p %p)\n", hProcess, Line);
1063 if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1064 if (!pcs) return FALSE;
1065 module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1066 if (!(module = module_get_debug(pcs, module))) return FALSE;
1068 if (symt_get_func_line_next(module, Line)) return TRUE;
1069 SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1073 /***********************************************************************
1074 * SymFunctionTableAccess (DBGHELP.@)
1076 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1078 FIXME("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1079 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1083 /***********************************************************************
1084 * SymUnDName (DBGHELP.@)
1086 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1088 FIXME("(%p %s %lu): stub\n", sym, UnDecName, UnDecNameLength);
1089 return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1093 /***********************************************************************
1094 * UnDecorateSymbolName (DBGHELP.@)
1096 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1097 DWORD UndecoratedLength, DWORD Flags)
1099 FIXME("(%s, %p, %ld, 0x%08lx): stub\n",
1100 debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1102 strncpy(UnDecoratedName, DecoratedName, UndecoratedLength);
1103 UnDecoratedName[UndecoratedLength - 1] = '\0';