2 * Generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004-2005, Eric Pouech.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static BOOL symbol_get_debug_start(const struct dbg_type* func, ULONG64* start)
38 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
39 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
41 struct dbg_type child;
43 if (!func->id) return FALSE; /* native dbghelp not always fills the info field */
45 if (!types_get_info(func, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
49 fcp->Count = min(count, 256);
50 if (types_get_info(func, TI_FINDCHILDREN, fcp))
52 for (i = 0; i < min(fcp->Count, count); i++)
54 child.module = func->module;
55 child.id = fcp->ChildId[i];
56 types_get_info(&child, TI_GET_SYMTAG, &tag);
57 if (tag != SymTagFuncDebugStart) continue;
58 return types_get_info(&child, TI_GET_ADDRESS, start);
60 count -= min(count, 256);
68 static BOOL fill_sym_lvalue(const SYMBOL_INFO* sym, ULONG base,
69 struct dbg_lvalue* lvalue, char* buffer, size_t sz)
71 if (buffer) buffer[0] = '\0';
72 if (sym->Flags & SYMFLAG_REGISTER)
76 if (!memory_get_register(sym->Register, &pval, buffer, sz))
78 lvalue->cookie = DLV_HOST;
79 lvalue->addr.Offset = (DWORD_PTR)pval;
81 else if (sym->Flags & SYMFLAG_REGREL)
85 if (!memory_get_register(sym->Register, &pval, buffer, sz))
87 lvalue->cookie = DLV_TARGET;
88 lvalue->addr.Offset = (ULONG)((ULONG64)*pval + sym->Address);
90 else if (sym->Flags & SYMFLAG_LOCAL)
92 lvalue->cookie = DLV_TARGET;
93 lvalue->addr.Offset = base + sym->Address;
97 lvalue->cookie = DLV_TARGET;
98 lvalue->addr.Offset = sym->Address;
100 lvalue->addr.Mode = AddrModeFlat;
101 lvalue->type.module = sym->ModBase;
102 lvalue->type.id = sym->TypeIndex;
112 /* FIXME: NUMDBGV should be made variable */
113 struct dbg_lvalue lvalue;
116 } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
117 int num; /* out : number of found symbols */
118 int num_thunks; /* out : number of thunks found */
119 const char* name; /* in : name of symbol to look up */
120 unsigned do_thunks : 1; /* in : whether we return thunks tags */
121 ULONG64 frame_offset; /* in : frame for local & parameter variables look up */
124 static BOOL CALLBACK sgv_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
126 struct sgv_data* sgv = (struct sgv_data*)ctx;
130 if (sym->Flags & SYMFLAG_THUNK)
132 if (!sgv->do_thunks) return TRUE;
136 if (sgv->num >= NUMDBGV)
138 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
142 WINE_TRACE("==> %s %s%s%s%s%s%s%s\n",
144 (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
145 (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
146 (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
147 (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
148 (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
149 (sym->Flags & SYMFLAG_LOCAL) ? "local " : "",
150 (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
152 /* always keep the thunks at end of the array */
154 if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
156 insp -= sgv->num_thunks;
157 memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
158 sizeof(sgv->syms[0]) * sgv->num_thunks);
160 if (!fill_sym_lvalue(sym, sgv->frame_offset, &sgv->syms[insp].lvalue, tmp, sizeof(tmp)))
162 dbg_printf("%s: %s\n", sym->Name, tmp);
165 sgv->syms[insp].flags = sym->Flags;
166 sgv->syms[insp].sym_info = sym->info;
172 /***********************************************************************
175 * Get the address of a named symbol.
177 * sglv_found: if the symbol is found
178 * sglv_unknown: if the symbol isn't found
179 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
180 * and user didn't pick one of them)
182 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
183 struct dbg_lvalue* rtn, BOOL bp_disp)
189 IMAGEHLP_STACK_FRAME ihsf;
191 if (strlen(name) + 4 > sizeof(buffer))
193 WINE_WARN("Too long symbol (%s)\n", name);
199 sgv.name = &buffer[2];
200 sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
202 if (strchr(name, '!'))
204 strcpy(buffer, name);
210 strcpy(&buffer[2], name);
213 /* this is a wine specific options to return also ELF modules in the
216 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
217 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv);
219 if (!sgv.num && (name[0] != '_'))
221 char* ptr = strchr(name, '!');
225 memmove(ptr + 1, ptr, strlen(ptr));
233 strcpy(&buffer[3], name);
235 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv);
239 /* now grab local symbols */
240 if (stack_get_current_frame(&ihsf) && sgv.num < NUMDBGV)
242 sgv.frame_offset = ihsf.FrameOffset;
243 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
248 dbg_printf("No symbols found for %s\n", name);
252 /* recompute potential offsets for functions (linenumber, skip prolog) */
253 for (i = 0; i < sgv.num; i++)
255 if (sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL|SYMFLAG_LOCAL|SYMFLAG_THUNK))
260 struct dbg_type type;
263 type.module = sgv.syms[i].lvalue.type.module;
264 type.id = sgv.syms[i].sym_info;
265 if (bp_disp && symbol_get_debug_start(&type, &addr))
266 sgv.syms[i].lvalue.addr.Offset = addr;
274 il.SizeOfStruct = sizeof(il);
275 SymGetLineFromAddr(dbg_curr_process->handle,
276 (DWORD)memory_to_linear_addr(&sgv.syms[i].lvalue.addr),
280 if (lineno == il.LineNumber)
282 sgv.syms[i].lvalue.addr.Offset = il.Address;
286 } while (SymGetLineNext(dbg_curr_process->handle, &il));
288 WINE_FIXME("No line (%d) found for %s (setting to symbol start)\n",
294 if (dbg_interactiveP)
296 if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
297 (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
298 (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
300 dbg_printf("Many symbols with name '%s', "
301 "choose the one you want (<cr> to abort):\n", name);
302 for (i = 0; i < sgv.num; i++)
304 if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
306 dbg_printf("[%d]: ", i + 1);
307 if (sgv.syms[i].flags & SYMFLAG_LOCAL)
309 dbg_printf("%s %sof %s\n",
310 sgv.syms[i].flags & SYMFLAG_PARAMETER ? "Parameter" : "Local variable",
311 sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL) ? "(in a register) " : "",
314 else if (sgv.syms[i].flags & SYMFLAG_THUNK)
316 print_address(&sgv.syms[i].lvalue.addr, TRUE);
317 /* FIXME: should display where the thunks points to */
318 dbg_printf(" thunk %s\n", name);
322 print_address(&sgv.syms[i].lvalue.addr, TRUE);
329 if (input_read_line("=> ", buffer, sizeof(buffer)))
331 if (buffer[0] == '\0') return sglv_aborted;
333 if (i < 1 || i > sgv.num)
334 dbg_printf("Invalid choice %d\n", i);
336 } while (i < 1 || i > sgv.num);
338 /* The array is 0-based, but the choices are 1..n,
339 * so we have to subtract one before returning.
346 /* FIXME: could display the list of non-picked up symbols */
348 dbg_printf("More than one symbol named %s, picking the first one\n", name);
350 *rtn = sgv.syms[i].lvalue;
354 BOOL symbol_is_local(const char* name)
357 IMAGEHLP_STACK_FRAME ihsf;
362 sgv.do_thunks = FALSE;
364 if (stack_get_current_frame(&ihsf))
366 sgv.frame_offset = ihsf.FrameOffset;
367 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
372 /***********************************************************************
373 * symbol_read_symtable
375 * Read a symbol file into the hash table.
377 void symbol_read_symtable(const char* filename, unsigned long offset)
379 dbg_printf("No longer supported\n");
382 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
383 * this with an already loaded module !!
392 if (!(symbolfile = fopen(filename, "r")))
394 WINE_WARN("Unable to open symbol table %s\n", filename);
398 dbg_printf("Reading symbols from file %s\n", filename);
402 fgets(buffer, sizeof(buffer), symbolfile);
403 if (feof(symbolfile)) break;
405 /* Strip any text after a # sign (i.e. comments) */
406 cpnt = strchr(buffer, '#');
407 if (cpnt) *cpnt = '\0';
409 /* Quietly ignore any lines that have just whitespace */
410 for (cpnt = buffer; *cpnt; cpnt++)
412 if (*cpnt != ' ' && *cpnt != '\t') break;
414 if (!*cpnt || *cpnt == '\n') continue;
416 if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
418 if (value.addr.off + offset < value.addr.off)
419 WINE_WARN("Address wrap around\n");
420 value.addr.off += offset;
421 SymAddSymbol(current_process->handle, BaseOfDll,
429 /***********************************************************************
430 * symbol_get_function_line_status
432 * Find the symbol nearest to a given address.
434 enum dbg_line_status symbol_get_function_line_status(const ADDRESS64* addr)
438 ULONG64 disp64, start;
439 DWORD lin = (DWORD)memory_to_linear_addr(addr);
440 char buffer[sizeof(SYMBOL_INFO) + 256];
441 SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
442 struct dbg_type func;
444 il.SizeOfStruct = sizeof(il);
445 sym->SizeOfStruct = sizeof(SYMBOL_INFO);
446 sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
448 /* do we have some info for lin address ? */
449 if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
450 return dbg_no_line_info;
455 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
456 * and furthermore, we no longer take care of them !!!
458 return dbg_in_a_thunk;
460 case SymTagPublicSymbol: break;
462 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym->Tag);
464 return dbg_no_line_info;
466 /* we should have a function now */
467 if (!SymGetLineFromAddr(dbg_curr_process->handle, lin, &disp, &il))
468 return dbg_no_line_info;
470 func.module = sym->ModBase;
473 if (symbol_get_debug_start(&func, &start) && lin < start)
474 return dbg_not_on_a_line_number;
476 if (!sym->Size) sym->Size = 0x100000;
477 if (il.FileName && il.FileName[0] && disp < sym->Size)
478 return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
480 return dbg_no_line_info;
483 /***********************************************************************
486 * Find the symbol nearest to a given address.
487 * Returns sourcefile name and line number in a format that the listing
488 * handler can deal with.
490 BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line)
494 DWORD opt, disp, linear;
495 unsigned i, found = FALSE;
500 sgv.name = &buffer[2];
501 sgv.do_thunks = FALSE;
505 strcpy(&buffer[2], name);
507 /* this is a wine specific options to return also ELF modules in the
510 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
511 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
517 if (!sgv.num && (name[0] != '_'))
520 strcpy(&buffer[3], name);
521 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
529 for (i = 0; i < sgv.num; i++)
531 linear = (DWORD)memory_to_linear_addr(&sgv.syms[i].lvalue.addr);
533 il.SizeOfStruct = sizeof(il);
534 if (!SymGetLineFromAddr(dbg_curr_process->handle, linear, &disp, &il))
536 if (filename && strcmp(line->FileName, filename)) continue;
539 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
547 if (filename) dbg_printf("No such function %s in %s\n", name, filename);
548 else dbg_printf("No such function %s\n", name);
554 /******************************************************************
558 * <name>=<value> in non detailed form
559 * <name>=<value> (local|pmt <where>) in detailed form
560 * Note <value> can be an error message in case of error
562 void symbol_print_local(const SYMBOL_INFO* sym, ULONG base,
565 struct dbg_lvalue lvalue;
568 dbg_printf("%s=", sym->Name);
570 if (fill_sym_lvalue(sym, base, &lvalue, buffer, sizeof(buffer)))
572 print_value(&lvalue, 'x', 1);
574 dbg_printf(" (%s%s)",
575 (sym->Flags & SYMFLAG_PARAMETER) ? "parameter" : "local",
583 (sym->Flags & SYMFLAG_PARAMETER) ? "parameter" : "local");
587 static BOOL CALLBACK info_locals_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
589 struct dbg_type type;
592 type.module = sym->ModBase;
593 type.id = sym->TypeIndex;
594 types_print_type(&type, FALSE);
597 symbol_print_local(sym, (ULONG)ctx, TRUE);
603 int symbol_info_locals(void)
605 IMAGEHLP_STACK_FRAME ihsf;
608 stack_get_current_frame(&ihsf);
609 addr.Mode = AddrModeFlat;
610 addr.Offset = ihsf.InstructionOffset;
611 print_address(&addr, FALSE);
612 dbg_printf(": (%08lx)\n", (DWORD_PTR)ihsf.FrameOffset);
613 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, (void*)(DWORD_PTR)ihsf.FrameOffset);
619 static BOOL CALLBACK symbols_info_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
621 struct dbg_type type;
624 mi.SizeOfStruct = sizeof(mi);
626 if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
627 mi.ModuleName[0] = '\0';
630 size_t len = strlen(mi.ModuleName);
631 if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
632 mi.ModuleName[len - 5] = '\0';
635 dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
636 type.id = sym->TypeIndex;
637 type.module = sym->ModBase;
639 if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0)
642 types_print_type(&type, FALSE);
648 void symbol_info(const char* str)
653 if (strlen(str) + 3 >= sizeof(buffer))
655 dbg_printf("Symbol too long (%s)\n", str);
660 strcpy(&buffer[2], str);
661 /* this is a wine specific options to return also ELF modules in the
664 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
665 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);