2 * Generate hash tables for Wine debugger symbols
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
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static BOOL symbol_get_debug_start(DWORD mod_base, DWORD typeid, ULONG64* start)
38 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
39 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
43 type.module = mod_base;
46 if (!types_get_info(&type, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
50 fcp->Count = min(count, 256);
51 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
53 for (i = 0; i < min(fcp->Count, count); i++)
55 type.id = fcp->ChildId[i];
56 types_get_info(&type, TI_GET_SYMTAG, &tag);
57 if (tag != SymTagFuncDebugStart) continue;
58 return types_get_info(&type, TI_GET_ADDRESS, start);
60 count -= min(count, 256);
71 /* FIXME: NUMDBGV should be made variable */
72 struct dbg_lvalue lvalue;
74 } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
75 int num; /* out : number of found symbols */
76 int num_thunks; /* out : number of thunks found */
77 const char* name; /* in : name of symbol to look up */
78 const char* filename; /* in (opt): filename where to look up symbol */
79 int lineno; /* in (opt): line number in filename where to look up symbol */
80 unsigned bp_disp : 1, /* in : whether if we take into account func address or func first displayable insn */
81 do_thunks : 1; /* in : whether we return thunks tags */
82 IMAGEHLP_STACK_FRAME ihsf; /* in : frame for local & parameter variables look up */
85 static BOOL CALLBACK sgv_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
87 struct sgv_data* sgv = (struct sgv_data*)ctx;
90 unsigned cookie = DLV_TARGET, insp;
92 if (sym->Flags & SYMFLAG_REGISTER)
94 const struct dbg_internal_var* div;
96 if (dbg_curr_frame != 0)
98 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
102 for (div = dbg_context_vars; div->name && div->val != sym->Register; div++);
105 dbg_printf(" %s (register): couldn't find register %lu\n",
106 sym->Name, sym->Register);
109 addr = (ULONG64)(DWORD_PTR)div->pval;
112 else if (sym->Flags & SYMFLAG_FRAMEREL)
115 struct dbg_type type;
117 type.module = sym->ModBase;
118 type.id = sym->TypeIndex;
119 types_get_info(&type, TI_GET_OFFSET, &offset);
120 addr = sgv->ihsf.FrameOffset + offset;
122 else if (sym->Flags & SYMFLAG_THUNK)
124 if (!sgv->do_thunks) return TRUE;
131 il.SizeOfStruct = sizeof(il);
132 SymGetLineFromAddr(dbg_curr_process->handle, sym->Address, &disp, &il);
133 if (sgv->filename && strcmp(sgv->filename, il.FileName))
135 WINE_FIXME("File name mismatch (%s / %s)\n", sgv->filename, il.FileName);
139 if (sgv->lineno == -1)
142 !symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &addr))
150 if (sgv->lineno == il.LineNumber)
155 } while (SymGetLineNext(dbg_curr_process->handle, &il));
158 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
159 sgv->lineno, sgv->name);
165 if (sgv->num >= NUMDBGV)
167 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
171 WINE_TRACE("==> %s %s%s%s%s%s%s\n",
173 (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
174 (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
175 (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
176 (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
177 (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
178 (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
180 /* always keep the thunks at end of the array */
182 if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
184 insp -= sgv->num_thunks;
185 memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
186 sizeof(sgv->syms[0]) * sgv->num_thunks);
188 sgv->syms[insp].lvalue.cookie = cookie;
189 sgv->syms[insp].lvalue.addr.Mode = AddrModeFlat;
190 sgv->syms[insp].lvalue.addr.Offset = addr;
191 sgv->syms[insp].lvalue.type.module = sym->ModBase;
192 sgv->syms[insp].lvalue.type.id = sym->TypeIndex;
193 types_get_info(&sgv->syms[insp].lvalue.type, TI_GET_TYPE,
194 &sgv->syms[insp].lvalue.type.id);
195 sgv->syms[insp].flags = sym->Flags;
201 /***********************************************************************
204 * Get the address of a named symbol.
206 * sglv_found: if the symbol is found
207 * sglv_unknown: if the symbol isn't found
208 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
209 * and user didn't pick one of them)
211 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
212 struct dbg_lvalue* rtn, BOOL bp_disp)
216 char tmp[sizeof(SYMBOL_INFO) + 256];
217 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
221 if (strlen(name) + 4 > sizeof(buffer))
223 WINE_WARN("Too long symbol (%s)\n", name);
229 sgv.name = &buffer[2];
232 sgv.bp_disp = bp_disp ? TRUE : FALSE;
233 sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
237 strcpy(&buffer[2], name);
239 /* this is a wine specific options to return also ELF modules in the
242 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
243 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
249 if (!sgv.num && (name[0] != '_'))
252 strcpy(&buffer[3], name);
253 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
261 /* now grab local symbols */
262 si->SizeOfStruct = sizeof(*si);
263 si->MaxNameLen = 256;
264 if (stack_get_frame(si, &sgv.ihsf) && sgv.num < NUMDBGV)
266 if (SymSetContext(dbg_curr_process->handle, &sgv.ihsf, NULL))
267 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
272 dbg_printf("No symbols found for %s\n", name);
276 if (dbg_interactiveP)
278 if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
279 (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
280 (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
282 dbg_printf("Many symbols with name '%s', "
283 "choose the one you want (<cr> to abort):\n", name);
284 for (i = 0; i < sgv.num; i++)
286 if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
288 dbg_printf("[%d]: ", i + 1);
289 if (sgv.syms[i].flags & SYMFLAG_LOCAL)
291 dbg_printf("local variable %sof %s\n",
292 sgv.syms[i].flags & SYMFLAG_REGISTER ? "(in a register) " : "",
295 else if (sgv.syms[i].flags & SYMFLAG_PARAMETER)
297 dbg_printf("parameter %sof %s\n",
298 sgv.syms[i].flags & SYMFLAG_REGISTER ? "(in a register) " : "",
301 else if (sgv.syms[i].flags & SYMFLAG_THUNK)
303 print_address(&sgv.syms[i].lvalue.addr, TRUE);
304 /* FIXME: should display where the thunks points to */
305 dbg_printf(" thunk %s\n", name);
309 print_address(&sgv.syms[i].lvalue.addr, TRUE);
316 if (input_read_line("=> ", buffer, sizeof(buffer)))
318 if (buffer[0] == '\0') return sglv_aborted;
320 if (i < 1 || i > sgv.num)
321 dbg_printf("Invalid choice %d\n", i);
323 } while (i < 1 || i > sgv.num);
325 /* The array is 0-based, but the choices are 1..n,
326 * so we have to subtract one before returning.
333 /* FIXME: could display the list of non-picked up symbols */
335 dbg_printf("More than one symbol named %s, picking the first one\n", name);
337 *rtn = sgv.syms[i].lvalue;
341 BOOL symbol_is_local(const char* name)
344 char tmp[sizeof(SYMBOL_INFO) + 256];
345 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
353 sgv.do_thunks = FALSE;
355 si->SizeOfStruct = sizeof(*si);
356 si->MaxNameLen = 256;
357 if (stack_get_frame(si, &sgv.ihsf) &&
358 SymSetContext(dbg_curr_process->handle, &sgv.ihsf, NULL))
359 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
363 /***********************************************************************
364 * symbol_read_symtable
366 * Read a symbol file into the hash table.
368 void symbol_read_symtable(const char* filename, unsigned long offset)
370 dbg_printf("No longer supported\n");
373 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
374 * this with an already loaded module !!
383 if (!(symbolfile = fopen(filename, "r")))
385 WINE_WARN("Unable to open symbol table %s\n", filename);
389 dbg_printf("Reading symbols from file %s\n", filename);
393 fgets(buffer, sizeof(buffer), symbolfile);
394 if (feof(symbolfile)) break;
396 /* Strip any text after a # sign (i.e. comments) */
397 cpnt = strchr(buffer, '#');
398 if (cpnt) *cpnt = '\0';
400 /* Quietly ignore any lines that have just whitespace */
401 for (cpnt = buffer; *cpnt; cpnt++)
403 if (*cpnt != ' ' && *cpnt != '\t') break;
405 if (!*cpnt || *cpnt == '\n') continue;
407 if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
409 if (value.addr.off + offset < value.addr.off)
410 WINE_WARN("Address wrap around\n");
411 value.addr.off += offset;
412 SymAddSymbol(current_process->handle, BaseOfDll,
420 /***********************************************************************
421 * symbol_get_function_line_status
423 * Find the symbol nearest to a given address.
425 enum dbg_line_status symbol_get_function_line_status(const ADDRESS* addr)
429 ULONG64 disp64, start;
430 DWORD lin = (DWORD)memory_to_linear_addr(addr);
431 char buffer[sizeof(SYMBOL_INFO) + 256];
432 SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
433 struct dbg_type type;
435 il.SizeOfStruct = sizeof(il);
436 sym->SizeOfStruct = sizeof(SYMBOL_INFO);
437 sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
439 /* do we have some info for lin address ? */
440 if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
441 return dbg_no_line_info;
446 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
447 * and furthermore, we no longer take care of them !!!
449 return dbg_in_a_thunk;
451 case SymTagPublicSymbol: break;
453 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym->Tag);
455 return dbg_no_line_info;
457 /* we should have a function now */
458 if (!SymGetLineFromAddr(dbg_curr_process->handle, lin, &disp, &il))
459 return dbg_no_line_info;
461 if (symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &start) && lin < start)
462 return dbg_not_on_a_line_number;
463 type.module = sym->ModBase;
464 type.id = sym->TypeIndex;
465 if (!types_get_info(&type, TI_GET_LENGTH, &size) || size == 0)
467 if (il.FileName && il.FileName[0] && disp < size)
468 return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
470 return dbg_no_line_info;
473 /***********************************************************************
476 * Find the symbol nearest to a given address.
477 * Returns sourcefile name and line number in a format that the listing
478 * handler can deal with.
480 BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line)
488 sgv.name = &buffer[2];
489 sgv.filename = filename;
492 sgv.do_thunks = FALSE;
496 strcpy(&buffer[2], name);
498 /* this is a wine specific options to return also ELF modules in the
501 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
502 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
508 if (!sgv.num && (name[0] != '_'))
511 strcpy(&buffer[3], name);
512 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
523 if (filename) dbg_printf("No such function %s in %s\n", name, filename);
524 else dbg_printf("No such function %s\n", name);
527 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
529 return SymGetLineFromAddr(dbg_curr_process->handle,
530 (DWORD)memory_to_linear_addr(&sgv.syms[0].lvalue.addr),
536 static BOOL CALLBACK info_locals_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
539 const char* explain = NULL;
541 struct dbg_type type;
544 type.module = sym->ModBase;
545 type.id = sym->TypeIndex;
546 types_get_info(&type, TI_GET_TYPE, &type.id);
547 types_print_type(&type, FALSE);
549 if (sym->Flags & SYMFLAG_LOCAL) explain = "local";
550 else if (sym->Flags & SYMFLAG_PARAMETER) explain = "parameter";
551 else if (sym->Flags & SYMFLAG_REGISTER) explain = buf;
553 if (sym->Flags & SYMFLAG_REGISTER)
555 const struct dbg_internal_var* div;
557 if (dbg_curr_frame != 0)
559 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
563 for (div = dbg_context_vars; div->name; div++)
565 if (div->val == sym->Register)
568 sprintf(buf, "local in register %s", div->name);
573 else if (sym->Flags & SYMFLAG_FRAMEREL)
575 type.id = sym->TypeIndex;
576 types_get_info(&type, TI_GET_OFFSET, &v);
577 v += ((IMAGEHLP_STACK_FRAME*)ctx)->FrameOffset;
579 dbg_read_memory_verbose((void*)v, &val, sizeof(val));
581 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym->Name, val, explain);
586 int symbol_info_locals(void)
588 IMAGEHLP_STACK_FRAME ihsf;
589 char buffer[sizeof(SYMBOL_INFO) + 256];
590 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
592 si->SizeOfStruct = sizeof(*si);
593 si->MaxNameLen = 256;
594 if (stack_get_frame(si, &ihsf))
596 dbg_printf("%s:\n", si->Name);
597 if (SymSetContext(dbg_curr_process->handle, &ihsf, NULL))
598 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, &ihsf);
603 static BOOL CALLBACK symbols_info_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
605 struct dbg_type type;
608 mi.SizeOfStruct = sizeof(mi);
610 if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
611 mi.ModuleName[0] = '\0';
614 size_t len = strlen(mi.ModuleName);
615 if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
616 mi.ModuleName[len - 5] = '\0';
619 dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
620 type.id = sym->TypeIndex;
621 type.module = sym->ModBase;
623 if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0 &&
624 types_get_info(&type, TI_GET_TYPE, &type.id))
627 types_print_type(&type, FALSE);
633 void symbol_info(const char* str)
638 if (strlen(str) + 3 >= sizeof(buffer))
640 dbg_printf("Symbol too long (%s)\n", str);
645 strcpy(&buffer[2], str);
646 /* this is a wine specific options to return also ELF modules in the
649 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
650 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);